Structural
How classes and objects compose into larger, more flexible structures.
Structural patterns explain how to assemble objects and classes into larger structures while maintaining flexibility and efficiency. They solve problems of composition: adapting incompatible interfaces, adding behavior without inheritance, separating abstraction from implementation, or representing part-whole hierarchies.
#The Idea
While creational patterns deal with who builds objects and behavioral patterns with how they communicate, structural patterns answer a simpler but crucial question: how do these objects connect to each other without ending up in rigid coupling?
#When to use them
- You need to integrate legacy code with a modern API without rewriting everything (
Adapter). - You want to add optional functionality without a subclass explosion (
Decorator). - You handle recursive hierarchies (folders, trees, menus) (
Composite). - You want to hide a complex subsystem behind a simple API (
Facade). - You need to control access to a remote, expensive, or dangerous object (
Proxy).
#When NOT to use them
- If "incompatibility" can be solved by renaming a method, do that instead.
- Misapplied Decorator leads to stacks of wrappers impossible to debug.
- Flyweight is an optimization: measure before applying it.
#In this chapter
Adapter, Bridge, Composite, Decorator, Facade, Flyweight, and Proxy —
the seven GoF classics, each with its C# example and the real-world tradeoffs
seen in production.
- Adapter — Allows objects with incompatible interfaces to collaborate.
- Bridge — Decouples an abstraction from its implementation so that both can evolve independently.
- Composite — Composes objects into tree structures and treats them uniformly as if they were individual objects.
- Decorator — Adds responsibilities to an object at runtime by wrapping it in successive layers.
- Facade — Provides a simplified interface to a complex subsystem.
- Flyweight — Share intrinsic state among many objects to support large quantities without exhausting memory.
- Proxy — Substitutes another object to control access to it (lazy load, security, caching, remoting).