Design Patterns: SingletonBlog

Software Design Patterns with PlantUML: Singleton

post-thumb

Introduction

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 this posts can also be used as a source of design patterns in PlantUML syntax.

What is the Singleton Pattern?

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 (i.e., 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

Singleton Pattern

You can click on the diagram to open it online in Pladitor.

Diagram Sources

Here are the diagram sources:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
@startuml
title Singleton

class Singleton

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

@enduml

Open this diagram online with Pladitor.

Share: