Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 284
BZink Avatar asked Aug 08 '11 02:08

BZink


People also ask

What is the best reason to use a design pattern?

Why use a design pattern? The usefulness of using a design pattern is obvious. The design pattern can accelerate the development process. It provides proven development paradigms, which helps save time without having to reinvent patterns every time a problem arises.

What is an example of a design pattern?

These design patterns are about organizing different classes and objects to form larger structures and provide new functionality. Structural design patterns are Adapter, Bridge, Composite, Decorator, Facade, Flyweight, Private Class Data, and Proxy.

Which design patterns are most important?

Adapter (Structural) The adapter pattern (or sometimes known as a wrapper) is one of the most useful and most popular design patterns in software engineering. This pattern seeks to solve the problem of incompatible interfaces between a client and a service provider.


1 Answers

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.

like image 143
isobar Avatar answered Sep 29 '22 16:09

isobar