简易单例模型
easysiglemodle.h
#ifndef EASYSIGLEMODLE_H
#define EASYSIGLEMODLE_H
#include <iostream>
using namespace std;
class EasySingleModle{
public:
static EasySingleModle& getInstance(){
static EasySingleModle easyInstance;
return easyInstance;
}
/*static EasySingleModle* getInstance(){
static EasySingleModle easyInstanceptr;
return &easyInstanceptr;
}*///can't avoid user to delete instance, would cause obj ahead of destroed导致对象被提前销毁
~EasySingleModle(){
cout<<"destruct EasySingleModle"<<endl;
}
private:
EasySingleModle(){
cout<<"construct EasySingleModle"<<endl;
}
EasySingleModle(const EasySingleModle&) = delete;
EasySingleModle & operator=(const EasySingleModle&) = delete;
};
#endif // EASYSIGLEMODLE_H
单例模式
singlemodel.h
#ifndef SINGLEMODEL_H
#define SINGLEMODEL_H
#include <iostream>
#include <string>
using namespace std;
class SingleModel {
private:
SingleModel(){ cout<<"constructor SingleModel"<<endl;}
SingleModel( SingleModel &)=delete;
SingleModel& operator=(const SingleModel&)=delete;
class InternalModel{// allow to destruct instance ptr
public:
~InternalModel(){
if (SingleModel::model != nullptr) {
cout<<"delete instance"<<endl;
delete SingleModel::model;
SingleModel::model = nullptr;
}
}
};
private:
static SingleModel* model;
static InternalModel internalModel;
public:
~SingleModel() {
cout << "destruct SingleModel" << endl;
}
static SingleModel* getInstance() {
if (model == nullptr) model = new SingleModel;
cout << "create new model" << endl;
return model;
}
};
SingleModel* SingleModel::model = nullptr;
SingleModel::InternalModel SingleModel::internalModel;
#endif // SINGLEMODEL_H
单例 饿汉
singlemodelhungery.h
#ifndef SINGLEMODELHUNGERY_H
#define SINGLEMODELHUNGERY_H
#include <iostream>
using namespace std;
class SingleModelHungery{
public:
static SingleModelHungery *getInstance(){
return instance;
}
~SingleModelHungery(){
cout<<"destruct SingleModleHungery"<<endl;
}
class InternalSingleModelHungery{
public:
~InternalSingleModelHungery(){
if(instance != NULL){
cout<<"delete instance from SingleModelHungery"<<endl;
delete instance;
instance = NULL;
}
}
};
private:
SingleModelHungery(){
cout<<"construct SingleModelHungery"<<endl;
}
SingleModelHungery(const SingleModelHungery&)=delete;
SingleModelHungery& operator=(const SingleModelHungery&)=delete;
static SingleModelHungery *instance;
static InternalSingleModelHungery internalSingleModelHungery;
};
SingleModelHungery *SingleModelHungery::instance = new SingleModelHungery();
SingleModelHungery::InternalSingleModelHungery SingleModelHungery::internalSingleModelHungery;
#endif // SINGLEMODELHUNGERY_H
单例 懒汉
singlemodellazy.h
#ifndef SINGLEMODELLAZY_H
#define SINGLEMODELLAZY_H
#include <iostream>
#include <memory>
#include <mutex>
using namespace std;
class SingleModelLazy{
public:
typedef std::shared_ptr<SingleModelLazy> Ptr;
//using Ptr = std::shared_ptr<SingleModelLazy>;
~SingleModelLazy(){
cout<<"destruct singleModelLzay"<<endl;
}
static Ptr getInstance(){
if(instance == NULL){
std::lock_guard<std::mutex> lock(m_mutex);
if(instance == NULL){
instance = shared_ptr<SingleModelLazy>(new SingleModelLazy);
}
}
return instance;
}
private:
SingleModelLazy(const SingleModelLazy&);
SingleModelLazy & operator=(const SingleModelLazy&);
SingleModelLazy(){
cout<<"construct SingleModelLazy"<<endl;
}
class InternalSingleModelLazy{
public:
~InternalSingleModelLazy(){
if(instance != NULL){
// cout<<"delet instance"<<endl;
// delete instance;
instance = NULL;
}
}
};
private:
static Ptr instance;
static InternalSingleModelLazy internalSingleModelLazy;
static std::mutex m_mutex;
};
SingleModelLazy::Ptr SingleModelLazy::instance = NULL;
SingleModelLazy::InternalSingleModelLazy SingleModelLazy::internalSingleModelLazy;
std::mutex SingleModelLazy::m_mutex;
#endif // SINGLEMODELLAZY_H
单例 模板
templatesinglemodle.h
#ifndef TEMPLATESINGLEMODLE_H
#define TEMPLATESINGLEMODLE_H
#include <iostream>
using namespace std;
template <typename T>
class Singleton{
public:
static T& getInstance(){
static T instance;
return instance;
}
virtual ~Singleton(){
cout<<"destruct Singleton"<<endl;
}
protected:
Singleton(){
cout<<"construct Singleton"<<endl;
}
private:
Singleton(const Singleton&) = delete;
Singleton& operator=(const Singleton&)=delete;
};
class DemoSingleton : public Singleton<DemoSingleton>
{
private:
friend class Singleton<DemoSingleton>;
public:
DemoSingleton(const DemoSingleton&) = delete;
DemoSingleton& operator=(const DemoSingleton&) = delete;
private:
//DemoSingleton()=default;
DemoSingleton(){
cout<<"construct DemoSingleton"<<endl;
}
};
#endif // TEMPLATESINGLEMODLE_H
main.cpp
#include <iostream>
#include "singlemodel.h"
#include "singlemodelhungery.h"
#include "singlemodellazy.h"
#include "easysiglemodle.h"
#include "templatesinglemodle.h"
using namespace std;
class SingleModel;
int main()
{
cout << "Hello World! 12" << endl;
SingleModel *SigModel1 = SingleModel::getInstance();
SingleModel *SigModel2 = SingleModel::getInstance();
//delete SigModel; SigModel = nullptr;//delete static ptr but delete will call destruct function ~ is will circular
SingleModelHungery *sinModelHunger1 = SingleModelHungery::getInstance();
SingleModelHungery *sinModelHunger2 = SingleModelHungery::getInstance();
SingleModelLazy::Ptr intance1 = SingleModelLazy::getInstance();
SingleModelLazy::Ptr intance2 = SingleModelLazy::getInstance();
EasySingleModle &easSigModl1= EasySingleModle::getInstance();
EasySingleModle &easSigModl2= EasySingleModle::getInstance();
DemoSingleton& demosig1 = DemoSingleton::getInstance();
DemoSingleton& demosig2 = DemoSingleton::getInstance();
return 0;
}
调试结果
construct SingleModelHungery
Hello World! 12
constructor SingleModel
create new model
create new model
construct SingleModelLazy
construct EasySingleModle
construct Singleton
construct DemoSingleton
destruct Singleton
destruct EasySingleModle
destruct singleModelLzay
delete instance from SingleModelHungery
destruct SingleModleHungery
delete instance
destruct SingleModel
标签:EasySingleModle,std,设计模式,单例,模式,instance,SingleModelLazy,include,cout
From: https://www.cnblogs.com/bell123/p/17098003.html