Skromne obiekty i granica widoku
Zastosują Państwo wzorzec Humble Object, aby kod widoku był prosty i podatny na testowanie, przenosząc wszystkie decyzje do testowalnych prezenterów na granicy warstwy prezentacji.
Skromne obiekty i granica widoku to bezpłatna lekcja Clean Architecture & Design Patterns in Practice na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Clean Architecture & Design Patterns in Practice, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Clean Architecture & Design Patterns in Practice zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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.
Ucz się Clean Architecture & Design Patterns in Practice dzięki korepetycjom AI — za darmo
Pisz i uruchamiaj kod w przeglądarce, otrzymuj natychmiastową pomoc od korepetytora AI dostępnego 24/7 i kontynuuj naukę w sieci lub w aplikacji.
- Kursy
- 12
- Lekcje
- 48
Często zadawane pytania
Czy lekcja „Skromne obiekty i granica widoku” jest bezpłatna?
Tak — pełny tekst „Skromne obiekty i granica widoku” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Clean Architecture & Design Patterns in Practice, przejdź na CoddyKit PRO. Kurs Clean Architecture & Design Patterns in Practice zawiera 4 lekcji w sumie.
Co nauczysz się w „Skromne obiekty i granica widoku”?
Zastosują Państwo wzorzec Humble Object, aby kod widoku był prosty i podatny na testowanie, przenosząc wszystkie decyzje do testowalnych prezenterów na granicy warstwy prezentacji. Ćwiczysz Clean Architecture & Design Patterns in Practice z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Clean Architecture & Design Patterns in Practice?
Nie wymagamy żadnego doświadczenia. Clean Architecture & Design Patterns in Practice w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.
Ile czasu zajmuje lekcja „Skromne obiekty i granica widoku”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Clean Architecture & Design Patterns in Practice?
Tak. Każda lekcja Clean Architecture & Design Patterns in Practice zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Presenters i View Models
- Dostosowanie do frameworków webowych
- Testowanie warstwy prezentacji
- Skromne obiekty i granica widoku