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 Syntax:</p><p>@startuml<br /> title Singleton</p><p>class Singleton</p><p>Singleton : {static} uniqueInstance<br /> Singleton : // other useful Singleton data …<br /> Singleton : {static} getInstance() : Singleton<br /> Singleton : // other useful Singleton methods()</p><p>note right of Singleton<br /> The uniqueInstance<br /> class variable holds our<br /> one and only instance<br /> of Singleton<br /> end note</p><p>note left of Singleton<br /> The getInstance() method is static,<br /> which means it’s a class method, so you<br /> can conveniently access this method<br /> from anywhere in your code using<br /> Singleton getInstance(). That’s just as<br /> easy as accessing a global variable, but<br /> we get benefit like lazy instantiation<br /> from the Singleton.<br /> end note</p><p>note bottom of Singleton<br /> A class implementing the Singleton<br /> Pattern is more than a Singleton;<br /> it is a purpose class with its<br /> own set of data and methdods.<br /> end note<br /> @enduml</p><p>

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:


@startuml
title Singleton

class Singleton

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

@enduml

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”]