Professional Documents
Culture Documents
LAB # 5
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.
Java Code:
Step 1
Create a Singleton Class.
SingleObject.java
//instantiated
private SingleObject(){}
return instance;
System.out.println("Hello World!");
Step 2
Get the only object from the singleton class.
SingletonPatternDemo.java
//illegal construct
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 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).
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.