Singleton Design Pattern

Default featured post

Design pattern is a part of object oriented programming which sometimes is difficult to understand or find a logical reason to apply them. Overall, up to now design pattern was not that much useful for me and just makes my application really complex and in some extends difficult to understand. Anyway, today I am writing about Singleton design pattern which is one of the easiest design pattern to learn and apply. I will concentrate on definition, motivation, applicability, consequences, and finally an example.

Definition

Singleton design pattern makes sure that a class has just only one instance and provides a global access to it.

Motivation

Singleton can be utilized in many systems which is important that a class just has only one instance not more. For instance, in a system many printers can be installed but all of the printers have just only one printer spooler to mange them and schedule printing. Or in other systems sometimes there should be one file system, one windows manager, etc.

Although, it is possible to access to one instance globally with defining a global variable, it is impossible to control the number of instances and make sure that just is available not more. Singleton design pattern does not allow a class to have more than one instance with imposing limitation on the constructor.

Applicability

  1. It is used when exactly one instance of a class must be available and it must be accessible from different access points.
  2. When the single instance should be extensible bu sub class, and clients should be able to use an extended instance without changing their code.

Consequences

  1. Controlled access: Since singleton encapsulates it sole instance, it can have strict control on how and when clients access it.
  2. Reduced name space: The singleton pattern is an improvement over global variables. It avoids polluting name space with global variables that store instances.
  3. Permits refinement of operation and representation: The singleton class may be subclasses and it is easy to configure an application with an instance of this extended class.
  4. Permits a variable number of instances: The pattern makes it easy to change your mind and allow more than one instance of the singleton class.
  5. More flexible than class operation: In comparison with the class operation which is available in C++ (Static member functions) singleton is more flexible and allows user to change code to have more than one instance.

Example

public class Student {
private String name;
private String id;
private int age;
// private static variable
private static Student s = new Student();
// private constructor
private Student() {
}
// public static global access point
public static Student createStudent() {
return s;
}
public void setName(String x) {
name = x;
}
public void setId(String x) {
id = x;
}
public void setAge(int x) {
age = x;
}
public String getName() {
return name;
}
public String getId() {
return id;
}
public int getAge() {
return age;
}
}
view raw Student.java hosted with ❤ by GitHub

UML Diagram

Singleton Design Pattern

I’ve learned this design pattern from a design pattern book which I do not remember its name. Some parts of this post are very similar to that book like motivation part, etc. As a result the main source of this post is that book which I cannot remember the name.