Singleton design pattern implementation

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

I’ve seen multiple articles written on singleton design pattern implementation and they are very informative. Many talk about implementing singleton design pattern in java with multiple ways. Here we will see the best way to do it and how it can be used in java EE environment.

Singleton Design Pattern Implementation:

Java 1.5 was released almost 12 years ago which introduced concept of enums in java. We will directly use enum singleton design pattern implementation instead of other ways which were preferred before java 1.5.

Enumerations in Java:

Enums are special types in java which also are fully fledged classes but they provide additional advantages over classic classes.

  • Enum types are singleton by nature
  • Object creation is managed by JVM itself.
  • Enums are highly recommended to be used for singleton design pattern implementation in Effective Java by Joshua Bloch.

Definition:

As defined in Gang of Four Design Patterns book,

The singleton design pattern ensures that class has only one instance and provides a global point of access to it.

Uses cases of Singletons:

  • Objects acting as device drivers, e.g. a printer driver instance.
  • access shared data across whole web application.
  • creating cache objects
  • Creating a logger instance as only one is required

Implementation:

we are going to use enums for singleton design pattern implementation.

Creating a singleton instance is as simple as that.

  • We have created an enum, Singleton,
  • INSTANCE is of type Singleton
  • It is created as soon as INSTANCE is accessed for the first time.
  • Singleton, has a private constructor by default, we can define the same and write some code in constructor to test the same.
  • The constructor will get invoked only once.

Testing Singleton Design Pattern Implementation:

Singleton Enum:

SingletonDemo.java:

The output is as follows.

This shows that singleton design pattern implementation using enum calls constructor only once even though we are accessing the INSTANCE multiple times.

Implementation in Java EE environment:

Java EE provides very simple way to implement singletons. You can simply mark any bean with @Singleton annotation. Also, the task of concurrency can be delegated to container by marking it as @ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)

Thats it, now LoggerResource class will have only one instance in java EE environment and concurrency will be handled by container itself

I hope the article helped understand singleton design pattern implementation in java SE and java EE environments

Share Button

2 comments for “Singleton design pattern implementation

  1. Pranaya
    June 27, 2016 at 1:20 am

    Usage of
    @ConcurrencyManagement(ConcurrencyManagementType.CONTAINER) is simply great.

    • June 27, 2016 at 7:42 am

      Thank you Pranaya, any feedback is greatly appreciated. I have started design patterns articles and will continue to contribute. Your constructive criticism will help me improve greatly.

Leave a Reply

Your email address will not be published. Required fields are marked *