Structural Design Patterns
The Structural Design Patterns deal with the relations between components and objects.
Types
- structural class patterns - use inheritance
- structural objects patterns - use composition
Purpose
- Best way to compose objects and to form larger structures
Patterns
There are 7 structural patterns introduced by GoF. Later we will add some more useful patterns to this list.
Design Pattern | Main Purpose |
---|---|
Adapter (GoF) | Designed to make two incompatible types compatible without changing their existing code |
Bridge (GoF) | Designed to separate object abstraction from the implementation, so that you can change implementation without affecting the rest of the code. “The bridge approach” - when you have two different hierarchies. |
Composite (GoF) | Designed to build a class hierarchy from primitive and composite objects with similar behavior, that may represent complex structures |
Decorator (GoF) | Designed to add/ remove independent responsibilities to objects dynamically (even at runtime), without changing their implementation |
Facade (GoF) | Designed to hide an entire subsystem behind a facade and present it is as a single object/ interface |
Flyweight (GoF) | Designed to share stateless objects (to reuse instances) for better efficiency and consistency |
Proxy (GoF) | Designed to provide a level of indirection to object members or add additional logic (i.e. to control access or to provide a wrapper implementation for better performance) |
Private Class Data | Designed to protect the object data (to restrict write access) even from it’s own methods. |
Consider applying structural patterns when:
- Need to adapt incompatible interfaces (Adapter)
- You have to deal with two different hierarchies or to decouple abstraction from implementation (Bridge)
- You have tree-like structure with same general behavior (Composite)
- You need to add/ remove behavior to specific instances (not the class) at runtime (Decorator)
- You need to simplify usage and hide complexity (Facade)
- You have objects that share same data, many instances with the same values, and you need to lower application memory usage (Flyweight)
- You have to protect some object by controlling or managing access to it; to cache or log requests/ responses (Proxy)
- You have to implement an immutable object and restrict even his own methods from modifying it’s data (Private Class Data)