I can't stop scratching my head over this trying to figure out the best way forward. Let's say I have a Model class which is selectable and all selectable objects can change their colour to show that they're selected. I want to put the functionality into separate interfaces because some things can change colour that aren't necessarily selectable, however, everything that is Selectable is going to need to be IColourable, so I can make the ISelectable interface inherit the IColourable interface like so:
interface ISelectable : IColourable
{
void SetIsSelected(bool isSelected);
}
interface IColourable
{
void UpdateColour(Color color);
}
class Model : ISelectable
{
private bool _isSelected;
public void UpdateColour(Color color)
{
...
}
public void SetIsSelected(bool isSelected)
{
_isSelected = isSelected;
}
}
So now I'm thinking to keep the model class small (because it implements a lot of other interfaces) lets delegate the implementation of ISelectable and IColourable to another object - like a ModelSelectableImpl and a ModelColourableImpl
So now class Model looks like:
class Model : ISelectable
{
private ISelectable _selectableImplementation;
private IColourable _colourableImplementation;
public Model()
{
_selectableImplementation = new ModelSelectableImpl(this);
_colourableImplementation = new ModelColourableImpl(this);
}
public void SetIsSelected(bool isSelected)
{
_selectableImplementation.SetIsSelected(isSelected);
}
public void UpdateColour(Color color)
{
_colourableImplementation.UpdateColour(color);
}
}
and I have another two classes to implement the models interfaces
class ModelColourableImpl : IColourable
{
private IColourable target;
public ModelColourableImpl(IColourable colourable)
{
target = colourable;
}
public void UpdateColour(Color color)
{
// Update targets colour
}
}
class ModelSelectableImpl : ISelectable
{
private Model target;
private bool _isSelected;
public ModelSelectableImpl(Model model)
{
target = model;
}
public void SetIsSelected(bool isSelected)
{
_isSelected = isSelected;
}
public void UpdateColour(Color color)
{
// GRRRRR I don't want this to have to be here, can probably do something like:
target.UpdateColour(color);
// But it just seems messy having it in this class
}
}
So you can see where the annoyance lies, I have a ModelSelectableImpl which now has an implementation of UpdateColour which passes it back to the model to pass to the ModelColourableImpl which just seems kinda messy.