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

Design Patterns Singleton Pattern

The singleton pattern provides a simple way to create a single instance of a class and provide access to it through a global point of access. It involves a private constructor, a static method that returns the instance, and a static field containing the instance. This ensures only one object is created while providing simple access to that object from anywhere. The document provides an example singleton class SingleObject that creates a single instance, makes the constructor private, and provides a static getInstance() method to retrieve the single object.

Uploaded by

corneliuskoo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Design Patterns Singleton Pattern

The singleton pattern provides a simple way to create a single instance of a class and provide access to it through a global point of access. It involves a private constructor, a static method that returns the instance, and a static field containing the instance. This ensures only one object is created while providing simple access to that object from anywhere. The document provides an example singleton class SingleObject that creates a single instance, makes the constructor private, and provides a static getInstance() method to retrieve the single object.

Uploaded by

corneliuskoo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Design Patterns Singleton Pattern

1 of 4

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

Advertisements

Previous Page

Next Page

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.

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.

03/08/2016 13:56

Design Patterns Singleton Pattern

2 of 4

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

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!");
}
}

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

Verify the output.


Hello World!

Previous Page

Print

PDF
Next Page

Advertisements

03/08/2016 13:56

Design Patterns Singleton Pattern

3 of 4

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

03/08/2016 13:56

Design Patterns Singleton Pattern

4 of 4

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

03/08/2016 13:56

You might also like