In my model all the derived classes have the same ** persistent** attributes and methods as the base abstract class. There are some class specific attributes which aren't persisted and methods have different implementation.
Right now I have about 4 inheriting classes, and I will add more in the future. The nature of the application is that such classes may be added for different uses, so its impossible to know them in advance. The only given is that they will all share the same methods and persistent attributes. The is one column which will be used as discriminator.
I am struggling with strategy. Obviously I don't want to write a ClassMap for each derived class. In fact I's like the persistence layer to be completely ignorant of these derived classes. I am thinking of having the derived classes be able to be created off the base class and to return a base class.
I don't suppose I have any better option?
Your approach is flawed in that the persistence layer can not be ignorant about the subclasses, because it needs to know what the class is when loading/storing.
What you can do is use a convention-based mapping instead of an explicit one (Fluent has Automapping, and ConfORM is convention/override based only), so you don't have to write every classmap.
In ConfORM, it's as easy as saying, for example, orm.TablePerClass<TheBaseClass>(), then mapper.CompileMappingFor(TheBaseClassAndAllItsSubclasses), and you'll get the mappings without any additional effort.
Related
I have a core data abstract entity which is backed by a class that's subclassed for many concrete [sub]entities that I create and manipulate instances of. No attributes or relationships or properties are different in the subclasses. I'm just overriding method implementations. So other then setting the subclass and parent entity, the xcdatamodeld is empty for each subentity.
If there are no unique properties or relationships in the subentities in xcdatamodeld, and I'm creating quite a number of them, is there some code that I could create in the abstract superentity class to handle the core data subentity registration?
What I'm looking for is some reasonable code that lets me just create a .h/.m for each new subentity (which subclasses the abstract entity, which subclasses NSManagedObject), and skip seemingly duplicate and cluttering work in xcdatamodeld of +entity, edit name, add class, declare parent entity. Is this feasible and reasonable? Or do I go down a rabbit hole of arcane programmatic managedObjectModel editing outside of my class implementations?
[edit] To add background about why subclasses. One-- each subclass creates a different tree of child entities. Two-- each subclass assembles its data differently for passing to views. ie they all have the same string properties, but each subclass might present an attributedStringForTitle differently than the next.
Probably the closest you will get is by using mogenerator. You may be able to script changes to the data model, but unless you're going to create lots of classes then it likely won't save you time.
I am working on a project with class hierarchy as in the image. It has many classes but i am showing some of them. The problem is that there are some attributes that repeat in many classes but not in all classes. I want to reduce the occurance by creating a new class with common attributes, and use the class as Instance variable in all the classes. This way i can maintain my class instead of maintaining all the occurances all over the code. These attributes has no operation, and class also have methods so it does to fulfill the definition of class. The class has many levels of inheritance and if create subclasses, it has yo-yo problem then, so i do not want them to be further subclassed. I want to know is there any other way? or any better way to do it.
You have yo-yo problem when you have all concrete classes. I would suggest to make an abstract class and put all common attributes there. I also see that you have Unit value attributes repeating for same attribute. For this, you can use hash map to have only one attribute with two values for unit and for value.
I have a class A that depends on class B. They have very different functions but share similar methods. I though of splitting these classes and subclassing them from a common superclass, but they do not relate.
Should I subclass them from a common superclass, or should I create another class that contains components of the class A and class B?
Very broad scenario. From what I believe, if two completely unrelated classes have some operations in common, that totally represents some package-level or global utility operations.
You might consider extracting those operations into a utility class or regular class depending on the specific scenario.
I am designing a utility to backup applications.
The backup functionality will contain both common tasks to do (common code) and some unique steps. Am I on the right track by using an interface for the unique behaviour and an abstract base class for the common behaviour in common by all the children? Is there any downside to this approach? Anything better?
Thanks
If the base class actually implements some behaviour then I think it's called a non-abstract base class.
Anyway I think that's called Template method pattern: you may want to look that up in a dictionary of patterns (which should explain when it's appropriate, and reference any similar alternative patterns).
I wouldn't use abstract base classes to share common functionality, but only to express is-a relationships. If D derives from B, wherever B is expected, a D can come up. This is the criteria for using public inheritance.
You can use private inheritance though, but you are limited to derive from only one class in some languages.
Which brings us to the point to should be the first - you should think about responsibilites and encapsulate functionality wherever it belongs to, exposing interfaces (or pure abstract classes in C++) to clients, and implementing functionalities in concrete classes that derive from those interfaces.
Abstract classes are described as being useful for a family of objects (e.g. could be used for animals which are mammals). However, what difference is there between using an interface or abstract class for representing a family of related objects?
My process is to use an abstract class when I want to define common functionality but with the option for future extensions and an interface for custom functionality (implementations).
For example, I wrote an abstract class to encapsulate some database functionality which will be used heavily in a small web app at work. I wrote an abstract class with virtual methods which can be overrided with custom functionality in the future (e.g. logging, or some reporting of the database events which may be required).
Is this the right way to go? Is there any significance in choosing one construct (abstract or interface) to represent a family?
An abstract class should be used when there is common state and behavior between all types. An interface should be used when all types will have a common interface but will not share state or behavior.
Here is an example.
German Shepherd, Golden Retriever, Beagle
These three objects are all dogs, and as such they share certain common state (carnivorous, 4 legs, etc.) and they also share certain overridable behavior (bark, pant, etc.). In this instance it would make the most sense to create an abstract Dog class to hold this common state and behavior and create subtypes of Dog for each type of dog.
Pencil, Pen, Chalk
These objects have no common state and they cannot share behavior. Yet you may notice that they do have something in common - they are cabaple of writing. These objects are best build separately and without a base class and then tied together with a Writable interface that exposes each type's Write method.
I would suggest using interfaces so that you can implement new functionality in your database utility at some future point.
As always, the primary design principle when it comes to development is
Design towards an interface, not an implementation
With abstract classes, you can provide implementation that is needed and shared by all the classes in your hierarchy. Therefore, you're reusing code. You may allow the derived classes to override the default behavior or not but at least you're providing a baseline functionality like breathing for a new born animal. However, with interfaces, you can't provide any implementation. You simply define a contract that all classes that inherits that interface should honor and provide implementation for. This may lead to repetitive and duplicate code among the hierarchy of classes.
Interfaces are not very good for extensibility and you need to worry about versioning. You decide to make change to an existing interface but you will soon realize that there are a lot of classes in existence you may need to modify. Think about adding Breath method to IMammal interface that's already being used by many mammals. You will need to go and provide Breath implementation for each one. With an abstract class, you can simply add Breath method and provide some baseline implementation without having to worry about existing derived classes. So abstract classes are more flexible in term of the development of your hierarchy and the api.