Design Patterns - Definition, Motivation & Classification
Overview
"Design patterns are conventional answers to common challenges in software design."
They function similarly to pre-made blueprints that you can modify to tackle a reoccurring design problem in your code. You cannot just discover a pattern and copy it into your program like you can with ready-made functions or libraries. The pattern is not a single code but a general notion for dealing with a given situation. Patterns are conventional solutions to common challenges in object-oriented design. When a solution is used repeatedly in several projects, someone ultimately gives it a name and discusses it in depth. That is essentially how a pattern is discovered.
What are Design Patterns in Software Engineering?
A Software Design Pattern is a general, reusable solution to a commonly occurring problem within a given context in software design.
They are templates to solve common software engineering problems, representing some of the finest practices experienced object-oriented software engineers utilize. A design pattern systematically describes and explains a general design that handles a recurring design challenge in object-oriented systems. It discusses the problem, the remedy when to use it, and the repercussions. It also provides implementation guidance and examples.
How are Patterns Described?
A pattern in object-oriented programming might include the description of specific objects and object classes to be utilized, their properties and dependencies, and the overall strategy for solving the problem. Most patterns are described formally so they can be replicated in various circumstances. The following are the sections that are typically included in a pattern description:
- Intent:
The pattern's intent briefly communicates both the problem and the solution. - Motivation:
It describes the challenge and the solution made possible by the pattern. - Structure:
The structure of classes demonstrates each component of the pattern and how they are related. - Code:
A code example in one of the most popular programming languages makes understanding the pattern's concept easier.
Patterns vs Algorithms
Patterns and algorithms are sometimes misconstrued since both notions represent typical solutions to well-known situations. A pattern is a more high-level description of a solution than an algorithm, which always describes a defined set of activities that can achieve some goal. The code for the same pattern in two separate programs may differ.
A cooking recipe is similar to an algorithm because both involve explicit stages to attain a goal. On the other hand, a pattern is more like a blueprint: you can see the outcome and its features, but the specific order of implementation is up to you.
Why should you Learn Software Design Patterns?
Why do we require design patterns? We don't want to reinvent the wheel, to put it bluntly!
Problems that arise frequently enough in the software world usually have well-defined solutions that are flexible, modular, and easier to understand. When these solutions are abstracted from implementation details, they become design patterns.
If you had a déjà vu feeling while devising a solution for a problem that felt strikingly similar to a previous problem's solution, albeit, in a different area, you were probably unwittingly applying a pattern.
Gang of Four (GoF)
In 1994, four authors, Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, wrote a book named 'Design Patterns - Elements of Reusable Object-Oriented Software,' which popularised the design pattern concept in software development. These authors are known as the Gang of Four (GOF). According to these writers, design patterns are essentially based on the following object-oriented design concepts:
- Program to an interface, not an implementation
- Prefer object composition to inheritance.
Their book is considered a classic in the field of software engineering. It was published over 25 years ago and is still very popular and a must-read for Software Engineers. It cataloged 23 design patterns into three categories: creational, structural, and behavioral.
Usage of Design Patterns in Software Engineering
Design Patterns have two main usages in software development.
- Provide a standard for developers:
Design patterns provide standardized terminologies and are scenario-specific. A singleton design pattern, for example, denotes the use of a single object; thus, all developers familiar with the single design pattern will use a single object and may notify each other that the software is following the singleton pattern. - Recommended Practices:
Design patterns have evolved and provide the best answers to challenges encountered during software development. Learning these patterns allows inexperienced engineers to learn software design more quickly and easily.
Types of Design Patterns in Software Engineering
Design patterns vary in complexity, level of detail, and scale of applicability to the entire system being designed. Let us take the analogy to road construction: you can make an intersection safer by either installing some traffic lights or building a whole multi-level interchange with underground passages for pedestrians.
As per the book 'Design Patterns - Elements of Reusable Object-Oriented Software,' there are 23 design patterns that can be classified into three categories: Creational, Structural, and Behavioral. Let us look at each one of them in detail.
Creational Patterns
These patterns provide various object creation mechanisms, which increase flexibility and reuse of existing code.
These design patterns are all about instantiating classes or creating objects. These patterns are further divided into two types: class-creational patterns and object-creational patterns. While class-creation patterns effectively employ inheritance in the instantiation process, object-creation patterns effectively use delegation. The Factory Method, Abstract Factory, Builder, Singleton, Object Pool, and Prototype are examples of creational design patterns.
Structural Patterns
-
These patterns provide various object creation mechanisms, which increase flexibility and reuse of existing code.
-
These design patterns are concerned with grouping various classes and objects into larger structures that give new functionality. Adapter, Bridge, Composite, Decorator, Facade, Flyweight, Private Class Data, and Proxy are some examples of structural design patterns.
Behavioral Patterns
-
Behavioral patterns are concerned with algorithms and the assignment of responsibilities between objects.
-
These design patterns are about recognizing and realizing common communication patterns between objects. Chain of duty, Command, Interpreter, Iterator, Mediator, Memento, Null Object, Observer, State, Strategy, Template method, and Visitor are examples of behavioral patterns.
Below is an image showing the relationship among the various design patterns as explained by the seminal design patterns work done by the gang of four.
Anti-Patterns
An anti-pattern is a common reaction to a reoccurring problem that is usually ineffectual and potentially highly detrimental.
As the name implies, Anti-Patterns are typical solutions to common issues that have adverse effects. These Anti-Pattern remedies may appear effective and acceptable initially but fail to produce satisfactory results. They have more negative effects than positive ones.
Anti-patterns are software engineering patterns that are considered undesirable programming techniques. Anti-patterns are the polar opposite of design patterns, which are standard techniques to handle common problems that have been formalized and are usually regarded as an excellent development practice.
Conclusion
- A design pattern in software engineering is a general repeatable solution to a frequently occurring problem in software design.
- A design pattern is not a finished design that can be easily translated into code. Instead, it is a description or template for solving an issue that may be applied in a variety of contexts.
- Patterns enable software engineers to communicate about software interactions using well-known, well-understood terminology.
- Over time, common design patterns can be enhanced, making them more robust than ad hoc ideas.
- Creational design patterns are all about class instantiation. Structural design patterns are all about Class and Object composition. Behavioral design patterns are all about the Class's object communication.