Java设计模式
我正在尝试设计一个应用程序,其中有一个 IAction
对象的映射。每个IAction
对象都有一个IAction IAction.processAction()
方法,用于执行其中包含的知识。这可以查询数据库,调用 Web 服务等。执行每个 IAction
控件后,然后将其发送到下一个 IAction
实例。
当应用程序启动时,它将包含一个 Map
的 Map,该 Map 将按照正确的执行顺序进行。 Map
的 Integer
类型的键是执行的顺序。 IAction.processAction()
可以将控制跳转到 Map 中的最后一个 IAction
或一起停止。
我可以在脑海中想象代码,并且我已经编写了一些行来帮助我解决这个问题。我正在寻找一种可以轻松帮助进行此类处理的设计模式。我不确定命令模式是否适合这个角色。
我希望有人能告诉我他们认为哪些模式适合或不适合。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
听起来命令模式在这里肯定有一些相关性。
但是,我认为您的做法是错误的。设计模式本质上是作为示例和秘诀,在设计产品的工作方式时引导您的想法沿着生产线进行。你所做的,采取一个好的设计,然后试图将其硬塞进“官方”设计模式,都是倒退的。
除此之外,
IAction
对象映射中的键是什么?听起来(可能是链接的)列表对他们来说是一种更自然的结构。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.
您的 IAction 显然遵循 Command 模式。
您要执行的操作列表可以是复合模式。
Your IAction clearly follows the Command pattern.
Your list of actions to be executed could be a Composite Pattern.
发布您的代码将有助于我们提供良好的解决方案。如果没有代码,您将只能获得指南。
还有一件事:设计模式只是指南。在实现解决方案时,您不应该将您的解决方案置于特定的设计模式中。提出问题的解决方案后,您可以决定您的解决方案是否适合特定模式。如果没有,您仍然可以在没有任何设计模式的情况下实现您的解决方案。
Command_Pattern
似乎是执行简单命令的解决方案
。如果您的命令彼此独立,则简单的命令模式可以解决您的问题。如果您的某些命令依赖于其他命令,那么您必须使用 Chain-of-responsibility_pattern< /a> 通过建立层次结构。Composite 模式可用于将所有命令存储在 Map 对象中。
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.