What’s a design pattern?
There could be some problems with the software design. Design patterns are solutions to these problems and they are like blueprints to solve a commonly occurring design problem. They can also be defined as templates that can be used in varied situations to solve problems in software design.
How do algorithms differ from patterns?
Algorithms and patterns offer solutions to problems and at the same time, they differ.
Algorithms have a clear set of actions to achieve a goal while the pattern is a high-level description of a solution.
Why design patterns?
1. Patterns are technically proven solutions that can be used to rectify problems in software development. The valid approach that it provides is a reflection of the developer’s experiences and deep insights.
2. Patterns can be reutilized. They can be tailored to meet our requirements and this is what makes them unique.
3. Patterns are coherent. The solution that it presents has a pre-defined structure and vocabulary.
What does a design pattern have?
The pattern description usually consists of the following areas:
This part of the pattern gives a brief insight into the problem and solution.
It provides a more detailed description of the problem and the solution.
The structure of the classes indicates each part of the pattern and how they are related to each other.
- Code example
It’s among the programming languages that help to understand the idea behind the pattern.
There are other design catalogs that contain details on the applicability of the pattern, and implementation steps.
An exact solution is not provided by the Patterns. They only provide a solution scheme and support the software designers.
How did patterns develop?
Patterns were first described by Christopher Alexander in the book titled A Pattern Language: Towns, Buildings, Construction. A ‘language’ for designing the urban environment has been described in the book and this is where the patterns were first used.
Later the same idea of Patterns was applied by four authors to Programming. Erich Gamma, John Vlissides, Ralph Johnson, and Richard Helm published a book titled Design Patterns: Elements of Reusable Object-Oriented Software in 1994. The book was shortly known as “Gang of four” and again it was shortened as “ GoF” book. The book introduced 23 patterns that could solve varied problems associated with object-oriented design.
Today we have many design patterns in the object-oriented design as well as outside the object-oriented design.
Classification of Patterns
There are mainly 3 types of patterns:
- Creational Pattern
- Structural Pattern
- Behavioral Pattern
They provide varied mechanisms to create objects. To solve the design problems and added complexity to object creation, creational design patterns are used.
Types of Creational patterns
- Factory Method
The Factory Method is a type of creational design pattern. It provides an interface for creating objects in a superclass. At the same time, subclasses can alter the object type in the superclass.
- Abstract Factory Method
This type of pattern helps to generate families of objects that are related and their concrete class is not specified.
This type of pattern helps to create objects in a step-by-step process. Using the same construction code, we are able to generate varied representations and types of an object.
In this type of pattern, existing objects can be copied without making the code dependent on classes.
It helps the user to ensure that the class has only one instance and at the same time, it provides a global access point to the same instance.
- Structural Pattern
Structural Patterns show how to assemble objects and classes into large structures.
They are again subdivided into
It’s a type of pattern where objects with incompatible interfaces are allowed to collaborate.
It’s a type of design pattern wherein the object’s interface is separated from its implementation. In other words, it can be defined as a pattern that allows a large class or a set of closely related classes to be split into two hierarchies ie abstraction and implementation which are then developed independently of each other.
It helps to compose the objects into tree structures. Then one needs to work with these tree structures considering them as individual objects.
It allows attaching new behaviors to objects and it is done by placing those objects inside the special wrapper objects that have the behaviors.
It gives a simplified interface to a library, framework, or a set of complex classes. In other words, an entire subsystem is represented by a single class.
It helps the user to fit more objects into the available space of RAM. In other words, flyweight can be defined as a fine-grained instance that can be used for efficient sharing.
It allows the user to provide a substitute for another object. Proxy also controls access to the original object and allows the user to perform something either before or after the request gets through to the original object.
- Behavioral Pattern
They are another type of design pattern that deals with algorithms and the assignment of responsibilities between objects.
They are again subdivided as :
- Chain of responsibility
It allows the user to pass requests along a chain of handlers. When a request is received, each handler decides either to process or pass the request to the next handler in the chain.
In this type, the request is turned into a stand-alone object which has all the information regarding the request. This kind of transformation allows passing requests as to method arguments, delay or even queue a request’s execution.
It allows traversing elements of a collection without exposing its underlying representation( list, tree, etc).
It helps to decrease the chaotic dependencies between objects. This type of pattern restricts the direct communication between objects and they are forced to collaborate only through a mediator object.
It helps to save as well as restore the previous state of an object. This is performed without revealing the details of its implementation.
It allows for a subscription mechanism that will notify multiple objects about any events that happen to the object which is under observation.
An object is allowed to alter its behavior when its internal state changes.
It helps the user to define a family of algorithms, allows to put each of them into a separate class, and thus makes their objects interchangeable.
- Template Method
It defines the skeleton of an algorithm in the superclass. At the same time superclasses are allowed to override specific steps of the algorithm while keeping the structure unchanged.
It’s a type of pattern wherein the algorithms are separated from the objects on which they operate.
Although patterns are technically proven solutions, coherent and helps to solve the problems in the software design it has got some criticisms such as
- Patterns try to unify the approaches that are widely used.
- Patterns are used unnecessarily used where simple codes can work.
Thanks For Reading!
POST YOUR COMMENTS
Sign up for our newsletter the monthly updates
How about a lil' game of fill in the blanks?
We love working alongside ambitious brands and people