Design Patterns
Main concepts behind design patterns
Introduction
Section titled “Introduction”I’ve been using refactoring.guru/design-patterns as a refence for this article.
Design patterns are typical solutions to commonly occurring problems in software design.
They are like pre-made blueprints that you can customize to solve a recurring design problem in your code.
Design patterns can be categorized by their intent and divided into three groups:
- Creational patterns
- Structural patterns
- Behavioral patterns
Creational patterns provide object creation mechanisms that increase flexibility and reuse of existing code:
- Singleton,
- Factory Method,
- Abstract Factory,
- Builder,
- Prototype
Structural patterns explain how to assemble objects and classes into larger structures, while keeping these structures flexible and efficient;
- Adapter,
- Bridge,
- Composite,
- Decorator,
- Facade,
- Flyweight,
- Proxy
Behavioral patterns take care of effective communication and the assignment of responsibilities between objects:
- Chain of Responsibility,
- Command,
- Iterator,
- Mediator,
- Memento,
- Observer,
- State,
- Strategy,
- Template Method,
- Visitor
Singleton
Section titled “Singleton”Creational pattern
Singleton design pattern lets you ensure that a class has only one instance, while providing a global access point to this instance.
Problem
Section titled “Problem”The Singleton pattern solves two problems:
- Ensure that a class has just a single instance,
- Provide a global access point to that instance.
Just like a global variable, the Singleton pattern lets you access some object from anywhere in the program. However, it also protects that instance from being overwritten by other code.
Solution
Section titled “Solution”All implementations of the Singleton have these two steps in common:
- Make the default constructor private: prevent other objects from using the
newoperator with the Singleton class. - Create a static creation method that acts as a constructor.
Under the hood, the static method calls the private constructor to create an object and saves it in a static field. All following calls to that method return the cached object. If your code has access to the Singleton class, then it’s able to call the Singleton’s static method. So whenever that method is called, the same object is always returned.
Applicability
Section titled “Applicability”Use the Singleton pattern when a class in your program should have just a single instance available to all clients (i.e. a single database object shared by different parts of the program). Use the Singleton pattern when you need stricter control over global variables.
Implementation
Section titled “Implementation”The steps to implement Singleton pattern are:
- Add a private static field to the class for storing the singleton instance.
- Declare a public static creation method for getting the singleton instance.
- Implement lazy initialization inside the static method. It should create a new object on its first call and put it into the static field. The method should always return that instance on all subsequent calls.
- Make the constructor of the class private. The static method of the class will still be able to call the constructor, but not the other objects.
- Go over the client code and replace all direct calls to the singleton’s constructor with calls to its static creation method.
Pros & Cons
Section titled “Pros & Cons”| Pros |
|
|---|---|
| Cons |
|
Since the constructor of the singleton class is private and overriding static methods is impossible in most languages, it will be difficult to mock the singleton.