Let me offer a slightly longer sentence or opinion that may provide some recommendations.
When designing - a hierarchy, I try to make distinctions only in behavior through polymorphism. In general, I try to avoid differences between objects only by data. Over the years, I even stopped creating inheritance hierarchies for structural “data objects” based on which variables they store.
Where I spend most of my time defining a common interface for an abstraction class, represents in terms of methods, and then the presence of subclasses that all lend themselves to the same interface, but with different behavior. (The principle of replacing Liskov)
In this case, I would consider a common interface / abstract class Item with a hierarchy of Item implementations that differ in how they add up, calculate their taxes, etc. I can have a .isInCatalog () method that tells me where the element is or its nature, if I have to handle it differently, but I will still try to encapsulate most of the element type logic in the implementation of my polymorphic methods.
Although these types of computations are usually performed in practice in batch / aggregation operations in a database and not in a single object, I would have at the abstract level .getTax () ,. getQuantity (), and then would have subclasses with different behavior. (Consider refactoring NullObject to not make any mistakes to avoid handling zeros)
The few principles that help this design are described here in this solid blog:
http://codebork.com/2009/02/18/solid-principles-ood.html
source share