Design Patterns โ Structural Patterns
Master Structural Design Patterns โ Adapter, Decorator, Facade, Proxy, and Composite โ that define how classes and objects are composed into larger, flexible structures.
โStructural design patterns address how classes and objects are composed into larger, more complex structures. While Creational patterns control object birth, Structural patterns control how those objects are assembled and connected. The common challenge they solve: how do you make classes work together when they weren't designed to? Or how do you add new behavior to existing objects without modifying them? The seven GoF Structural Patterns are Adapter, Bridge, Composite, Decorator, Facade, Flyweight, and Proxy. We'll focus on the five most commonly encountered in practice. These patterns appear throughout major frameworks: Java's Arrays.asList() uses Adapter, Python's property decorators implement the Decorator pattern, Spring AOP uses Proxy, file system structures use Composite. Recognizing these patterns in codebases you read is as valuable as applying them in codebases you write.โ
โThe Adapter pattern is the translator of the design pattern world. When two interfaces are incompatible but need to work together, you create an Adapter that wraps one and exposes the other's interface. The real-world analogy is perfect: an electrical plug adapter allows a European plug to fit into an American socket. Neither plug nor socket changes โ the adapter bridges them. In software: your modern codebase expects a PaymentProcessor interface with a processPayment(PaymentRequest request) method. But you're integrating a legacy payment library with a completely different API: charge(double amount, String currency, String cardToken). You write a LegacyPaymentAdapter: it implements PaymentProcessor, and internally it translates the processPayment call into the legacy API's format. Your modern code never knows a legacy library exists. The Object Adapter composes the adaptee via a field โ this is preferred because it doesn't require inheritance and keeps coupling loose. The Adapter pattern is indispensable when integrating third-party libraries, consuming external APIs, or working with legacy code you can't modify.โ
โThe Decorator pattern attaches additional responsibilities to objects dynamically, without subclassing. Imagine a coffee ordering system. You have a base Coffee class. Without Decorator, to support all combinations โ coffee with milk, coffee with sugar, coffee with milk and sugar, coffee with whip โ you'd need a subclass for every combination. With three add-ons, that's already 8 subclasses. With 10 add-ons, it's 1024. Decorator solves this by wrapping: MilkDecorator wraps a Coffee and adds milk cost and description. SugarDecorator wraps any Coffee (including an already-decorated one) and adds sugar. WhipDecorator adds whip. Each decorator implements the same interface as the base component, delegates to the wrapped object, and adds behavior before or after. You can stack any combination at runtime with zero new subclasses. This pattern is everywhere: Java's I/O streams are decorators โ BufferedReader wraps FileReader wraps InputStream. Python's @decorator syntax directly implements this pattern. Logging, caching, and retry middleware in modern web frameworks are all decorators.โ
โThe Facade pattern provides a simplified, unified interface to a complex subsystem. The word facade comes from architecture โ it's the front face of a building that presents a clean, organized appearance while hiding the complex structural engineering behind it. Software analogy: a home theater system. To watch a movie you must turn on the projector, set it to HDMI input, turn on the amplifier, set volume to 11, turn on the Blu-ray player, dim the lights, and start playback. That's six operations across six different objects. A HomeTheaterFacade exposes one method: watchMovie(). Inside, it orchestrates all six operations in the correct order. The client just calls watchMovie() and movie night begins. Important: the Facade doesn't hide the subsystem from advanced users โ they can still access ProjectorController and AmplifierController directly. The Facade is a convenience layer for the common case. Real-world facades: REST APIs are facades over complex backend systems. Cloud SDKs are facades over raw HTTP calls to cloud services. Service facade classes in layered architectures are the entry points from controllers into the business logic layer.โ
โProxy provides a surrogate or placeholder for another object to control access to it. There are four common types. Virtual Proxy implements lazy loading: the full object (say, a high-resolution image) is not loaded until first needed. The proxy holds a placeholder. Protection Proxy adds access control: before delegating to the real object, it checks whether the caller has permission. Remote Proxy handles network transparency: calling methods on a remote object feels like calling local methods โ the proxy handles serialization and network communication. Logging Proxy adds an audit trail without touching the real object. Composite treats individual objects and compositions uniformly. The classic example is a file system: File is a leaf node. Directory is a composite node โ it contains a list of FileSystemNodes, each of which is either a File or another Directory. Both File and Directory implement the FileSystemNode interface with methods like getSize() and list(). When you call getSize() on a Directory, it recursively calls getSize() on all its children. The calling code doesn't need to know whether it has a File or a Directory โ it treats them identically. Composite appears anywhere you have recursive tree structures: UI component hierarchies, organizational charts, menu systems, mathematical expression trees.โ
โHere's your structural patterns summary. Adapter is your integration tool โ when you need incompatible interfaces to work together, Adapter translates between them without modifying either side. Decorator is your extension tool โ when you need to add behavior to objects without modifying their class, stack decorators for unlimited combinations. Facade is your simplification tool โ when a subsystem is complex, provide a clean facade for the common use cases while keeping the subsystem accessible for advanced scenarios. Proxy is your control tool โ use it for lazy loading expensive resources, adding access control, making remote calls transparent, or adding logging around existing objects. Composite is your tree tool โ whenever you have hierarchical structures where individual elements and containers need to be treated uniformly, Composite is the answer. These five patterns will appear repeatedly throughout your career in framework code, in library APIs, and in well-designed systems. Recognizing them is half the battle.โ
- 1Build an Adapter to integrate a legacy payment API with a modern interface
- 2Implement a Decorator to add logging and caching to any service
- 3Create a Facade for a complex third-party API
- 4Build a Proxy for lazy-loading and access control