Tomcat’s Use of Facade, Observer, Command & Chain of Responsibility Patterns
This article explains the principles of the Facade, Observer, Command, and Chain of Responsibility design patterns, illustrates how Tomcat implements each with class diagrams and code snippets, and shows how these patterns help organize subsystems, manage events, delegate actions, and streamline request processing in a Java server.
Facade Design Pattern
The Facade pattern is used throughout Tomcat to simplify interactions between complex subsystems, such as wrapping Request/Response objects, Standard Wrapper to ServletConfig, and ApplicationContext to ServletContext.
Principle
It provides a unified interface (the façade) that hides internal complexities, much like a diplomatic ministry, allowing clients to access only the data they need without exposing subsystem internals.
Tomcat Example
In Tomcat, the HttpRequestFacade class wraps the HttpRequest interface, delegating all calls to the underlying request while keeping the actual request object private or protected.
Observer Design Pattern
Also known as the publish‑subscribe pattern, it enables objects (observers) to be notified when a subject changes.
Principle
The pattern defines three roles: Subject (manages observers), ConcreteSubject (notifies observers on state change), and Observer (receives notifications).
Subject – abstract subject managing observer references.
ConcreteSubject – concrete implementation that triggers events.
Observer – defines the reaction to events.
Tomcat Example
Tomcat’s Lifecycle mechanism uses the Observer pattern for component lifecycle events.
The LifecycleSupport class fires events to registered listeners:
public void fireLifecycleEvent(String type, Object data) {
LifecycleEvent event = new LifecycleEvent(lifecycle, type, data);
LifecycleListener interested[] = null;
synchronized (listeners) {
interested = (LifecycleListener[]) listeners.clone();
}
for (int i = 0; i < interested.length; i++)
interested[i].lifecycleEvent(event);
}When a container starts, it notifies observers:
public void start() throws LifecycleException {
lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, null);
lifecycle.fireLifecycleEvent(START_EVENT, null);
started = true;
synchronized (services) {
for (int i = 0; i < services.length; i++) {
if (services[i] instanceof Lifecycle)
((Lifecycle) services[i]).start();
}
}
lifecycle.fireLifecycleEvent(AFTER_START_EVENT, null);
}Command Design Pattern
The Command pattern separates the object that invokes an operation from the one that knows how to perform it.
Principle
Client – creates a command and selects a receiver.
Command – defines an abstract execution method.
ConcreteCommand – implements the command and calls the receiver.
Invoker – triggers the command.
Receiver – carries out the actual work.
Tomcat Example
In Tomcat, the Connector (invoker) creates an HttpProcessor (command) which is executed by a ContainerBase (receiver). The server builds the command chain and the container processes the request, supporting different protocols such as HTTP/1.0 and HTTP/1.1.
Chain of Responsibility Pattern
Tomcat’s container hierarchy (Engine → Host → Context → Wrapper) forms a classic chain of responsibility, passing requests along until one component handles them.
Principle
Handler – abstract handler defining a request‑processing method.
ConcreteHandler – either processes the request or forwards it to the next handler.
Tomcat Example
The container chain is enhanced by Pipeline and Valve interfaces, which allow additional processing (e.g., logging, authentication) at each step.
Each container includes a standard valve that guarantees the request continues down the chain, making the pattern a valuable reference for building extensible processing pipelines.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITFLY8 Architecture Home
ITFLY8 Architecture Home - focused on architecture knowledge sharing and exchange, covering project management and product design. Includes large-scale distributed website architecture (high performance, high availability, caching, message queues...), design patterns, architecture patterns, big data, project management (SCRUM, PMP, Prince2), product design, and more.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
