`
experience
  • 浏览: 197741 次
社区版块
存档分类
最新评论

Supervising Controller - Martin's "GUI Architectures" series (2)

阅读更多

zz from www.martinfowler.com

To be translated...

Supervising Controller

Factor the UI into a view and controller where the view handles simple mapping to the underlying model and the the controller handles input response and complex view logic.

[TBD: Not sure yet whether to use the word 'controller' or 'presenter' here.]

Many UI frameworks provide the ability to easily map between the view and model, often using some kind of Data Binding. These approaches are very effective in allowing you to declaratively set up a relationship between elements in the view and model. Usually, however, there are more complex relationships which require you to have more complex view logic. This logic can be hard to manage, and in particular hard to test, while embedded in the view.

Supervising Controller uses a controller both to handle input response but also to manipulate the view to handle more complex view logic. It leaves simple view behavior to the declarative system, intervening only when effects are needed that are beyond what can be achieved declaratively.

How it Works

Supervising Controller decomposes presentation functionality into two parts: a controller (often called presenter) and view. The domain data that needs to be displayed is separate, and following rough MVC terminology I'll refer to it as a model, although it need not be a Domain Model. The basic division of responsibilities echoes the Model-View-Presenter architecture in its Dolphin form, as described by Bower and McGlashan.

A Supervising Controller has two primary responsibilities: input response and partial view/model synchronization.

For input response the controller operates in the presenter style. The user gestures are handled initially by the screen widgets, however all they do in response is to hand these events off to the presenter, which handles all further logic.

For view/model synchronization the controller defers as much of this as reasonable to the view. The view typically uses some form of Data Binding to populate much of the information for its fields. Where Data Binding isn't up to more complex interactions then the controller steps in.

Figure 1

Figure 1: Class diagram for the assessment example.

Figure 2

Figure 2: Sequence diagram showing the response for putting in a low actual value.

In the assessment window, the initial text change is handled by the text field widget in the view. This widget is observed by the controller, so when the text changes the widgets emits an event which results in the controller's actualFieldChanged method being called. This method then handles the full response to the event. It first updates the reading model object's actual value. The window observes the reading object so the change to the reading's value triggers a refresh. It's pretty easy to map the actual and variance text fields' text to the appropriate properties of the reading, so that updates those values. For the purposes of our example changing the color is a bit more involved. The reading object can, and should, determine which category the reading should fit into. A sophisticated widget might be able to bind its text color to a category like this, but we'll assume we don't have such a clever young thing. In this case controller takes over setting the variance field's text color directly.

As the example shows, the essence of a good Supervising Controller is to do as little as possible. Let the view handle as much as possible and only step in when there's more complex logic involved.

One of the prime reasons to use Supervising Controller is for testability. Assuming the view is hard to test, by moving any complex logic into the controller, we put the logic in a place that's easier to test. In order to run tests on the controller, however, we do need some form of view, so a Test Double is often in order. With the double in place, we don't have any need for UI framework objects in order to test the more awkward parts of the UI behavior.

This testability issue affects another decision - whether the controller should access the view and its widgets directly, or through an intermediary. With an intermediary we build a Gateway for the the controller. The gateway defines an interface for the controller to manipulate. One implementation then adapts the window's interface while the other provides a stub for testing (you could also use a mock). This is the same approach that you need for Passive View.

Figure 3

Figure 3: Using an intermediary between the controller and the window.

The discussion so far suggests using Flow Synchronization with Supervising Controller, but this need not be the case. It is possible to use Observer Synchronization, but it needs to be modified so that it's the controllers that observe the model rather than the views.

When to use it

There are two main reasons to look into using a Supervising Controller: to separate out the complexity of an Autonomous View, and to improve testability.

The separation advantage is that it pulls all the behavioral complexity away from the basic window itself, making it easier to understand. This advantage is offset by the fact that the controller is still closely coupled to its screen, needing a pretty intimate knowledge of the details of the screen. In which case there is a real question mark over whether it's worth the effort of making it a separate object.

The testability reason is the more compelling one. Many people I've talked to have found using some form of controller has made a big difference to making a UI that is properly testable.

If testability is your driver then the driving issue is how much behavior to leave in the view. Passive View is a very similar pattern to Supervising Controller, but with the difference that Passive View puts all the view update behavior in the controller, including simple cases. This results in extra programming, but does mean that all the presentation behavior is testable. The choice between the two depends on what kind of Data Binding support you have and whether you're happy to leave that untested by controller tests.

A third alternative is Presentation Model. This again pulls most behavior away from the view, but leaves it to the view to synchronize all its updates. There isn't a great deal of the difference in test coverage between Presentation Model and Supervising Controller - much of the choice (as with Passive View) depends on personal judgments.

Significant Revisions

19 Jun 06: First publication in development area of website.

评论

相关推荐

    e15-3yp-GUI,用于远程控制和监督多个机器人:这个想法是创建一个GUI平台,用户可以在其中发出命令,远程位置的一些地面单元可以跟随该命令。 服务器可以识别机器人的起始位置,并操纵它们以尽可能高效的方式完成任务

    2. **多线程**:为了同时处理多个机器人并实时更新GUI,多线程技术可能会被用来并发地处理不同的任务。 3. **GUI框架**:可能使用了Qt、wxWidgets或GTK+等C++ GUI库来构建用户界面,这些库提供了丰富的控件和事件...

    c# MVP模式范例解析

    2. **Supervising Controller**: 相比Passive View,Supervising Controller模式允许视图有一些简单的数据绑定和基本的事件处理。Presenter仍然负责处理复杂的业务逻辑和视图之间的协调工作,但它并不直接控制视图...

    SaflyMVP

    而Supervising Controller模式则允许View有一些简单的逻辑,但主要逻辑仍在Presenter。 总的来说,SaflyMVP可能是一个关于如何在项目中实施MVP架构的资源,帮助开发者理解并应用这一模式,从而提升应用的质量和可...

    基于python爬虫的菜价可视化系统源码数据库论文.docx

    The main task of the website is to crawl the prices and sales volume of vegetables in Guizhou's vegetable markets, effectively supervising the vegetable price situation through web crawling data, and...

    MVPFirstDo

    在实际开发中,MVP模式有多种变体,比如Passive View、Supervising Controller等,可以根据项目的复杂度和需求选择合适的实现方式。MVPFirstDo项目作为一个简单的示例,有助于初学者理解MVP模式的基本概念和工作流程...

    Android的MVP模式

    实际开发中,可以根据项目需求灵活调整MVP的具体实现,比如使用 Passive View 或 Supervising Controller 变种。总之,MVP模式是Android开发中一种实用的设计模式,能够提升代码质量并简化项目管理。

    plzrun:监督和重试命令行执行的工具。 运行某件事直到成功

    A tool for supervising and retrying command line executions. Runs something until it succeeds. Options: -r, --retries number How many times to retry the command if it fails (-1 is infinite tries, ...

    MVPRemoting_source

    MVP模式通常分为 Passive View、Supervising Controller 和 Presentation Model 三种变体,这里可能探讨了哪一种或多种变体如何适应远程通信的需求,比如处理延迟、数据同步、错误处理等问题。 【压缩包子文件的...

    MVP

    2. Supervising Controller:视图保留一定的逻辑,但主要的业务逻辑仍在Presenter中。视图可能会直接更新自身,但大的更改仍由Presenter控制。 **C#中的MVP实践** 在C#开发中,MVP模式可以显著提升代码的组织结构...

    MVPDemp.zip

    2. **Supervising Controller**:View有一些简单的逻辑,但仍依赖Presenter来处理复杂的业务逻辑。 3. **Clean MVP**:这是一种更为严格的MVP变体,强调Presenter的独立性,避免了View和Presenter之间的直接引用,...

    MVP模式的选择

    2. ** Supervising Controller MVP**:视图有一定的处理能力,例如可以处理简单的事件,而复杂的逻辑仍然在呈现器中处理。 3. ** Clean MVP**:这种变体强调了更严格的分离,通常会有单独的数据层接口,使得呈现器...

    MvpSurvivalKit

    在C#开发中,MVP模式通常分为三种类型: Passive View、Supervising Controller 和 Presentation Model。其中,Passive View是最基础的形式,View完全被动,不包含任何业务逻辑,所有的处理都由Presenter完成;...

    MVP模式实例

    **MVP模式详解** MVP(Model-View-Presenter)是一种软件设计模式,常用于Android和Web应用...在实际项目中,可以根据项目的规模和复杂度调整MVP的具体实现,例如使用 Passive View 或 Supervising Controller 变体。

    对等网中超节点有效选取与组织算法及仿真

    在给定的文件内容中,提出了一个新颖的超节点选取与组织机制,命名为SSMRF(Supervising Super-nodes Mechanism based on Rationality Factors)。SSMRF的主要创新点在于: 1. 综合评估节点的物理性能、自愿性以及...

    研究生英语考试词汇.docx

    - **例句**:*The architect knew that his clients were very particular about the new house he had designed for them, and he was careful supervising the building all along the way.*(建筑师知道他的客户...

    Go-littleboss:supervisor构建Go工具包

    Go包,littleboss可以让你把你的程序变成一个self-supervising的二进制文件。 它作为一个子进程自行启动,监视它的生命周期,如果它退出就重新加载它,并且可以指示它用一个新的二进制代替它。

    应用与物理学中的神经网络

    approach to supervising neural networks by specifying constraints that should hold over the output space, rather than direct examples of input-output pairs. These constraints are derived from prior ...

    2013年高考英语二轮专题复习命题预测90

    age, we learn that the practice of building one's own home in the United States has become a popular alternative for those seeking affordable housing options, particularly among young couples who ...

    浅谈新能源汽车实训教学过程的安全控制.pdf

    This includes supervising teachers and students, conducting electrical inspections, assessing the adequacy of facilities, devising应急预案, and strengthening safety awareness training. 2.1 ...

    PLC监控系统 升级版

    而“supervising software”可能就是PLC监控系统的可执行文件,用户需要按照“安装说明.txt”的指示进行操作,以便在电脑上运行并使用这款软件。 总的来说,【PLC监控系统 升级版】是一个强大的、免费的PLC管理工具...

Global site tag (gtag.js) - Google Analytics