Skip to main content

Design Pattern – Singleton using Enum

Singleton Design Pattern is one of GOF (Gang of Four) design pattern. It comes under creational patterns. The main idea behind the Singleton is, create only one object of a specific class is ever created. There are couple of ways to implement the Singleton design pattern in Java with their own pros and cons. But the easiest way is using Enum in Java.

There are 2 type of initialization

Eager initialization – create an instance when initialize. It might be not required at that moment.
Lazy initialization – create an instance when required.

How to create it.

How to call it.

Joshua Bloch explained using an Enum in his Effective Java Reloaded talk at Google I/O 2008.

Pros :-

  • An Instance is thread-safe.
  • Easy to implement.

Cons :-

  • Not create an instance when required (Eager initialization)

 


Related Post

2272total visits.

RSS
Follow by Email
Facebook
Facebook
Google+
http://mydevgeek.com/design-pattern-singleton-using-enum
Twitter