Singleton Pattern, also known as Singleton Pattern, is a common software design pattern.When applying this pattern, the class of the singleton object must ensure that only one instance exists.
Singleton Pattern Thought
A class can return an object, a reference, and a method (which must be static) to get the instance, while only a single object is created.
Characteristic
- Singleton class - > can only have one instance.
- A singleton class must create its own unique instance.
- A singleton class must provide instances to all other objects.
Advantage
- There is only one instance where memory overhead is reduced, especially frequent creation and destruction.
- Avoid multiple occupations.
Be careful
- The getInstance() method requires synchronized lock (Singleton.class) to prevent multiple threads from entering simultaneously, causing instance s to be instantiated multiple times.
Java implementation
We will create a SingleObject class.The SingleObject class has its private constructor and a static instance of itself.
- Create Singleton class (SingleObject.java)
public class SingleObject {
//Create an object of SingleObject
private static SingleObject instance = new SingleObject();
//Make the constructor private so that the class is not instantiated
private SingleObject(){}
//Get the only available object
public static SingleObject getInstance(){
return instance;
}
public void showMessage(){
System.out.println("Hello World!");
}
}
- Get a unique object from the singleton class (SingletonPatternDemo.java)
public class SingletonPatternDemo {
public static void main(String[] args) {
//Illegal constructor
//Compile-time error: the constructor SingleObject() is not visible
//SingleObject object = new SingleObject();
//Get the only available object
SingleObject object = SingleObject.getInstance();
//display messages
object.showMessage();
}
}
Implementation
Lazy Mode_Thread Security
Lazy mode is characterized by delayed loading, such as configuration files, using a lazy approach.Load only when used.
public class Singleton {
private static Singleton instance;
private Singleton (){}
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
Bad Han Mode
When a class is loaded, it is initialized, wasting memory resources, and multithreaded synchronization is avoided based on the classloder mechanism.
public class Singleton {
private static Singleton instance = new Singleton();
private Singleton (){}
public static Singleton getInstance() {
return instance;
}
}
C++ Implementation_Thread Security
class Singleton //Lazy Man Mode
{
private: Singleton(){}
public:
static Singleton* p;
static Singleton* getInstance();
};
Singleton* Singleton::p = NULL;
Singleton* Singleton::getInstance();
{
if (NULL == p){
if (mtx.try_lock()){
p = new Singleton;
mtx.unlock();
}
}
return p;
}
class Singleton //Hungry Han Mode
{
private:
Singleton(){};
public:
static Singleton* p;
static Singleton* getInstance(){
return p;
}
};
Sing Singleton::p = new Singleton;