首页 > 其他分享 >设计模式-单例模式

设计模式-单例模式

时间:2023-02-07 12:45:59浏览次数:34  
标签:EasySingleModle std 设计模式 单例 模式 instance SingleModelLazy include cout

简易单例模型

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

相关文章

  • 设计模式-工厂模式/抽象工厂模式
    工厂模式#include<iostream>#include<string>usingnamespacestd;classShape{public: virtualvoiddraw()=0;};classRectangle:publicShape{pub......
  • 【Appium_python】启动app,出现多次打开关闭导致失败问题,driver用单例模式(_new_)进行解
    运用多设备,启动app多次出现打开又关闭问题,查看后是多次对driver进行实例化,就用单例的模式进行解决。单例模式(SingletonPattern)目的就是保证一个类仅有一个实例,每一次执行......
  • 工厂模式-go语言实现
    一、理论知识工厂模式的作用就是用来创建对象,细分为三种:简单工厂、工厂方法、抽象工厂。1.1应用场景工厂模式一般用于对于不同的场景,需要创建不同的对象,但是这些对象实......
  • 单例模式
      一个类永远只能创建一个对象,例如任务管理器我们只要一个就可以解决问题了,这样可以节省内存空间。单例的实现方式很多:比如饿汉单例模式和懒汉单例模式....................
  • 搞懂设计模式——代理模式 + 原理分析
    作者:京东零售秦浩然引子举个栗子,众所周知,我们是可以在京东上购买机票的。但机票是航司提供的,我们本质上是代理销售而已。那为什么航司要让我们代理销售呢?我们又是如帮他做......
  • java合成模式之神奇的树结构
    目录什么是合成模式安全式合成模式抽象构件(Component)角色树叶构件(Leaf)角色树枝构件(Composite)角色使用透明式合成模式抽象构件(Component)角色树叶构件(......
  • 模板模式
    /*模板模式:解决某类事情的步骤有些是固定的,有些是会发生变化的,那么这时候我们可以为这类事情提供一个模板代码,从而提高效率。需求;编写一个计算程序运行时间的模板。模板......
  • 理解vuex -- vue的状态管理模式
    [b]vuex是什么?[/b]先引用vuex官网的话:[quote]Vuex是一个专为Vue.js应用程序开发的状态管理模式。它采用集中式存储管理应用的所有组件的状态,并以......
  • 设计模式-策略模式升级(服务定位器模式)
    设计模式-策略模式(服务定位器模式)前言:正文开始前先抛出一个问题,项目开发中如果让你设计支付模块,目前有支付宝、微信、各大银行的部分,你会如何设计支付这块的代码?在调用的......
  • vim的visual模式和列编辑
    有三种方式进入visual模式:1>在普通模式下输入v(小写),底部提示信息为VISUAL,编辑粒度为字符  通过方向键或者HJKL调整选择的字符范围。输入d,删除选中字符;输入y,复制当......