Singleton mode: (Singleton mode) The entire class can only generate one object
Three classic design schemes of singleton mode:
1. Delayed loading , also known as lazy man mode, will create objects when needed;
2. Greedy loading , also known as hungry man mode, objects are created before the program is executed;
3. Double lock mode , delayed loading Upgraded version, lock thread safety, create objects when needed;
Features of singleton mode:
1. Shield the interface of the generated object;
2. Provide an interface in the class to generate a unique object;
(1. Cannot return in the way of class type
(2. Interface is a static function)
1. Delayed loading
class SingleTon
{
public:
static SingleTon* getInstance()
{
if (psing == NULL)
{
psing = new SingleTon();
}
}
return psing;
}
private:
SingleTon(){
}
SingleTon(const SingleTon&);
static SingleTon* psing;
};
SingleTon* SingleTon::psing = NULL;
Test sample:
SingleTon* psing1 = SingleTon::getInstance();
SingleTon* psing2 = SingleTon::getInstance();
SingleTon* psing3 = SingleTon::getInstance();
SingleTon* psing4 = SingleTon::getInstance();
Debugging results:
bug:
Delayed loading onlyinstance equals NULLIf you are in a multi-threaded environment, another thread executes to the same stage when the object is not created, and the program will have problems. So we can give the programLock;
class SingleTon
{
public:
static SingleTon* getInstance()
{
lock();
if (psing == NULL)
{
psing = new SingleTon();
}
unlock();
}
return psing;
}
private:
SingleTon(){
}
SingleTon(const SingleTon&);
static SingleTon* psing;
};
SingleTon* SingleTon::psing = NULL;
As you can see, the above program performs lock() and unlock() operations on both sides of if(psing == NULL)
Why is there a lock and unlock operation?
The method adopted to prevent dirty data and errors when multiple threads access shared resources. When multiple threads access the same database, the database is a shared resource, so the database operations must be locked. The procedure is the same.
Example: 1 toilet cannot be used by multiple people, enter the toilet and lock it, and unlock it after use.
After the above program is created, the object will be locked and unlocked repeatedly, occupying memory and reducing efficiency.
Therefore, based on the above procedure, aDouble lock mechanism
Two, double lock mechanism
class SingleTon
{
public:
static SingleTon* getInstance()
{
if (psing == NULL)//没有命中 对象已经生成
{
lock();
if (psing == NULL)
{
psing = new SingleTon();
}
unlock();
}
return psing;
}
private:
SingleTon(){
}
SingleTon(const SingleTon&);
static SingleTon* psing;
};
SingleTon* SingleTon::psing = NULL;
Three, greedy loading
class SingleTon
{
public:
static SingleTon* getInstance()
{
return psing;
}
private:
SingleTon(){
}
SingleTon(const SingleTon&);
static SingleTon* psing;
};
SingleTon* SingleTon::psing = new SingleTon();
After debugging, it can be seen that no matter how to call the psing method, only one instance is generated, which realizes the singleton mode in C++.