Design Pattern (Part 01) — Singleton Pattern
Singleton pattern is one of the simplest design patterns in Java. This type of design pattern comes under a creational pattern as this pattern provides one of the best ways to create an object. Singleton object means creating only one object for the entire application. (per JVM). and this object is globally accessible within the application.
This pattern involves a single class that is responsible to create an object while making sure that only a single object gets created. This class provides a way to access its only object which can be accessed directly without the need to instantiate the object of the class.
The application needs one, and only one, an instance of an object. Additionally, lazy initialization and global access are necessary.
Make the class of the single instance object responsible for the creation, initialization, access, and enforcement. Declare the instance as a private static data member. Provide a public static member function that encapsulates all initialization code, and provides access to the instance.
The client calls the accessor function (using the class name and scope resolution operator) whenever a reference to the single instance is required.
- Controlled access to a single instance
- Reduced number of objects
- Allows refinement of operations and representations
- Considered as anti-pattern by many
- Misused to situations where a single instance of a class might not be required
There are several ways a singleton design pattern can be implemented. Each of these has its own benefits and limitations. We can implement a Singleton design pattern through the following ways:-
- Eager Initialization
- Eager Initialization with static block
- Lazy Initialization
- Thread Safe Lazy Initialization
- Double Check Lazy Initialization
- Bill Pugh Implementation
- Singleton with Single Instance Enum
Make the class of the single instance responsible for access and “initialization on first use”. The single instance is a private static attribute. The accessor function is a public static method.
Let’s create the basic application by using the singleton pattern.
- Define a private
staticattribute in the "single instance" class.
- Define a public
staticaccessor function in the class.
- Do “lazy initialization” (creation on first use) in the accessor function.
- Define all constructors to be
- Clients may only use the accessor function to manipulate the Singleton.
Manager class —
Application Class(Main class) —
From the above output it’s clear to us that even though we have created two separate objects, the “Manager” class only gave us the same instance.
If you see this example we create the Manager instance, first, we create a initialize class from the “Manager” and make it private and volatile. what it does to the objects is, it won't be allowed access or modification from the outside of the class. and state remained as same even in the multiple threads.
And the constructor [Manager()] of the class is set to private. because to avoid being creating objects. to make sure the object initialization is protected even via the reflection API extra checking should be done inside the constructor whether the object is initialized or not.
Design Patterns and Refactoring
Ensure a class has only one instance, and provide a global point of access to it. Encapsulated "just-in-time…