This is a design issue I keep running into, so I thought I would finally put it out there and see how people would approach it. The problem is as follows:
I identify a certain class that for the most part describes all instances of objects I will use, both behaviour and data-wise. That's great and works well for basic objects. Then a few other type of object crop up which need the same data and behaviour, but additionally would like to have an extra field here and there, or an extra data structure.
Let's call the class Something:
public class Something {
private int id;
private String fieldA;
private String fieldB;
private List<Data> list;
// Then we have getters, setters, and some base methods
}
Sometimes we'll need to use SomethingElse and SomethingDifferent. They are 90% like Something in that the same data and behaviour describes them, however they each additionally have extra fields which need to be used by the rest of the program:
public class SomethingElse extends Something {
private String dataSpecificToSomethingElse;
// Then we have getters, setters, and some new-data specific methods
}
public class SomethingDifferentextends Something {
private List<DifferentData> dataSpecificToSomethingDifferent;
// Then we have getters, setters, and some new-data specific methods
}
I would like to come up with a decent way to handle the Something family of objects in a generic OO manner, as I would not like to couple the rest of my application on concrete implementation details (because we might need to add SomethingWacky later on). I don't want to deal with the subclasses directly as that breaks polymorphism, and will likely include a need to downcast/do a type switch - yuck.
The approaches I can think of to resolve this are as follows:
I've used approach 1 during a previous project - but in that instance even though each subclass only implemented/overrode the methods it cared about, the operations were actually generic enough so it was plausible that a class could coherently implement all or only some.
Each approach feels dirty in a way and I don't really like any. What are my alternatives? Perhaps I am totally misusing inheritance or approaching this the wrong way entirely. I'm open to any suggestions and would like to leverage OO techniques to come up with cleaner, decoupled designs. I'd really like to know how people have resolved issues like this, and any resources you could refer me to would be much appreciated.
Thanks
When the need to add SomethingElse, SomethingDifferent, etc. crops up, I'd ask: Does SomethingElse really need to be able to individually access all data and behavior elements of Something? Or is its use of Something limited to a few methods?
If it's the latter, it's usually a good idea to encapsulate the specific behavior of Something that is frequently used by other classes, and then use composition rather than inheritance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With