Emi Melo
Emi Melo
Dec 21, 2018 · 3 min read

Developers frequently encounter similar problems when investigating the developing world. Some experienced programmers discovered that the solution to these problems were always the same, and thus categorized both the problems and their solutions. Referred to as design patterns, the most known patterns are written down in a book called “Design Patterns¹.

We can think of these patterns as cake recipes, they all have a simple methodology to be followed: they need to have a name, to describe the problem encountered and to propose a solution with a UML diagram. Finally, a comparison of the pros and cons is completed using this solution.

Solutions can be classed into three major groups: those that takes care of the construction of objects and classes are called creational, those that organize the relationship between the different classes in a program are called structural, and finally those that aim to facilitate the communication between objects and classes are called behavioral.

The Singleton Design Pattern is a creational pattern example. It was created as a solution to classes that need to be instantiated only once. Singleton proposed a design where only one instance of the class is created in the entire duration of the application, but also ensures that other classes have a global point of access to the object.

This pattern is used in applications like print spoolers, where we only need one print managing the work list, or a single database connection shared between many classes.

To explain the problem encountered, let’s examine a very simple example in Java (inspired from this website) let’s pretend that we are creating a music streaming program where we want to only have one song history for each user. We will write on the console the list of the songs listened to, so the user may look back at the history and find the song that they liked. Firstly, a UML scheme must be created:

Picture 1: UML Diagram of the music streaming platform²

It can be seen in the UML diagram that there are two classes: MusicHistory and MusicStreaming. The MusicHistory class demands to be instantiated only once, so a private constructor must be used. But of course, we will need an access to this unique instance, that’s why a static function (getInstance()) is created to allow us to use the music history.

The MusicHistory class written in Java is seen as follows:

Picture 2: Java code of the MusicHistory class

The constructor is declared as private, so it’s only accessible from this class. There’s one attribute that allows us to stock the instance of this object: onlyInstance, which is returned by the static synchronized function. In this case we use a synchronized method to handle a multithread case (simultaneous use of the application).

Picture 3: Java code of the MusicStreaming class

We can see that in the MusicStreaming class we use the method getInstance() from the MusicHistory class.

In conclusion, our imaginary application allows us to have one history per user and doesn’t allow creation from outside the MusicHistory class. This demonstrates an example of using a Singleton Pattern Design.

Like all design patterns, the Singleton Design Pattern isn’t a completely perfect solution for our coding problems. For example, if we don’t use the synchronized keyword and we have 2 threads trying to access the instance, we will in turn create more than one instance. Yet the usage of a synchronized³ key will increase the program cost and as a result will decrease its efficiency.

Designs patterns are solutions for common coding problems, and the Singleton is one of them. The Singleton Pattern proposes solutions for classes that demand to be instantiated only once throughout the whole application time and gives a universal access to that object. The singleton is mostly used in logger and configuration classes.

ELP-2018

Ecosystème des langages de programmation

Thanks to Tristan Roussillon

Emi Melo

Written by

Emi Melo

ELP-2018

ELP-2018

Ecosystème des langages de programmation

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade