行为型模式-单例模式

1.懒汉式

class Singleton{
private:
Singleton(){}
private:
static *Sinleton instance;
static *Garbo garbo;
public:
static *Singleton::getInstance(){
    if(instance=nullptr){
    instance = new Sinleton();
    }
return instance;
}
class Garbo{
    ~Garbo(){
    if(instance!=nullptr){
    delete instance;
    }
    }
}
};
Sinleton* Sinleton::instance = nullptr;

 懒汉式:使用时才实例化对象,单线程没问题,多线程不可靠

2.饿汉式:

class Singleton{
private:
Singleton(){}
private:
static *Sigleton instance;
public:
static *Sinleton::getInstance(){
    return instance;
  }
};
Sinleton *Singleton::instance = new Singleton();

你可能感兴趣的:(设计模式,单例模式)