Search code examples
c#design-patternssolid-principles

Recommend a design pattern


An application I'm working on processes Work Items. Depending on the state of a work item there are a number of actions available. "Complete" "Cancel" "Reassign" etc...

To provide the functionality for the actions I currently have an interface that looks something like this...

public interface IActionProvider{
    public void Complete(WorkItem workItem);
    public void Cancel (WorkItem workItem);
    public void Reassign(WorkItem workItem);
}

Then based on other details of the work item I have concrete implementations of the interface. Just for example...

public class NormalActionProvider :IActionProvider
{
    ...
}

and

public class UrgentActionProvider : IActionProvider
{
   ....
}

The problem is, if I want to add a new action, say... "Delegate" I have to update the interface which of course has effects on all of the implementations.

Does this violate the Open/Close Principle? Can you recommend a design pattern or refactor that may help me here?


Solution

  • Looks like command pattern would be suitable. You can modify/add more commands. The command classes are decoupled from the main program.

    public interface IActionProvider{
        public void execute(WorkItem item,ActionType actionType);
    }
    

    ActionType represents Complete,Cancel & so on. You can keep adding more action types & plugin appropriate command classes.