This is a design problem that I encounter all the time, so I thought that I would finally post it there and see how people pick it up. The problem is this:
I identify a specific class, which for the most part describes all instances of objects that I will use, both behavior and data. This is great and works well for basic objects. Then there are several other types of objects that need the same data and behavior, but would also like to have an additional field here and there or an additional data structure.
Call the Something class:
public class Something { private int id; private String fieldA; private String fieldB; private List<Data> list;
Sometimes we need to use SomethingElse and SomethingDifferent. They are 90% similar to the fact that the same data and behavior describe them, however they additionally have additional fields that should be used by the rest of the program:
public class SomethingElse extends Something { private String dataSpecificToSomethingElse;
I would like to come up with a decent way to handle the Something family of objects in a generic OO style, since I would not want to associate the rest of my application with specific implementation details (because we might need to add SomethingWacky later). I don’t want to deal with subclasses directly, as this breaks the polymorphism and will probably include the need to lower / make a switch like - yuck.
The approaches that I can solve for this are as follows:
- Create an abstract base class that defines all the methods for the Something family. Then the children implement only the behavior that they relate to the provision, leaving the NOP / blank override for methods that do not present a problem. This allows everything to be handled the same, but introduces an interface bloat.
- Transfer responsibility to the base class through common working methods, following Tell, Don't Ask . For example, it could be things like display (), doWork (), persist (), getStateFromDisplay (), etc. Each subclass will take its data into account when overriding these basic methods. I recently read an article by Allen Holub, which suggested that this might be good practice. This seems like too much responsibility for the external problems for the class.
- Create a data class that groups all the extra data / behavior from subclasses and references this in Something. This is not very similar to OO-like.
I used approach 1 during the previous project, but in this case, despite the fact that each subclass only implemented / redefined the methods he cared for, the operations were generalized enough, so it was plausible that the class could execute all or only some .
Each approach seems messy in some way, and I really don't like it. What are my alternatives? Perhaps I am completely abusing inheritance or completely approaching this. I am open to any suggestions and would like to use OO methods to come up with cleaner, decoupled constructs. I would really like to know how people solved such problems, and any resources that you could refer to me would be very grateful.
thanks