Humble Objectとビューの境界
Humble Objectパターンを適用してビューコードを薄くテスト可能に保ち、プレゼンテーション境界にあるテスト可能なプレゼンターへすべての判断を移します。
「Humble Objectとビューの境界」はCoddyKit上の無料Clean Architecture & Design Patterns in Practiceレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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 — 無料
ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。
- コース
- 12
- レッスン
- 48
よくある質問
「Humble Objectとビューの境界」レッスンは無料ですか?
はい。「Humble Objectとビューの境界」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Clean Architecture & Design Patterns in Practiceコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Clean Architecture & Design Patterns in Practiceコースには全4レッスンが含まれています。
「Humble Objectとビューの境界」で何を学びますか?
Humble Objectパターンを適用してビューコードを薄くテスト可能に保ち、プレゼンテーション境界にあるテスト可能なプレゼンターへすべての判断を移します。 ブラウザで直接実行するハンズオンコードでClean Architecture & Design Patterns in Practiceを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Clean Architecture & Design Patterns in Practiceを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのClean Architecture & Design Patterns in Practiceは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「Humble Objectとビューの境界」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このClean Architecture & Design Patterns in Practiceレッスンでコードを書いて実行できますか?
はい。すべてのClean Architecture & Design Patterns in Practiceレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- PresentersとView Models
- Webフレームワークへの適応
- Presentation層のテスト
- Humble Objectとビューの境界