You are on page 1of 6

Lab # 5 SSUET/QR/114

LAB # 5

Creational Patterns: Singleton Pattern

OBJECTIVE
Implementation of Creational Design Patterns: Singleton Pattern.

THEORY:
1. Singleton Pattern
A singleton is a creational pattern, which describes a way to create an object. It is a powerful
technique, but also one of the simplest examples of a design pattern. In a singleton design
pattern only has one object of a class. This might be desirable in order to circumvent conflicts or
inconsistencies, by keeping clear which object the program should draw from. For example, the
Preferences of an app, the print queue of your printer, the software driver for a device are objects
that only having one of is preferable. If there are multiple instances, it can be confusing for the
program output. Another goal of singleton design pattern is that the single object is globally
accessible within the program.

Clients may not even realize that they’re working with the same object all the time.

SWE-208: Software Design and Architecture


Lab # 5 SSUET/QR/114

We're going to create a SingleObject class. SingleObject class will have its constructor as private


and have a static instance of itself.

SingleObject class provides a static method to get its static instance to outside


world. SingletonPatternDemo, our demo class will use SingleObject class to get
a SingleObject object.

Java Code:
Step 1
Create a Singleton Class.

SingleObject.java

public class SingleObject {

//create an object of SingleObject

private static SingleObject instance = new SingleObject();

//make the constructor private so that this class cannot be

SWE-208: Software Design and Architecture


Lab # 5 SSUET/QR/114

//instantiated

private SingleObject(){}

//Get the only object available

public static SingleObject getInstance(){

return instance;

public void showMessage(){

System.out.println("Hello World!");

Step 2
Get the only object from the singleton class.

SingletonPatternDemo.java

public class SingletonPatternDemo {

public static void main(String[] args) {

//illegal construct

//Compile Time Error: The constructor SingleObject() is not visible

//SingleObject object = new SingleObject();

//Get the only object available

SingleObject object = SingleObject.getInstance();

SWE-208: Software Design and Architecture


Lab # 5 SSUET/QR/114

//show the message

object.showMessage();

Output:
Step 3
Verify the output.

Hello World!

Another Example:

Let’s pretend that we are creating a music streaming program where we want to only have one
song history for each user. We will write on the console the list of the songs listened to, so the
user may look back at the history and find the song that they liked. Firstly, a UML scheme must
be created:

It can be seen in the UML diagram that there are two classes: MusicHistory and
MusicStreaming. The MusicHistory class demands to be instantiated only once, so a private
constructor must be used. But of course, we will need an access to this unique instance, that’s
why a static function (getInstance()) is created to allow us to use the music history.

The MusicHistory class is seen as follows:

SWE-208: Software Design and Architecture


Lab # 5 SSUET/QR/114

The constructor is declared as private, so it’s only accessible from this class. There’s one
attribute that allows us to stock the instance of this object: onlyInstance, which is returned by the
static synchronized function. In this case we use a synchronized method to handle a multithread
case (simultaneous use of the application).

SWE-208: Software Design and Architecture


Lab # 5 SSUET/QR/114

We can see that in the MusicStreaming class we use the method getInstance() from the
MusicHistory class.

In conclusion, our imaginary application allows us to have one history per user and doesn’t allow
creation from outside the MusicHistory class. This demonstrates an example of using a Singleton
Design Pattern.

Exercise
The Singleton Pattern proposes solutions for classes that demand to be instantiated only once
throughout the whole application time and gives a universal access to that object. Think where
you can apply singleton design pattern on your selected game scenario. For e.g, you have
selected candy crush. The application records your current level, your achieved score, the
candies you collected etc. which means one universal history per user.

Think of any such game and apply Singleton Design pattern. Make a class diagram first and then
implement it.

Hint: A GameHistory class can be made which demands to be instantiated only once, so a
private constructor must be used. But we will need an access to this unique instance, that’s
why a static function (getInstance()) is created to allow us to use the user game history.

SWE-208: Software Design and Architecture

You might also like