views:

93

answers:

3

I am trying to design an application where I will have a Map of IAction objects. Each IAction object has a method IAction IAction.processAction() where it is executing the knowledge contained within it. This could querying the database calling a web service, etc. After the execution of each IAction control is then sent to the next IAction instance.

When the application starts up it will contain a Map of Map that will be in the correct order of execution. The key of Integer type of the Map is the order for which execution will run. IAction.processAction() could jump control to the last IAction in the Map or stop it all together.

I can visualize the code in my head and I've written some lines to help me with this. I am looking for a Design Pattern that would easily help with this type of processing. I am not sure if the Command pattern would fit this role.

I was hoping someone could tell me which patterns they feel may fit the bill or not.

+1  A: 

Your IAction clearly follows the Command pattern.

Your list of actions to be executed could be a Composite Pattern.

KLE
+3  A: 

This sounds like a variation of the Chain of Responsibility pattern.

R. Bemrose
+4  A: 

It sounds like the Command pattern certainly has some relevance here.

However, I think you're going about it the wrong way. Design patterns are essentially meant to be examples and recipes to steer your thoughts along productive lines when designing how a product's going to work. What you're doing, taking a good design and then trying to shoehorn it into an "official" Design Pattern, is all backwards.

Other than that, what's the key in your map of IAction objects? It sounds like a (possibly linked) list is a much more natural structure for them.

Andrzej Doyle
+1 for the explanation on pattern usage
geowa4
I haven't done testing on it, but wouldn't a LinkedList be a bad choice for something that may seek to the end of the list from any point? ("`IAction.processAction()` could jump control to the last `IAction`")
R. Bemrose
@ R. Bemrose: in that case yes, definitely. My understanding was that the actions were tried in sequence: "After the execution of each `IAction`, control is then sent to the next `IAction` instance." The actual data structure does depend on how things fit together, though, which is why this was a suggestion for consideration rather than a recommendation.
Andrzej Doyle
You could be right about trying to shoe-horn a design pattern. I have a Map of IAction when a message comes in based on definitions in my database. The first IAction to be processed is also defined in the tables. The sequence of all of the following IAction could change at runtime. I am trying to define the best interface and controller that acts on those interfaces. Thanks for the help.
Peter Delaney