Design pattern is a general repeatable solution to a commonly occurring problem in software design. A design pattern isn’t a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations.
Why learn design patterns?
- Design pattern are a toolkit of tried and tested solutions to common problems in software design.
- Design patterns define a common language that you and your teammates can use to communicate more efficiently.
Criticism of design patterns
- “If all you have is a hammer, everything looks like a nail”. This is the the problem that haunts many beginners who have just familiarized themselves with design patterns. Having learned about patterns, they try to apply them everywhere, even in situations where simple code would do just fine.
- Most design patterns lacks formal foundations.
Classification of patterns
Creational Patterns
Creational patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.
- Singleton: A class of which only a single instance can exists.
- Abstract Factory: Create an instance of several families of classes
- Builder: Separates object construction from its representation.
- Factory Method: Creates an instance of several derived classes.
- Prototype: A fully initialized instance to be copied or cloned.
Structural Patterns
These patterns explain how to assemble objects and classes into large structures while keeping these structures flexible and efficient.
- Adapter: Adapter is a structural design pattern that allows objects with incompatible interfaces to collaborate.
- Bridge: Bridge lets you split a large class or a set of closely related classes into two separate hierarchies: abstraction and implementation, which can be developed independently of each other.
- Composite: Composite lets you compose objects into tree structures and then work with these structures as if they were individual objects.
- Decorator: Decorator lets you attach new behaviors to objects by placing these objects inside special wrapper objects that contain the behaviors.
- Facade: Facade is a structural design that provides a simplified interface to a library, a framework, or any other complex set of classes.
Behavioral Patterns
These patterns are concerned with algorithms and the assignment of responsibilities between objects.
- Chain of Responsibility: Chain of Responsibility is a behavioral design pattern that lets you pass requests along a chain of handlers.
- Iterator: Iterator let you traverse elements of a collection without exposing its underlying representation (list, stack, tree, etc.)
- Memento: Memento lets you save and restore the previous state of an object without revealing the detail of its implementation.
