Don’t reinvent the wheel, just realign it (Anthony J. D’Angelo). I am sure it is not the first to hear this quote, and by now, you are wondering why I have started the topic with such a dramatic intro. Be patient it will soon be clear.
The above quote indicates that it is useless to reinvent a standardized solution for a common problem. Although wheels can be enhanced in terms of durability, usability, longevity and usagity .. oh wait there is no such word, I just liked how it rhimes. Going back to the topic, the base of the wheel is the same even if it got enhanced.
What does all this mean to you as a software developer?
In software engineering, there is something called design patterns. A design pattern is a standard solution to a common problem (Liskov . B, 2005). In other words, if you want to write a code to tackle a specific problem, a design pattern will provide you with a blueprint on how to solve it in a way that is proven to be successful.
I would like to emphasise something, a design pattern is not the code per se, it is the idea or the concept behind it. So before blindly copying a code because it is a design pattern, you should know the concept behind it, usage case scenarios, code requirement, just to mention a few. Remember, it is not voodoo magic, its concepts to be applied only and only if your requirement meets the design pattern.
Furthermore, design patterns could be grouped into three main categories. Creational, Structural, and Behavioral.
Side note, there could be more. However, in my humble opinion, these three are the most popular and involves enough patterns to choose from.
Moreover, to learn effectively. This post will only focus on one of the creational patterns called a Singleton. Therefore, the rest of this post will be structured as follows.
- Creational patterns definition
- What is a Singleton pattern?
- Real-life Singleton vs Singleton class
- When to use a Singleton pattern?
- The best resource to learn about design patterns
- Final thoughts
Creational patterns definition
Creational patterns are patterns that focus on the mechanisms by which the instance of a class is created.
What is a Singleton pattern?
According to my understanding, a Singleton pattern is one of the creational patterns. Its main intent is to have a single instance of a class in the entire application. In addition, a singleton must provide global access for other classes to use it.
A real-life example of a Singleton vs Singleton class
You are a singleton! how?
First, you got initialized only once when you were born. Second, there is no copy of you since you were born. Third, you allow others to interact with you through global means like approaching you directly, over the phone, video call, just to mention a few.
What are the similarities between you and a singleton class?
First, a singleton class can only be initialized once. Second, a singleton class can not be copied. Third, a singleton class must provide a global access mechanism and the most common way to do it is through a public static method to get an instance of the class.
The most important question is when to use this pattern?
This pattern is widely used for accessing a shared resource such as a file or a database safely.
Imagine a case scenario of multiple objects trying to modify the same file at the same time. If there is no proper access management, the result will be either a crash, race-condition or the file could be overridden with the wrong content. However, a singleton with thread-safe access to the file will solve the issue.
Side note: be aware, a singleton is not thread-safe by default, you should look around for a thread-safe singleton implementation according to your specific case scenario.
The best resource to learn about design patterns
If you got interested in learning more about design patterns then I will highly recommend the second reference in the list of references below. It involves code examples, a depth explanation and a great way to present the pattern. It’s all for free.
However, you can buy the PDF book if you wanted to support the author. Bear in mind, I am not an affiliate nor in touch with the author, this is my personal opinion only.
Design patterns are one of the ways to reduce time and effort when solving coding problems. However, you should not view them as a dogma and start implementing a design pattern everywhere in your code. A design pattern exists to solve a particular problem. Therefore, your case should match the intent of the design pattern.
Liskov, B. (2015) Design Patterns. Available at: https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-170-laboratory-in-software-engineering-fall-2005/lecture-notes/lec18.pdf.
Design Patterns (2007) software design patterns with code examples. Available at: https://sourcemaking.com/design_patterns