Designing object-oriented software is hard, and it is really difficult to make it right the first time. A design pattern is a general solution to a commonly occurring problem in software development. They provide high-level guidelines while leaving the implementation for each developer to interpret.
In 1994, four authors Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides (Gang of Four - GoF) published a book titled Design Patterns - Elements of Reusable Object-Oriented Software, in which they applied the concept of design patterns to programming. These design patterns make it easier to reuse successful designs and architecture.
In general, a design pattern has four essential elements:
- Summary: includes pattern name and description of the pattern.
- Problem: describes the problem, the context behind it, and when to apply the pattern.
- Solution: provides an abstract description to solve the problem and some code examples to make it easier to grasp the idea behind the pattern.
- Consequences: discusses the results and trade-offs of applying the pattern. The consequences often concern space, time trade-offs, code complexity and readability.
Classification of patterns
The GoF classifies design patterns by two criteria: purpose and scope.
The purpose reflects what a pattern does:
- Creational patterns deal with object creation mechanisms that increase flexibility and reuse of existing code.
- Structural patterns concern the composition of classes or objects. They form larger structures from individual parts to obtain new functionalities.
- Behavioral patterns characterize how classes or objects interact and distribute responsibility.
The scope specifies whether the pattern applies primarily to classes or objects. Class patterns deal with relationships between classes and their subclasses through inheritance, which means they are fixed at compile-time. Object patterns deal with object relationships, which can be changed at run-time and are more dynamic.
References
Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides. Design Patterns - Elements of Reusable Object-Oriented Software