热门面试题与答案和在线测试
面向面试准备、在线测试、教程与实战练习的学习平台

通过聚焦学习路径、模拟测试和面试实战内容持续提升技能。

WithoutBook 将分主题面试题、在线练习测试、教程和对比指南整合到一个响应式学习空间中。

Chapter 7

Behavioral Patterns Part 2: Command, Observer, Mediator, and Chain of Responsibility

Model requests as objects, broadcast domain events, coordinate object collaboration, and pass requests through layered handlers.

Inside this chapter

  1. Command Pattern
  2. Observer Pattern
  3. Mediator Pattern
  4. Chain of Responsibility
  5. Real-World Usage Snapshot

Series navigation

Study the chapters in order for the clearest path from first design principles to advanced Java architecture, framework usage, and interview-level pattern mastery. Use the navigation at the bottom of the page to move through the full tutorial smoothly.

Tutorial Home

Chapter 7

Command Pattern

Command wraps a request as an object. This enables queuing, logging, retries, undo support, scheduling, and decoupled execution. Batch jobs, UI actions, workflow tasks, and job dispatchers often reflect command thinking.

interface Command {
    void execute();
}

class CreateInvoiceCommand implements Command {
    public void execute() {
        System.out.println("Invoice created");
    }
}
Chapter 7

Observer Pattern

Observer lets one object notify interested listeners when something happens. It is useful for event-driven design, UI updates, audit hooks, metrics, and integration triggers. In modern Java systems, domain events and messaging often play observer-like roles.

Chapter 7

Mediator Pattern

Mediator centralizes communication between collaborating objects. Instead of each component calling many others directly, they communicate through a mediator. This reduces direct coupling and is valuable in UI controllers, workflow coordinators, and orchestration logic.

Chapter 7

Chain of Responsibility

Chain of Responsibility passes a request through multiple handlers until one processes it or the chain completes. Servlet filters, Spring security chains, validation pipelines, and request processing middleware are familiar examples.

abstract class Handler {
    private Handler next;

    public Handler linkWith(Handler next) {
        this.next = next;
        return next;
    }

    public void handle(String request) {
        process(request);
        if (next != null) {
            next.handle(request);
        }
    }

    protected abstract void process(String request);
}
Chapter 7

Real-World Usage Snapshot

These patterns are the backbone of extensible enterprise systems. Security filters, notification pipelines, event publishing, scheduled jobs, audit handlers, and request processing frameworks all rely on variations of these ideas.

版权所有 © 2026,WithoutBook。