Introduction to Design Patterns & Understanding Singleton Design Pattern

Nisal Pubudu
Geek Culture
Published in
6 min readMay 22, 2021
Photo by Clark Van Der Beken on Unsplash

If you are a software engineer, understanding design patterns and working with those will make you an exceptional software engineer. Because if you work in an industry, you might see software engineers who have no idea about design patterns and how those work. Most of them are intern or associate level engineers or may be engineers who have few years of experience. So whatever level you are in, understanding design patterns is very critical part in our industry. So, in this article I will briefly explain about design patterns and detail explanation about Singleton design pattern.

What are Design Patterns?

In simple terms design patterns are practical solutions to general problems that software engineers faced during software development. All these patterns are like blueprints that we can use and customize to solve those specific problems in our code.

There are some common misunderstands about design patterns, such as these are particular codes that we can apply in our program, and design patterns are another way of describing algorithms. The reason for people gets confused between design patterns and algorithms because both are responsible to solve some problem in programming. But an algorithm is a clear set of actions that we can use to solve specific part of our code while design patterns are high-level description of a solution. Above all, design patterns are not particular codes that we can apply to our program, and as I mentioned already, those are like a blueprint on how to solve these problems.

Design patterns are always identified as a good practice to use while programming. Because there are many advantages of using design patterns, such as:

  • They are easily reusable in multiple projects.
  • They use object-oriented concepts like decomposition, inheritance, and polymorphism.
  • They are well-proved and testified solutions since they have been built upon the knowledge and experience of expert software developers.
  • Using proper design pattern in development of applications make development fast and easily documented.
  • They Lower the Size of the Codebase.
  • They provide the solutions that help to define the system architecture.
  • They provide transparency to the design of an application.

Types of design patterns

In 1994, 4 authors known as “Gang of Four” released a book named as “Design Patterns”. In that book they divided all these design patterns into three categories. At the moment there are 23 design patterns and all these are categorize under these 3 type of categories named as, Creational, Structural, and Behavioral.

Image: Design Patterns (laptrinhx.com)

In the next chapter I will explain about one of the most used design pattern named as Singleton design pattern.

Singleton Design Pattern

The Singleton Design Pattern is a Creational pattern, and it is one of the simplest design pattern in gang of four. The main functionality of this pattern is, to create only one instance of a class and to provide only one global access point to that object within the container (JVM; in case of Java).

If you wonder why would we need only one instance of a class (one object of a class), the most common reason for this is to control access to some shared resource, such as a database or a file. However, this design pattern considers as one of the most abused design pattern, due to some developers use it without proper understanding where to apply this pattern in their program.

When you are applying Singleton design pattern, there are several things that you should consider. The most common one is, you should not accept any arguments when you create the instance. In case if you want to accept arguments in this way, you should go with Factory design pattern instead of Singleton.

Also, if you want to use Singleton design pattern in your program, make sure not to overuse this pattern. The reason is, it really hard for unit testing due to no other instance variables and any references available. So, do not use this design pattern everywhere it can, instead use whenever it necessary.

Now I will explain how to apply Singleton design pattern with a practical example in Java.

Use case of Singleton design pattern

But before moving into the example, you need to know that there are two common steps in every Singleton implementation.

  • First step is to make the default constructor private, so it can prevent other objects from using the new operator with the Singleton class.
  • Then implement a static creation method that acts as a constructor. Under the hood, this method calls the private constructor to create an object and saves it in a static field. So, whenever this method gets called, it will return the object.

So, in the following chapter you will see examples of the singleton design pattern in several ways. However, all the examples were implemented based on a single scenario. The scenario is really simple; when a person deposit money into the bank, they will display or print the “interest rate” of that bank. Assume that the interest rate and its process only need to be created one time.

Note: The most common use case for applying Singleton design pattern is when dealing with databases.

Example 01:

In this example I’m going to implement the Singleton design pattern in the simplest way.

If we run this program, the result will be look like this:

Image: Results of the 1st example

As you can see on the result, we got the exact same instance even though we create two instances of that class. So that means, the implementation of Singleton design pattern is successful. However, this is not a complete implementation of Singleton design pattern. Because this will limit the things that can we do with this instance. So, let’s move to the next example and see how we can make it a more complete one.

Example 02:

If we run this program, the result will be exactly same as the previous example. So, why this way is more complete and secure than the first example? Because in the “getInterestRate” method, we check if the “iRate” object is created, and if it is not, we create the “iRate” object here (13th line). Additionally, in the private constructor, we have prevented the interfere from “Reflection framework”.

Even though its more secure and complete than first one, still there is a minor drawback in this implementation. Because this is not a thread safe implementation. So, in the next example we can see how to implement a complete and secure Singleton design pattern.

Example 03:

This example implemented using a principle called as “Double-Checking Singleton.” As a result, this can be identified as a fully complete and secure implementation of Singleton design pattern. As you can see in the gist, I have used synchronized keyword (13th line) to make that code segment into a thread safe one.

Also make sure not to use synchronized keyword with the method declaration, since it will cause you performance loss. Instead use it only for the code segment you want to synchronize.

If you are interested, you can use the following GitHub link to see my complete implementation of Singleton Design Pattern.

So, this is the end of my article and I hope you enjoyed it. Happy Coding👨‍💻.

--

--