Structural Patterns
Patterns for composing classes and objects into larger structures.
Structural patterns describe how objects are wired together: wrapping, adapting, sharing and composing. The GoF catalogue contains seven: Adapter, Bridge, Composite, Decorator, Facade, Flyweight and Proxy. Several of them look alike in code — a class that holds a reference to another object — but differ in intent. The wrapper patterns comparison puts them side by side.
Articles
- AdapterConvert an interface you cannot change into the one your code expects. A runnable Java 21 example wraps a legacy payment SDK, translating units, status codes and exceptions — plus class versus object adapters, trade-offs and interview questions.
- BridgeSplit a class that varies in two independent dimensions into two hierarchies connected by composition, so they can evolve separately. A runnable Java 21 notification example (alerts and digests × email and SMS), Bridge versus Adapter and Strategy, trade-offs and interview questions.
- CompositeCompose objects into tree structures and treat single objects and groups uniformly. A runnable Java 21 product-bundle example with nested discounts, transparency versus safety, sealed interfaces, and where Composite appears in the JDK, Spring and UI frameworks.
- DecoratorAdd responsibilities to an object dynamically by wrapping it in objects with the same interface. A runnable Java 21 HTTP client stacked with logging, caching and retry decorators, why order matters, java.io as the classic example, Decorator versus inheritance and proxies.
- FacadeGive a complex subsystem one simple entry point. A runnable Java 21 checkout facade coordinating inventory, payment, shipping and notifications with compensation, the Principle of Least Knowledge, Facade versus application service and API gateway, trade-offs and interview questions.
- FlyweightSupport huge numbers of fine-grained objects by sharing their common, immutable state. Intrinsic versus extrinsic state, a flyweight factory, a runnable Java 21 map-marker example sharing 3 styles across 100,000 markers, and Flyweight in the JDK (Integer cache, String interning).
- ProxyControl access to an object through a stand-in with the same interface. Virtual, protection, caching and remote proxies in a runnable Java 21 example, JDK dynamic proxies, how Spring's @Transactional and lazy JPA associations rely on proxies, and Proxy versus Decorator.