1. Introduction to Design Patterns
Hi everyone, it is the first article of my Design Patterns series. In this series, we will understand almost everything related to Design Patterns. So let's start with an introduction.
Introduction
In Software Engineering you may face the same design problems again and again and of course, there are many ways to solve these problems, but to avoid more problems in the future when you try to maintain your code, we need to use flexible and reusable solutions. One of these preferred solutions is that of design patterns.
What is Design Pattern?
A design pattern is a practical proven solution to a common design problem. It is like the best practices used by expert software engineers to solve this problem and these solutions help us to speed the process of development because you don't have to think, create and build a solution every time you face these problems. Also, a design pattern is not a standard piece of code that you just put in your code and the problem is solved, it is more like a model that you can modify to be suitable to use in your problem.
Gang of Four (GoF)
Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides are the authors of the famous book Design Patterns: Elements of Reusable Object-Oriented Software. They introduced 23 patterns of design in this book based on their experience as developers. Those patterns -which were introduced by GoF- are considered the foundation for all other patterns.
Categories of Design Patterns
Gang of Four patterns can be categorized into three groups:
1. Creational Patterns
Creational Patterns help us to deal with creating or cloning new objects. Creational Patterns are:
- Factory Method
- Abstract Factory
- Builder
- Singleton
- Object Pool
- Prototype
2. Structural Patterns
Structural Patterns help us to describe the connections between objects. These patterns relate to design principles of decomposition and generalization. Structural Patterns are:
- Adapter
- Bridge
- Composite
- Decorator
- Facade
- Flyweight
- Private Class Data
- Proxy
3. Behavioral Patterns
Behavioral Patterns help us to describe how each object performs a single cohesive function and describe how independent objects work towards a common goal. Behavioral Patterns are:
- Chain of Responsibility
- Command
- Interpreter
- Iterator
- Mediator
- Memento
- Null Object
- Observer
- State
- Strategy
- Template method
- Visitor
Elements of a Design Pattern
The Gang of Four's book tells us that each pattern has four essential elements:
- The Name - Used to describe a design problem, its solutions, and consequences in a word or two.
- The Problem - describes when to apply the pattern and explains the problem and its context.
- The Solution - that describes the elements that make up the design, the relationships, the responsibilities, and collaborations. The solutions do not describe a concrete implementation, because a pattern is like a template that can be applied in many different situations, but provides an abstract description of a design problem and how a general arrangement of elements (classes and objects) solves it.
- The consequences - the results of applying the pattern.
Reasons to use Design Patterns
There are many reasons to use design patterns. As we said before it makes the process of implementation faster because we don't have to think and build a solution for a problem every time we face it. It also defines a common language to make it easier to communicate between team members.
Conclusion
In this article, we give a quick introduction to design patterns and discussed categories of design patterns, elements of each pattern, and why we should use design patterns. In the following articles, we will talk in detail about each pattern and how to use design patterns.