I'm programming in C#. My class designs are getting rather complex. In many situations I'm attempting to avoid inheritance when possible; e.g., I prefer "has-a" design instead of "is-a" design.
I believe that my designs will run into a situation where the indicated eventual design would be multiple inheritance. I'm aware of the no-no's of multiple inheritance, and that both Java and C# don't allow that.
I will try and write some pseudo code to illustrate my situation.
Let's say in my application in development, I want to make a distinction between items that are visible on screen, and items that aren't. I will make a class "DisplayObject" that is the root class of all items to be displayed.
I have a "Button" class that will then inherit from DisplayObject.
class Button : DisplayObject
However, I want to create an abstract class "Clickable". (I know that, at this point, some may say "use an interface" e.g., IClickable. But for now, set that suggestion aside.)
The point of Clickable, and that being an abstract class, is that I want to provide some default functionality. The Button class (obviously) cannot inherit from both DisplayObject and Clickable.
One idea might be to make Clickable inherit from DisplayObject, and then Button inherit from Clickable.
class Clickable : DisplayObject
class Button : Clickable
That seems like a good idea, since I want to create a Link class that is also clickable. "Link" class can inherit from Clickable.
Without detailing what sort of "stinky" code growth this might cause, I seem to be working towards some sort of usage of the Decorator pattern. But from what I remember, the Decorator pattern is implemented such:
class Button : ClickableDecorator<DisplayObject>
I don't like that either. Especially if I plan on having a lot of "Decorator" additions to my class definitions.
So I might then be inclined to try making a List of pseudo Decorators as a member of the Button class:
public List<Decorator> Decorators { get; set; }
But that can still get confusing.
To summarize (in hopes of finding a solution):
- I don't want to do much "Prefactoring" (refactoring done ahead of time that is actually counterproductive).
- I don't want to go with a simplistic recommended solution, as that will cause a lot of headaches down the road, which will require a lot of Refactoring.
- Essentially, I'm hoping that there is a "has-a" solution to a multiple "is-a" dilemma.
Thank you for reading this far!