I have a scenario where I need to return a list of actions and represent them on a context menu.
The software has a standard list of actions, e.g:
- Analyse
- Design
- Develop
- Implement
I am tasked with extending the original software for a different client, who would like to add an extra action, e.g.:
- Analyse
- Design
- Develop
- Test
- Implement
I would like to develop this conforming to the OCP, so I have written the following factory class:
internal class ActionFactory
{
public IList<Action> Create()
{
var actionProvider =
Composition.Entity.CreateProvider<ActionProvider, Client>()[CurrentlyRunningClient];
return
actionProvider != null
?
StandardActions.Concat(actionProvider.AdditionalActions).ToList()
:
StandardActions;
}
private IList<Action> StandardActions
{
get
{
return
new Action[]
{
new Analyse(),
new Design(),
new Develop(),
new Implement()
}
.ToReadOnlyCollection();
}
}
}
The ActionProvider will seek out anything that implements a certain interface using reflection, returning me this class (if running the client that requires the new action):
internal class ActionProvider
{
public IList<MedicationAction> AdditionalActions
{
get
{
return new Action[]
{
new Test()
}
.ToReadOnlyCollection();
}
}
}
As far as conforming to OCP goes -- so far, so good! If anyone else wants to introduce a new action, then they don't have to modify any existing code. But the factory returns the standard list of actions with any new ones just tagged onto the end.
How would I specify an order for these actions without having to write some sort of class that would know about every possible action? (as I see it, violation of the OCP).