Singleton Design Pattern
1. What is the Singleton Design Pattern?
The Singleton Method or Singleton Design Pattern is one of the simplest design patterns. It ensures a class only has one instance and provides a global point of access to it.
2. Singleton Design Pattern Principles
The principles of the Singleton Pattern include:
3. When to Use Singleton Method Design Pattern?
Consider using the Singleton pattern in the following scenarios:
4. Initialization Types of Singleton
Singleton classes can be instantiated using two methods:
5. Key Components of Singleton Method Design Pattern
1. Static Member
A static member ensures that memory is allocated only once, preserving the single instance of the Singleton class.
private static Singleton instance;
2. Private Constructor
Restricts external attempts to instantiate the Singleton class.
private Singleton() {
// Initialization code here
}
3. Static Factory Method
Provides a global access point to the Singleton object.
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
6. Implementation of Singleton Method Design Pattern
The implementation consists of a single class with the following structure:
class Singleton {
private static Singleton instance;
private Singleton() {
System.out.println("Singleton is Instantiated.");
}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
public void doSomething() {
System.out.println("Something is Done.");
}
}
public class Main {
public static void main(String[] args) {
Singleton.getInstance().doSomething();
}
}
Output:
Singleton is Instantiated.
Something is Done.
7. Different Ways to Implement Singleton Method Design Pattern
Method 1: Classic Implementation
class Singleton {
private static Singleton obj;
private Singleton() {}
public static Singleton getInstance() {
if (obj == null) {
obj = new Singleton();
}
return obj;
}
}
Drawback: Not thread-safe.
Method 2: Thread-Safe Singleton
class Singleton {
private static Singleton obj;
private Singleton() {}
public static synchronized Singleton getInstance() {
if (obj == null) {
obj = new Singleton();
}
return obj;
}
}
Drawback: Synchronized methods can decrease performance.
Method 3: Eager Initialization
class Singleton {
private static final Singleton obj = new Singleton();
private Singleton() {}
public static Singleton getInstance() {
return obj;
}
}
Advantage: Thread-safe by default.
Method 4: Double-Checked Locking
class Singleton {
private static volatile Singleton obj;
private Singleton() {}
public static Singleton getInstance() {
if (obj == null) {
synchronized (Singleton.class) {
if (obj == null) {
obj = new Singleton();
}
}
}
return obj;
}
}
Advantage: Efficient and thread-safe.
Method 5: Inner Class
class Singleton {
private Singleton() {}
private static class SingletonHelper {
private static final Singleton INSTANCE = new Singleton();
}
public static Singleton getInstance() {
return SingletonHelper.INSTANCE;
}
}
Advantage: Ensures thread safety and lazy initialization.
8. Use Cases for the Singleton Design Pattern
9. Advantages of the Singleton Design Pattern
10. Disadvantages of the Singleton Design Pattern
Backend Developer | PHP & Laravel
2 个月Useful tips ??