Clean Architecture & Design Patterns in Practice · Pelajaran

Objek Sederhana dan Batas Tampilan

Terapkan pola Objek Sederhana untuk menjaga kode tampilan tetap tipis dan mudah diuji, dengan memindahkan semua keputusan ke penyaji yang dapat diuji pada batas presentasi.

Pelajaran 4 dari 413 langkah

Objek Sederhana dan Batas Tampilan adalah pelajaran Clean Architecture & Design Patterns in Practice gratis di CoddyKit. Ini adalah pelajaran 4 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Clean Architecture & Design Patterns in Practice, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Clean Architecture & Design Patterns in Practice mencakup 4 pelajaran total.

Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.

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.
Gratis untuk memulai

Belajar Clean Architecture & Design Patterns in Practice dengan tutor AI — gratis

Tulis dan jalankan kode asli di browser kamu, dapatkan bantuan instan dari tutor AI 24/7, dan lanjutkan di mana kamu tinggalkan di web atau aplikasi.

Kursus
12
Pelajaran
48

Pertanyaan yang Sering Diajukan

Apakah pelajaran “Objek Sederhana dan Batas Tampilan” gratis?

Ya — teks lengkap “Objek Sederhana dan Batas Tampilan” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Clean Architecture & Design Patterns in Practice, upgrade ke CoddyKit PRO. Kursus Clean Architecture & Design Patterns in Practice mencakup 4 pelajaran total.

Apa yang akan aku pelajari di “Objek Sederhana dan Batas Tampilan”?

Terapkan pola Objek Sederhana untuk menjaga kode tampilan tetap tipis dan mudah diuji, dengan memindahkan semua keputusan ke penyaji yang dapat diuji pada batas presentasi. Kamu berlatih Clean Architecture & Design Patterns in Practice dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.

Apakah aku perlu pengalaman untuk memulai Clean Architecture & Design Patterns in Practice?

Tidak diperlukan pengalaman sebelumnya. Clean Architecture & Design Patterns in Practice di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 4 dari 4.

Berapa lama pelajaran “Objek Sederhana dan Batas Tampilan” memakan waktu?

Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.

Bisakah aku menulis dan menjalankan kode dalam pelajaran Clean Architecture & Design Patterns in Practice ini?

Ya. Setiap pelajaran Clean Architecture & Design Patterns in Practice menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.

Semua pelajaran dalam kursus ini

  1. Penyaji dan Model Tampilan
  2. Beradaptasi dengan Kerangka Kerja Web
  3. Menguji Lapisan Presentasi
  4. Objek Sederhana dan Batas Tampilan
← Kembali ke Clean Architecture & Design Patterns in Practice