singleton java patterns code example
Example 1: java singleton implementation
public class SingletonClass {
private static final SingletonClass SINGLE_INSTANCE = new SingletonClass();
private SingletonClass() {}
public static SingletonClass getInstance() {
return SINGLE_INSTANCE;
}
}
Example 2: single design pattern
Singleton Design Pattern is basically limiting our class so that
whoever is using that class can only create 1 instance from that class.
I create a private constructor which limits access to the instance of the class.
Than I create a getter method where we specify how to create&use the instance.
- I am using JAVA Encapsulation OOP concept.