Singleton Design Pattern Introduction and Implementation

Posted By : Amarjeet Kumar | 21-Sep-2018

 Singleton Design Pattern 

Need for Singleton Design Pattern 

Take the example of a Big MNC. They generally have one central printer for each floor. All associates belonging to that floor generally take the printout from that printer only. Even though ten associates have given request to print the document in the printer, the printer will print the document one by one on first come first serve basis. If I try to frame the above story in terms of programming, all associates will call the print() on PrinterUtil class object to print the document. But as we have only one printer, should we allow the associates to create PrinterUtil class object for more than one time? In real time, should we install one printer for each associate?

Conclusion 

Creating multiple objects of a class, which serves the same functionality is wastage of memory and time.

Recommendation 

Follow Singleton Design Pattern, which allows the application to create only one object of a Java class and use it for multiple times on each JVM in order to minimize the memory wastage and to increase the performance. The class following such design pattern is called Singleton java class.

Definition 

Singleton java class is a java class, which allows us to create only one object per JVM.

In some cases, it is important to have only one instance for a class. For instance, in a framework, there ought to be just a single window director (or just a record framework or just a print spooler). Generally, Singletons are utilized for concentrated administration of inner or outer assets and they give a worldwide purpose of access to themselves.

The Singleton java class is utilized to typify the formation of an object keeping in mind the end goal to keep up power over it. This not only ensures only one object is created but also allows lazy instantiation i.e the instantiation of the object can be delayed until it is actually needed. This is particularly valuable if the constructor needs to play out an expensive activity, for example, getting to a remote database.

The intent of Singleton Design Pattern 

  •  Guarantee that just a single instance of a class is created.
  •  Allows a global point of access to the object.


Note 

For a normal java class, if programmer or container is creating only one object, even though that class allows to create multiple instances then that java class isn't singleton java class. As per this, at that point, a java class of servlet program isn't singleton java class. It is an ordinary java class for which servlet container creates just a single instance.

 

Applicability of Singleton Design Pattern 

  • The Singleton Design Pattern should be used, when there must be exactly one instance of a class, and when it must be accessible to clients from a global access point.
  •  If multiple applications of a project that are running from a single JVM, wants to work with objects of java class having the same information then it is recommended to create that java class as Singleton java class. With the goal that just a single instance will be allowed to create for that class and we can use that object for multiple times in multiple applications.

   Example 

  •    In the log4j environment, the logger class is given as Singleton java class.
  •    java.lang.Runtime class is Singleton java class.
  •    java.awt.Desktop is Singleton java class

About Author

Author Image
Amarjeet Kumar

He is working as Associate Consultant-Development on BlockChain Project.

Request for Proposal

Name is required

Comment is required

Sending message..