gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 08:48:55 26 4
gpt4 key购买 nike

这是策略模式的定义:

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com