Back
Close

Design Pattern – Singleton using Enum

Damith
5,094 views

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

  1. Eager initialization – create an instance when initialize. It might be not required at that moment.
  2. Lazy initialization – create an instance when required.
How to create it.
package com.mydevgeek;
public enum Singleton {
INSTANCE;
private int count;
public void setCount(int count) {
this.count = count;
}
public void doProcess() {
System.out.println("Do high memory or CPU consume operation. count :-" + this.count);
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

How to call the singleton instance.

  Singleton.INSTANCE.doProcess();

This is the simple test case for verifying single instance use for each initialization.

@Test
public void singletonTest() {
  Singleton.INSTANCE.setCount(1);
  Singleton.INSTANCE.setCount(2);
  Singleton.INSTANCE.doProcess();
  Singleton.INSTANCE.doProcess();
  Singleton.INSTANCE.doProcess();
}

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)

Original post find it here :- http://mydevgeek.com/design-pattern-singleton-using-enum/

Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants