0% found this document useful (0 votes)
29 views

Design Pattern - Singleton Pattern

The singleton pattern ensures that only one instance of a class is created. It provides a global point of access to the object and ensures only one object is created. The singleton class creates a private instance of itself and provides a public static method to access the instance.

Uploaded by

Indy Singh
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)
29 views

Design Pattern - Singleton Pattern

The singleton pattern ensures that only one instance of a class is created. It provides a global point of access to the object and ensures only one object is created. The singleton class creates a private instance of itself and provides a public static method to access the instance.

Uploaded by

Indy Singh
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/ 2

DESIGN PATTERN - SINGLETON PATTERN

http://www.tutorialspoint.com/design_pattern/singleton_pattern.htm Copyright © tutorialspoint.com

Singleton pattern is one of the simplest design patterns in Java. This type of design pattern comes
under creational pattern as this pattern provides one of the best ways to create an object.

This pattern involves a single class which is responsible to create an object while making sure that
only single object gets created. This class provides a way to access its only object which can be
accessed directly without need to instantiate the object of the class.

Implementation
We're going to create a SingleObject class. SingleObject class 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.

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


//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();

//show the message


object.showMessage();
}
}

Step 3
Verify the output.

Hello World!

You might also like