Java Design Pattern
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 executi开发者_如何学Gong 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.
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.
This sounds like a variation of the Chain of Responsibility pattern.
Your IAction clearly follows the Command pattern.
Your list of actions to be executed could be a Composite Pattern.
Posting your code will help us to provide good solutions. Without the code, you will get guidelines only.
And one more thing : Design patterns are just guidelines. While implementing a solution, you should not thing your solution in a particular design pattern. After coming up with solution to your problem, then you can decide if your solution fits into a particular pattern. If not, still you can implement your solution without any design pattern.
Command_Pattern seems to be the solution for executing simple commands
. If your commands are independent of each other, simple command pattern solves your problem. If some of your commands are dependent other commands, then you have to use Chain-of-responsibility_pattern by establishing hierarchy.
Composite pattern can be used to store all your commands in Map object.
精彩评论