Gangs of Four (GoF) Design Patterns
Overview​
- Creational
- Structural
- Behavioral
Deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. These patterns help in making a system independent of how its objects are created, composed, and represented.
Name | Visualization | Definition | Example |
---|---|---|---|
Singleton |
| database connection manager in a web application | |
Factory |
| different factories produce different types of vehicles (car, truck) | |
Abstract Factory |
| GUI toolkit providing different look and feel for different operating systems | |
Builder |
| construction of complex objects like HTML documents | |
Prototype |
| creating multiple instances of a document (cloning objects) with different content but similar structure |
Factory vs Abstract Factory
Aspect | Factory Method | Abstract Factory |
---|---|---|
Product Type | creates one product only | creates families of related or dependent products |
Exposure | exposes a method to create the object | exposes a family of related objects |
Construction Hiding | hides the construction of a single object | hides the construction of a family of related objects. Abstract factories are usually implemented using (a set of) factory methods |
Creation Responsibility | uses inheritance and relies on a derived class or subclass to create an object | uses composition to delegate the responsibility of creating an object to another class |
Ideal Usage | best for cases where the client doesn't know what concrete classes it will be required to create at runtime | best utilized when your system has to create multiple families of products or you want to provide a library of products without exposing the implementation details |
Focus on organizing different classes and objects to form larger structures and provide new functionality. They help ensure that if one part of a system changes, the entire system doesn't need to do so. These patterns generally identify simple ways to realize relationships between entities.
Name | Visualization | Definition | Example |
---|---|---|---|
Adapter |
| adapting an old version of an API to a new version | |
Bridge |
| Different rendering engines (DirectX, OpenGL) for graphical shapes in a drawing application | |
Composite |
| representing a hierarchy of files and directories in a file system | |
Decorator |
| adding additional features to a text editor, such as spell checking or formatting | |
Facade |
| providing a simplified interface to a complex subsystem like a software library | |
Flyweight |
| text editing software where frequently used characters (space, period) are stored as flyweight objects to save memory | |
Proxy |
| proxy server that acts as an intermediary between clients and a web server, handling requests and caching responses |
Concentrate on algorithms and the assignment of responsibilities between objects. They describe not just patterns of objects or classes but also the patterns of communication between them. These patterns help in defining how objects interact and communicate with each other.
Name | Visualization | Definition | Example |
---|---|---|---|
Chain of Responsibility |
| handling support tickets in a customer service system where tickets are passed down a chain of support agents until one can handle it | |
Command |
| undo and redo operations in a text editor | |
Interpreter |
| implementing a query language interpreter for a database system to parse and execute user queries | |
Iterator |
| iterating through a collection of objects in a database system | |
Mediator |
| chat application where users communicate through a central chat room (mediator) rather than directly with each other | |
Memento |
| saving and restoring the state of a text editor (cursor position, selected text) to implement undo/redo functionality | |
Observer |
| updating the GUI when the contents of a text editor change | |
State |
| switching between different views of a text editor (normal, bold, italic). Deals with what (state or type) an object is (in). It encapsulates state-dependent behavior | |
Strategy |
| switching between different sorting algorithms (quicksort, mergesort, heapsort). Deals with how an object performs a certain task. It encapsulates an algorithm | |
Template Method |
| creating a new type of text editor (text editor with spell checking) | |
Visitor |
| compiler that traverses an abstract syntax tree representing code and performs different operations (type checking, code generation) based on the node being visited |
State vs Strategy
Aspect | State | Strategy |
---|---|---|
Storage of Context Object Reference | store a reference to the context object that contains them | do not store |
Replacement Ability | are allowed to replace themselves, changing the state of the context object | cannot replace themselves or change the state of the context object |
Creation | created by the context object itself | passed to the context object as parameters |
Responsibility | provide the underlying implementation for most actions of the context object | handle specific tasks but do not dictate the behavior of the context object |
Focus | deals with what (state or type) an object is (in). It encapsulates state-dependent behavior | deals with how an object performs a certain task. It encapsulates an algorithm |