Singleton Pattern

Sometimes it’s important to have only one instance for a class. For example, in a system, there should be only one window manager (or only a file system or only a print spooler). Usually, singletons are used for centralized management of internal or external resources and they provide a global point of access to themselves. Create an object only once at a time.

Example – DB Object, Logger Object

Source Code (Java):

public class SingletonClass {

	private static SingletonClass _instance;

	public static SingletonClass getInstance() {

		if (_instance == null) {
			return _instance = new SingletonClass();

		return _instance;
	public void disPlayMessage(){
		System.out.println("Singleton Object generated");
public class MainClass {

	public static void main(String args[]) {

		SingletonClass instance = SingletonClass.getInstance();


Toufiq Mahmud

Leave a Reply

Your email address will not be published.