How would someone join two classes each with independent inheritance trees to bridge context boundaries, given the restrictions that neither class’s inheritance trees can be modified to inherit from the other?
i.e., if an item that is an entity saved to a database, let’s say:
public class Stockitem : StockItemBase { ... }
needs to be displayed using a drawing component, let’s say:
Public class GraphicNode : BaseNode { ... }
It would be nice to simply have a subclassed stockitemgraphicnode as there are many common members.
What is the best way to design a solution?
Composition.
Create a new class that inherits from neither but has an instance of both the classes you want to use within it, then expose the methods you want to call.
Google ‘Composition over Inheritence’ for much better ramblings on the subject than mine