I am looking for the best way to model a scenario in C#, which I would describe as “crossinheritance”.
First, I have a directed graph, which nodes can be of two different types: Connector and Component. I create an interface IObjectBase which contains properties both Connector and Component have, like a list of IObjectBase-Children. Then I create interfaces for the Connectors and Components which inherited from IObjectBase:
public interface IObjectConnector : IObjectBase ...
public interface IObjectComponent : IObjectBase ...
The implementation looks similar, I create an abstract class for the BaseObject and inherit it for Connector and Component classes:
public abstract class ObjectBase : IObjectBase ...
public class ObjectConnector : ObjectBase, IObjectConnector ...
public class ObjectComponent : ObjectBase, IObjectComponent ...
So far so good. The problem is, that the directed graph can exist in two different “states” or “modes” – in Editor-Mode and in Configuration-Mode. Depending on the mode, there are Properties and methods Connectors and Components both have (like adding children only when in editor-mode or a “isSelected” property only when in configurator-mode).
So, I would need to have 4 different objects: ComponentConfig, ComponentEdit, ConnectorConfig, ConnectorEdit
I started with thinking about further inherit IObjectConnector and IObjectComponent:
public interface IObjectConnectorEditor : IObjectConnector
public interface IObjectConnectorConfigurator : IObjectConnector
...
and create objects while inheriting from the existing Components and Connectors:
public class ObjectConnectorEditor : ObjectConnector, IEditor
...
public class ObjectComponentEditor: ObjectComponent, IEditor
The problem now is: I would need to implement the IEditor-interface in both Editor-classes, which would be an awefull redundancy. But I cannot create an implementation of IEditor from which the objects would inherit, because I can only inherit from exactly one class in C#, so the following will not work:
public abstract class Editor : IEditor ...
public class ObjectConnectorEditor: ObjectConnector, Editor
What would you suggest as the best architectural solution for this problem?
Thanks in advance,
Frank
I agree with Polity’s answer, but if you really want to fake up multiple inheritance in C#:
Create Component, Connector, Edit, and Config as separate classes. Also create IComponent, IConnector, IEdit, and IConfig as separate, matching interfaces.
Now you can create ComponentConfig, implementing IComponent and IConfig, and just delegate the interface methods to private instances of Component and Config.
See http://en.wikipedia.org/wiki/Delegation_pattern