How do Gang of Four Design Patterns fit into the MVC paradigm?
我已经仔细考虑了一段时间的设计模式,我刚刚开始了解如何在我的开发工作中更谨慎地将这些模式结合起来。然而,我仍然对他们在本书开头对MVC的处理以及它与本书其余部分的关系感到困惑。
我使用过的大多数框架——Spring、YII、ASP.NET甚至Objective-C Cocoa(uikit)——都符合MVC范式。我之所以得到MVC,是因为对我来说,它是一种有用的分类对象的方法,以及它们应该如何相互传递消息或进行交互。另外,这些框架有点强迫你,即使你不打算用MVC的方式思考。
我还觉得我理解设计模式的前提:它们真的不喜欢子类化,它们喜欢抽象接口,它们努力实现松散耦合。我还不能说我完全理解所有的模式,或者它们是如何有用的,但是我正在体验它。
我的问题是:MVC和设计模式之间的相互作用是什么?在书的第一章中,通过MVC应用程序示例,他们学到了什么?某些设计模式在MVC范式中是否只是不相关的?例如,我想知道命令模式应该如何适应MVC。它看起来非常有用,但是我们是否创建了一个
我个人的观点是,MVC是观察者模式的简化版本,它是中介者模式的简化版本。
MVC:一个模型,一个视图,控制器管理它们之间的通信。
观察者模式:一个模型,多个视图(观察者/订阅者),发布者管理通信
中介模式:几个不同的模型、几个视图,中介管理它们之间的通信。
GOF手册中的MVC是用于桌面的,它使用观察者模式来更新视图。GOF手册中的命令示例用于编辑器。
MVC还有其他风格,使用其他设计模式可能并不明显:MVC和MVVM有什么区别?表示抽象控制
政府的书上说:
...
Taken at face value, this example reflects a design that decouples views from models. But the design is applicable to a more general problem: decoupling objects so that changes to one can affect any number of others without requiring the changed object to know details of the others. This more general design is described by the Observer (page 293) design pattern.
Another feature of MVC is that views can be nested. For example, a control panel of buttons might be implemented as a complex view containing nested button views. The user interface for an object inspector can consist of nested views that may be reused in a debugger. MVC supports nested views with the CompositeView class, a subclass of View. CompositeView objects act just like View objects; a composite view can be used wherever a view can be used, but it also contains and manages nested views.
Again, we could think of this as a design that lets us treat a composite view just like we treat one of its components. But the design is applicable to a more general problem, which occurs whenever we want to group objects and treat the group like an individual object. This more general design is described by the Composite (163) design pattern. It lets you create a class hierarchy in which some subclasses define primitive objects (e.g., Button) and other classes define composite objects (CompositeView) that assemble the primitives into more complex objects.
MVC also lets you change the way a view responds to user input without changing its visual presentation. You might want to change the way it responds to the keyboard, for example, or have it use a pop-up menu instead of command keys. MVC encapsulates the response mechanism in a Controller object. There is a class hierarchy of controllers, making it easy to create a new controller as a variation on an existing one.
A view uses an instance of a Controller subclass to implement a particular response strategy; to implement a different strategy, simply replace the instance with a different kind of controller. It's even possible to change a view's controller at run-time to let the view change the way it responds to user input. For example, a view can be disabled so that it doesn't accept input simply by giving it a controller that ignores input events.
The View-Controller relationship is an example of the Strategy (315) design pattern. A Strategy is an object that represents an algorithm. It's useful when you want to replace the algorithm either statically or dynamically, when you have a lot of variants of the algorithm, or when the algorithm has complex data structures that you want to encapsulate.
MVC uses other design patterns, such as Factory Method (107) to specify the default controller class for a view and Decorator (175) to add scrolling to a view. But the main relationships in MVC are given by the Observer, Composite, and Strategy design patterns.
...
MVC是一种模式。但它只覆盖Web应用程序的一小部分。与MVC一起使用的另一个常见模式是存储库。这些是更多的架构模式……它们的范围对整个项目的影响更大。
gof模式将以很小的方式在各地介绍自己。它们可以根据设计选择帮助构建MVC基础结构。例如,策略被大量使用,因此您可以插入不同的方式来执行诸如"身份验证"等操作。
您不必像现在那样使用模式,它们甚至不必是完全相同的代码结构。更多的是您在设计中使用的模式的设计原则/目标。
MVC是一种体系结构模式。它完全适合其他设计模式,如命令模式。但是您不应用模式仅仅是因为它们存在,并且它们被写在权威的书中。当您遇到编程/设计问题,并且有一种方法可以解决由其他人发现并记录下来的问题时,可以应用模式。解决问题的方法是模式。例如,您有一个将数据保存到数据库的应用程序。要保存的数据非常复杂:必须插入一些记录,更新一些记录,删除一些记录。步骤的顺序很重要,因为要插入到一个表中的记录取决于要插入到另一个表中的记录。因此,必须使用数据库事务。实现事务的一种可能方法是使用命令模式。在Larman的《应用UML和模式》一书中(章节"设计一个持久性框架和模式",章节"用命令模式设计一个事务"-向下滚动到第556页)很好地解释了实现这一点的方法。