0% found this document useful (0 votes)
83 views10 pages

Lecture w7 03 PDF

The Singleton pattern ensures that only one instance of a class is created, and provides a global access point to that instance. There are three main steps to implement the Singleton pattern: 1) make the constructor private so no other class can instantiate the Singleton class, 2) create a static method that returns the instance, and 3) check if an instance already exists and if not, create one and save it statically for future access. To make the Singleton thread-safe, the getInstance method should be synchronized.

Uploaded by

Noora Sweis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views10 pages

Lecture w7 03 PDF

The Singleton pattern ensures that only one instance of a class is created, and provides a global access point to that instance. There are three main steps to implement the Singleton pattern: 1) make the constructor private so no other class can instantiate the Singleton class, 2) create a static method that returns the instance, and 3) check if an instance already exists and if not, create one and save it statically for future access. To make the Singleton thread-safe, the getInstance method should be synchronized.

Uploaded by

Noora Sweis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Singleton Pattern

Definition

Ensures that a class has only one instance, and


provides a global point of access to it

• There are two main points in the definition

• make it impossible to instantiate a class more than


once

• global access to this one instance


How to prevent an object
from being instantiated
more than one
Step 1: Private Constructor

• Use a private constructor

• This means that the constructor can only be called from


the class itself

public Singleton {

private Singleton(){}

}
Step 2: make the class responsible
for creating an instance of itself

• Add a static method inside the class, and make this method responsible to
making the instance

• return an object of type Singleton (class name)

• not enough : we can call Singleton.getInstance() as much as we want

public Singleton {

private Singleton(){}

public static Singleton getInstance (){

return new Singleton // not enough

}
Step 3:Make Sure to Create
One Instance
• Define a static variable to hold the one instance of the class Singleton

• in the getInstance method create an instance of the class Singleton if


it is not instantiated

public Singleton {

private static Singleton uniqueInstance ;

private Singleton(){}

public static Singleton getInstance (){

if (uniqueInstance == null){

uniqueInstance = new Singleton();//call the private constructor

return uniqueInstance;
this type of initialization is called lazy
}
initialization, created when requested
}
Singleton & Multithreading

• Change the getInstance method to be Synchronized

• Adding synchronized will force every thread to wait for its


turn before entering the getInstance method

public Singleton {

private static Singleton uniqueInstance ;

private Singleton(){}

public static Synchronized Singleton getInstance (){

if (uniqueInstance == null){

uniqueInstance = new Singleton();

return uniqueInstance;

}
Notes On Synchronization

• What to do

1. nothing if the performance is not an issue just use the synchronization as shown in
the previous slide

2. use eagerly approach to create the instance

3. double-checked locking
Eager Instantiation

• Create the instance from the beginning

• Any class wants an instance will get the same instance

public Singleton {

private static Singleton uniqueInstance = new Singleton() ;

private Singleton(){}

public static Singleton getInstance (){

return uniqueInstance;

• This considered a waste of resources, the instance might not be


needed
double-checked locking
• volatile indicates that the value public Singleton {

of the variable will be modified private volatile static Singleton uniqueInstance ;

private Singleton(){}

by multithreads
public static Singleton getInstance (){

if (uniqueInstance == null){

• synchronize only if the synchronized(Singleton.class){

if (uniqueInstance == null){

uniqueInstance is null
uniqueInstance = new Singleton();

• if it is not null, the created }

instance will be returned


return uniqueInstance;

• If null synchronize and check


}

again to make sure that


uniqueInstance is still null

You might also like