MVC and command pattern

六眼飞鱼酱① 提交于 2019-12-06 05:31:19

问题


Ok, this is kinda dumb. I'm trying to wrap my head around the use of the MVC and Command Patterns. The questions basically concern where to place code.

In the MVC pattern, where are the Commands instantiated? Are they created by the Controller, or are they contained fully in the Model or neither.

BTW, Should one be using the Command pattern if there is no need of undo/redo.

Regards


回答1:


While there are many variations based on the needs/complexity of an application, you typically find them implemented in the Controller. Here is a great article on using the Command pattern in an MVC architecture. I'm not sure what type of application you are building, but here are a few more resources that show implementations on different application stacks.

http://java.sun.com/blueprints/guidelines/designing_enterprise_applications_2e/web-tier/web-tier5.html

http://msdn.microsoft.com/en-us/library/ff647590.aspx

http://www.phpwact.org/pattern/model_view_controller




回答2:


To implement Command pattern in an MVC application depends on your specific need. Controller does not create any Command by default. If you need to implement command pattern in a MVC application you have to do it on your own.

You can either implement the commands in the Controller or can do in a separate Service layer. It should not be in Model as Command pattern is behavioural and should be independent of model.



来源:https://stackoverflow.com/questions/3148097/mvc-and-command-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!