This post gives a brief overview about the Singleton Pattern. The post is part of a series about software design patterns and their UML representations with the help of PlantUML.

The article aims at providing a very short description of the general idea of the pattern in the first part. This also involves a descriptive UML diagram. Then, the second part provides the PlantUML code for the diagram so that these posts can also be used as a source of design patterns in PlantUML syntax.

What is the Singleton Pattern?

According to Wikipedia, the Singleton Pattern is a software design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system.

The singleton pattern solves problems like:

  • How can it be ensured that a class has only one instance?
  • How can the sole instance of a class be accessed easily?
  • How can a class control its instantiation?
  • How can the number of instances of a class be restricted?

The singleton design pattern describes how to solve such problems:

  • Hide the constructor of the class.
  • Define a public static operation (getInstance()) that returns the sole instance of the class.

The key idea in this pattern is to make the class itself responsible for controlling its instantiation (that it is instantiated only once).
The hidden constructor (declared private) ensures that the class can never be instantiated from outside the class.
The public static operation can be accessed easily by using the class name and operation name (Singleton.getInstance()).

UML Diagram

The following diagram shows the Singleton Pattern in UML notation. It is based on the corresponding chapter in the book “Head First Design Patterns“:

[plantuml]

@startuml
title Singleton

class Singleton

Singleton : {static} uniqueInstance
Singleton : // other useful Singleton data …
Singleton : {static} getInstance() : Singleton
Singleton : // other useful Singleton methods()

note right of Singleton
The uniqueInstance
class variable holds our
one and only instance
of Singleton
end note

note left of Singleton
The getInstance() method is static,
which means it’s a class method, so you
can conveniently access this method
from anywhere in your code using
Singleton getInstance(). That’s just as
easy as accessing a global variable, but
we get benefit like lazy instantiation
from the Singleton.
end note

note bottom of Singleton
A class implementing the Singleton
Pattern is more than a Singleton;
it is a purpose class with its
own set of data and methdods.
end note
@enduml

[/plantuml]

PlantUML Sources

PlantUML is a tool allowing users to create UML diagrams from a plain text language. Here are the PlantUML sources for the above software pattern:

[code language=”javascript”]

@startuml
title Singleton

class Singleton

Singleton : {static} uniqueInstance
Singleton : // other useful Singleton data …
Singleton : {static} getInstance() : Singleton
Singleton : // other useful Singleton methods()

@enduml

[/code]

Other Design Patterns

In another article you find information about how to put together a single-side web application using PlantUML.

[display-posts category=”Software Design Patterns in PlantUML” include_excerpt=”true” excerpt_length=”20″ excerpt_more=”… Read More” excerpt_more_link=”true” image_size=”thumbnail” wrapper=”div”]