Card 03/ 08
Creational, Structural and Behavioural Design Patterns
Creational patterns make objects, structural ones join them, behavioural ones share the work.
The Gang of Four sort their 23 patterns into three families by the kind of problem each one solves. Creational patterns control how objects are created. Structural patterns combine objects and classes into larger structures. Behavioural patterns divide work and communication between objects. This card lays out the whole catalogue in those families, and gives the one question that places a problem in each.
Knowing the family narrows the search. Faced with a design problem, you ask what kind of problem it is before you ask which pattern fits, and that question alone leaves five, seven or eleven candidates instead of 23.
| Family | The question it answers | Patterns |
|---|---|---|
| Creational (5) | Who decides which class is created, and how is the object put together? | Factory Method, Abstract Factory, Builder, Prototype, Singleton |
| Structural (7) | How do existing objects fit together into something larger, without rewriting them? | Adapter, Bridge, Composite, Decorator, Facade, Flyweight, Proxy |
| Behavioural (11) | Which object does which part of the work, and how do they tell each other? | Chain of Responsibility, Command, Interpreter, Iterator, Mediator, Memento, Observer, State, Strategy, Template Method, Visitor |
One pattern from each family shows its question at work. Factory Method lets a subclass choose the class of the object a method returns. Adapter wraps a class so it matches the interface its caller already expects. Observer lets many objects react when one object changes.
The most common sorting mistake is to read "structural" as "about data" and "behavioural" as "about anything that changes what happens". An adapter changes what a call does, yet it is structural, because what it solves is a mismatch between two shapes. Ask what the problem is about: making something, fitting things together, or dividing responsibility.
The book also sorts the patterns a second way, by whether the relationship is fixed between classes at compile time or set up between objects at run time. Most patterns are object patterns, and this subject uses the three families alone.
← → move through sections and cards