0Pricing
Clean Architecture & Design Patterns in Practice · 课时

谦卑对象与视图边界

应用谦卑对象模式,让视图代码保持精简且易于测试,并将所有决策推入表示层边界中可测试的表示器。

谦卑对象与视图边界 是 CoddyKit 上的免费 Clean Architecture & Design Patterns in Practice 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Clean Architecture & Design Patterns in Practice 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Clean Architecture & Design Patterns in Practice 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

The Hardest Code to Test

UI code is notoriously hard to test: it touches frameworks, screens, and event loops.

The Humble Object pattern solves this by splitting behavior so that the hard-to-test part becomes humble — nearly logic-free.

The Pattern in One Idea

Separate code into two parts across a boundary:

  • A humble part: thin, dumb, hard to test (the view).
  • A testable part: holds all the logic (the presenter).

Almost all behavior moves to the testable side.

Applying It to the View

The view becomes humble: it only displays already-formatted data and forwards user events.

It contains no formatting decisions, no conditionals about what to show — just assignment of fields to widgets.

The View Interface

Define what the presenter can ask the view to display.

interface OrderView {
    void showTotal(String formattedTotal);
    void showError(String message);
}

The Testable Presenter

The presenter does all the work and pushes finished strings to the humble view.

class OrderPresenter {
    private final OrderView view;
    OrderPresenter(OrderView view) { this.view = view; }
    void present(double total) {
        if (total < 0) { view.showError("Invalid total"); return; }
        view.showTotal("$" + String.format("%.2f", total));
    }
}

Why This Is Testable

Because the presenter talks to a view interface, a test supplies a fake view and asserts what was shown — no framework required.

class FakeView implements OrderView {
    String shown;
    public void showTotal(String t) { shown = t; }
    public void showError(String m) { shown = m; }
}

A Runnable Example

The presenter formats; the humble view simply records what it was told.

public class Main {
  interface View { void show(String s); }
  static class Presenter {
    final View v;
    Presenter(View v){ this.v=v; }
    void present(double total){ v.show(total<0 ? "Invalid" : "$"+String.format("%.2f", total)); }
  }
  static class FakeView implements View { String last; public void show(String s){ last=s; } }
  public static void main(String[] a){
    FakeView fv = new FakeView();
    new Presenter(fv).present(12.5);
    System.out.println("View shows: " + fv.last);
  }
}

The Boundary Is the Key

The humble object pattern always centers on a boundary interface. Logic lives on the testable side of that boundary; the framework lives on the humble side.

This is exactly how Clean Architecture keeps frameworks at arm length.

Where Else It Applies

  • Database access: a humble gateway, testable logic above it.
  • Hardware or sensors: humble drivers, testable controllers.
  • Web handlers: humble controllers delegating to interactors.

Anywhere the framework boundary is hard to test.

Keeping the View Truly Humble

Resist sneaking logic back into the view. The moment a view starts deciding what to format or whether to show something, it stops being humble and becomes untestable again.

Guidelines

  • Define a view interface the presenter drives.
  • Put all formatting and branching in the presenter.
  • Let the view only assign values and emit events.
  • Test the presenter with a fake view.

Quick Check

Test your understanding of the Humble Object pattern.

Recap

You learned the Humble Object pattern for the view boundary.

  • Split into a humble view and a testable presenter.
  • All logic lives on the testable side of a boundary interface.
  • This keeps frameworks out of your tests and your core.

常见问题解答

「谦卑对象与视图边界」课时是免费的吗?

是的 — 「谦卑对象与视图边界」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Clean Architecture & Design Patterns in Practice 课程的其余内容,请升级到 CoddyKit PRO。 Clean Architecture & Design Patterns in Practice 课程共包含 4 节课。

「谦卑对象与视图边界」这节课中我会学到什么?

应用谦卑对象模式,让视图代码保持精简且易于测试,并将所有决策推入表示层边界中可测试的表示器。 你通过在浏览器中直接运行的动手代码来练习 Clean Architecture & Design Patterns in Practice,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Clean Architecture & Design Patterns in Practice 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Clean Architecture & Design Patterns in Practice 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「谦卑对象与视图边界」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Clean Architecture & Design Patterns in Practice 课中编写并运行代码吗?

能。每节 Clean Architecture & Design Patterns in Practice 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 表示器与视图模型
  2. 适配 Web 框架
  3. 测试表示层
  4. 谦卑对象与视图边界
← 返回 Clean Architecture & Design Patterns in Practice