Home » Blog » Software Development » Software Design Patterns in PlantUML » Design Patterns with PlantUML: Singleton

Design Patterns with PlantUML: Singleton

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 Factory Method Pattern in UML notation. It is based on the corresponding chapter in the book “Head First Design Patterns“:

PlantUML Syntax:
<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>

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.

Adapter Pattern Design Patterns with PlantUML: Adapter Pattern - This post gives a brief overview about the Adapter Pattern. The post is part of a series about software design ... Read More
Command Pattern Design Patterns with PlantUML: Command Pattern - This post gives a brief overview about the Command Pattern. The post is part of a series about software design ... Read More
Singleton Design Patterns with PlantUML: Singleton - This post gives a brief overview about the Singleton Pattern. The post is part of a series about software design ... Read More
Design Patterns with PlantUML: Abstract Factory Pattern - This post gives a brief overview about the Abstract Factory Pattern. The post is part of a series about software design ... Read More
Design Patterns with PlantUML: Factory Method Pattern - This post gives a brief overview about the Factory Method Pattern. The post is part of a series about software ... Read More
Decorator Pattern Design Patterns with PlantUML: Decorator Pattern - This post gives a brief overview about the Decorator Pattern. The post is part of a series about software design ... Read More
Observer Pattern Design Patterns with PlantUML: Observer Pattern - This post gives a brief overview about the Observer Pattern. The post is part of a series about software design ... Read More

Check Also

Adapter Pattern

Design Patterns with PlantUML: Adapter Pattern

This post gives a brief overview about the Adapter Pattern. The post is part of …

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

We use Cookies and similar technology to collect and analyse information about the users of this website. We use this information to enhance the content, advertising and other services available on the site. Please click ‘Accept cookies’ to consent to the use of this technology by petrockblock. You can manage your preferences at any time by visiting our Cookies Policy page.