Design pattern Singleton, in a nutshell

Luiz Gustavo De O. Costa
3 min readNov 26, 2020

Alone, this word defines Singleton’s behavior. In this article, I’ll show the benefits and some code examples in Spring, Quarkus, and writing by my own Singleton class.

Design Pattern Singleton

Motivation

It’s important for some classes to have exactly one instance. Although there can be many printers in a system, there should be only one printer spooler. There should be only one file system and one window manager. A digital filter will have one A/D converter. An accounting system will be dedicated to serving one company.[3]

Applicability

There must be exactly one instance of a class, and it must be accessible to clients from a well-known access point.[3]

when the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code.[3]

Where I can use this pattern? [5,6]

  1. Hardware interface access,
  2. Logger,
  3. Configuration file,
  4. Cache,
  5. Controls concurrent access to a shared resource,
  6. Access to the resource will be requested from multiple, disparate parts of the system,
  7. Storing some global state (user language, help filepath, application path),
  8. Managing a connection (or a pool of connections) to a database.

Examples

Spring

In Spring the Singleton pattern could be easily used, just adding the annotation @ scope(“singleton”). But sometimes the Singleton doesn’t fit the requirements and the @ scope(“prototype”) should be used instead.

Spring Singleton

In order to check if the Singleton is applied, the code below does it. The test is repeated 3 times and only one instance of the class above should be created. After the execution, there is a verification on method testIt (bad name) I know 😥. To this test fails, only change the scope to “prototype”.

Spring Singleton Test
Test result executed on IntelliJ

Quarkus/Java EE

Now is Quarkus time 😍. In Quarkus, using the annotation @Singleton, yes a good name from Java EE, is used to declare that the class is a Singleton. As I said before if your requirements don’t fit in this pattern, use the annotation @Dependent to have other behavior.

Quarkus Singleton

The Singleton test for Quarkus follows the same approach as Spring, a repeated test, and then a test to check how many classes were used.

Quarkus Singleton Test
Test result executed on IntelliJ

Hands on

Now is time to code our Singleton. Is important to decide when creating the class and also don’t forget to use the synchronized reserved word on method declaration to avoid some pitfalls. I’m not cover all pitfalls, check the reference article number 4, in the References section.

Below, the Singleton structure, with a private constructor, a static method to return the instance, and one class behavior, in this case, count tasks. Remember, no one is able to execute two tasks at the same time, the humans are single task, as Singleton is.

Singleton

As before, was used the repeated tests to prove how many instances were created.

Singleton Test
Test result executed on IntelliJ

Using enum

Enum also could be used as a Singleton class. Behind the scenes the enum is converted into a class then has the same effect of using a class. Take a look on the enum and the test.

Enum singleton

Test based on enum, to test if it’s a singleton.

Enum Singleton Test
Test execution

Code

The complete code is available on my GitHub.

References

1 — Cover image

2 — https://www.oracle.com/technical-resources/articles/java/singleton.html

3 — https://learning.oreilly.com/library/view/design-patterns-elements/0201633612/ch03.html

4 — https://dzone.com/articles/all-about-the-singleton

5 — https://stackoverflow.com/questions/228164/on-design-patterns-when-should-i-use-the-singleton

6 — https://www.geeksforgeeks.org/singleton-design-pattern-introduction/

--

--

Luiz Gustavo De O. Costa

Hey friend!! I’m Luiz Gustavo, a Java developer and I’m here to learn and write about Java, tests and good practices