zoukankan      html  css  js  c++  java
  • C++单例模式案例

    单例模式要注意两点:

    1. 通过一个Instance方法,得到类型唯一的实例
    2. 屏蔽掉外部创建类型实例的方法

    有两种实现思路:

    1. 在Instance函数内部使用static声明变量,使其具有唯一实例
    2. 在cpp文件中定义全局static指针,并在Instance函数初始化一次

    对于屏蔽外部的实现,只需将构造函数和析构函数声明成私有的即可(友元什么的特殊情况就不考虑了)。

    思路1的代码比较简单,如下:

    // somemgr.hpp
    class SomeMgr {
    public:
        static SomeMgr& Instance();
    
        int GetSomeValue() const { return some_value_; }
    private:
        SomeMgr() : some_value_(42) {}
        ~SomeMgr() { std::cout << "~SomeMgr()\n"; }
    
        int some_value_;
    };
    
    // somemgr.cpp
    #include "somemgr.hpp"
    SomeMgr& SomeMgr::Instance()
    {
        static SomeMgr inst;
        return inst;
    }
    

    思路2略为复杂,但由于是用指针维护单例,所以更加灵活,比如可以提供一个ReInit方法,来重新创建一个新的单例。

    // somemgr.hpp
    class SomeMgr {
    public:
        static SomeMgr* Instance();
        bool ReInit();      // 重新创建单例对象, 并执行 Init
        
         // user implements below
        bool Init();
        int GetSomeValue() const { return some_value_; }
    
    private:
        SomeMgr() : some_value_(0) {}
        ~SomeMgr() {}
    
        int some_value_;
    };
    
    #include "somemgr.hpp"
    
    static SomeMgr *some_mgr = nullptr;
    
    SomeMgr* SomeMgr::Instance()
    {
        if (!some_mgr) {
            some_mgr = new SomeMgr();
        }
    
        return some_mgr;
    }
    
    bool SomeMgr::ReInit()
    {
        SomeMgr *temp = new SomeMgr();
        if (!temp->Init()) {
            delete temp;
            return false;
        }
    
        if (some_mgr)
            delete some_mgr;
    
        some_mgr = temp;
        return true;
    }
    
    bool SomeMgr::Init()
    {
        some_value_ = 100;
        return true;
    }
    
  • 相关阅读:
    【pywin32总结】
    python 操作 office
    Python操作Word【批量生成文章】
    该怎样用几何画板绘制正五边形呢
    安装ChemOffice 15.1就是这么简单
    MathType编辑钢筋符号就是这么简单
    该如何将MathType公式粘贴到文档中
    修改Chem 3D模型的化学键属性的方法有哪些
    几何画板做圆柱的方法有哪些
    用几何画板如何实现抛物线左右平移
  • 原文地址:https://www.cnblogs.com/demon90s/p/15618712.html
Copyright © 2011-2022 走看看