model-view-controller - MVC : why controller is the strategy for the view

标签 model-view-controller design-patterns strategy-pattern design-principles

这是策略模式的定义:

Strategy - defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

因此,Strategy 将功能分为两部分:一部分不会更改,另一部分可能会在未来某个时间更改。

而且,在 MVC 中,他们说:

the controller is the strategy for the view

这意味着 Controller 是可变的,View 将来可能不会改变。

我还不是很懂。我认为他们两个将来都可以改变。

请为我解释为什么人们这么说。

最佳答案

我怀疑您可能已经在 Model View Controller As An Aggregate Design Pattern 上的 C3 wiki 部分看到注释“ Controller 是 View 的策略”。 ,这很有趣,但有点简洁。

这个想法在Design Patterns 的介绍中有更完整的描述。 ,其中关于 MVC 的部分说:

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 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.

总结一下,您可以使用具有不同 Controller 的相同 View 在系统的不同部分具有不同的行为,您可以在运行时替换 Controller 以启用或禁用特定功能,并且 Controller 封装了复杂的更新模型的交互。

关于model-view-controller - MVC : why controller is the strategy for the view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10064105/

相关文章:

java - 如何在 Java Spring MVC 创建时将大量数据作为文件提供?

c# - .NET MVC 3 如何循环模型状态错误并强制 ModelState.Valid 或禁用特定错误?

php - Multi-Tenancy 数据库和一个模型 - MVC 和 Joomla

oop - 设计用户访问/权限类

java - 遍历要执行的命令列表——哪种模式?

ruby-on-rails - 策略模式文件位置 Rails

java - 如何避免在策略模式中使用 java 和泛型进行未经检查的强制转换

objective-c - 如何设计一个应用程序来在 iOS 中跨模型保留一组字符串?

parsing - 解析文本的设计模式

java - MongoDB 相关的扩展问题