Structural Patterns Deep Dive
Master Adapter, Decorator, Facade, Proxy, and Composite patterns for object composition.
What you'll learn
- Adapter: bridge incompatible interfaces
- Decorator: add behavior without subclassing
- Facade: simplify complex subsystems
- Composite: treat individual and composite uniformly
Concept
Structural Patterns Deep Dive
Structural patterns deal with how classes and objects are composed to form larger structures. They ensure that if one part of a system changes, the entire system does not need to change with it.
Adapter
Converts one interface to another. Example: you have a European plug and a US socket - an adapter makes them work together. In code, an Adapter wraps an object with an incompatible interface and exposes the expected interface. This is essential when integrating third-party libraries or legacy code.
Decorator
Dynamically adds behavior to an object without subclassing. Each decorator wraps an object and delegates to it, adding its own behavior before or after. Example: a Coffee object, decorated with Milk, then Sugar, then WhippedCream - each adds cost and description. This is more flexible than a combinatorial explosion of subclasses.
Facade
Provides a simplified interface to a complex subsystem. Example: a Compiler facade exposes compile(source) while internally orchestrating lexer, parser, semantic analyzer, optimizer, and code generator. The client does not know about these subsystems.
Proxy
A placeholder that controls access to another object. Types: remote proxy, virtual proxy (lazy-loads), protection proxy (checks permissions), smart proxy (adds logging/caching). JavaScript Proxy object is a language-level implementation.
Composite
Treats individual objects and compositions uniformly. A File and a Directory both implement a Component interface with getSize(). A Directory getSize() sums its children.
Example
// Decorator: add behavior dynamically
class Coffee { cost() { return 5; } description() { return 'Coffee'; } }
class MilkDecorator { constructor(c) { this.c = c; } cost() { return this.c.cost() + 1; } description() { return this.c.description() + ', Milk'; } }
class SugarDecorator { constructor(c) { this.c = c; } cost() { return this.c.cost() + 0.5; } description() { return this.c.description() + ', Sugar'; } }
let coffee = new Coffee();
coffee = new MilkDecorator(coffee);
coffee = new SugarDecorator(coffee);
console.log(coffee.description(), '$' + coffee.cost());
// Facade: simplify a complex subsystem
class Compiler {
compile(source) {
const tokens = source.split('');
const ast = {type: 'ast', tokens};
return 'compiled:' + JSON.stringify(ast).length + 'bytes';
}
}
console.log(new Compiler().compile('let x = 1'));
Decorators wrap the coffee object, each adding cost and description. The Compiler facade hides the lex/parse/optimize/generate pipeline behind a single compile() method.
Try it
- Code Minifier
Minification is a transformation pipeline - a structural pattern application.
Common mistakes
The mistake
Creating deep decorator chains that are hard to debug.
The fix
Each decorator adds a layer. Deep chains make stack traces confusing and debugging hard. Limit chain depth.
The mistake
Using a Facade that hides too much, making important features inaccessible.
The fix
A Facade simplifies but should not prevent access to the underlying subsystem when needed. Provide escape hatches for advanced use cases.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.