Singleton Design Pattern
The singleton pattern is a design pattern that prevents a class from creating several objects at the same time. It's nothing more than a method of defining a class. A class is defined in such a way that during the execution of a program or project, only one instance of the class is created. When only one instance of a class is required to control the action throughout the execution. Multiple instances of a singleton class should be avoided at all costs. Logging, driver objects, caching and thread pools, and database connections all require singleton classes.
Singleton Design Pattern Implementation
It should only have one instance: This is done by supplying a class instance from within the class. The use of outer classes or subclasses to construct the instance should be avoided. This is done by making the constructor private in Java, which prevents any class from accessing it and hence from instantiating it.
The instance should be accessible from anywhere in the world: Each class should be able to use a singleton class instance because it should be globally accessible. In Java, this is done by making the instance's access-specifier public.
Advantage
To assure only one and same instance of object every time.

0 Comments