What Is A Design Pattern?
"Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice" -- Christopher Alexander
A design pattern is a description or template for how to solve a problem that can be used in many different situations. It is a general repeatable solution to a commonly occurring problem in software design.
The Gang of Four describes four essential elements:
- Pattern name. A word or two used to describe a design problem, its solutions and consequences.
- Problem. When to apply the pattern. Explains the problem and its context.
- Solution. The elements that make up the design, their relationships, responsibilities, and collaborations.
- Consequences. Results and trade-offs of applying the pattern.
The Gang Of Four's Design Patterns.
The Gang of Four describes 23 classic design patterns, divided into 3 categories: creational, structural, and behavioral.
Creational Design Patterns
The creational design patterns are all about class instantiation, being divided into class-creation patterns and object-creation patterns.
- Abstract Factory.
- Builder.
- Factory Method.
- Prototype.
- Singleton.
Structural Design Patterns
The structural design patterns are all about class and object composition, using inheritance to compose interfaces.
- Adapter.
- Bridge.
- Composite.
- Decorator.
- Facade.
- Flyweight.
- Proxy.
Behavioral Design Patterns
The behavioral design patterns are all about classes' objects communication. It is concerned with communication between objects.
- Chain of Responsibility.
- Command.
- Interpreter.
- Iterator.
- Mediator.
- Memento.
- Observer.
- State.
- Strategy.
- Template Method.
- Visitor.
The Design Patterns That Matter.
Although it is extremely advised to learn, understand and apply the patterns presented above, not all of them are entirely necessary to work on a N-Layer Architecture. Moreover, some modern patterns are not described by the Gang of Four's book.
Design patterns should be applied as needed, varying from application to application.