免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 9180 | 回复: 3

[C++] 单例模式中是否可以存在继承 [复制链接]

论坛徽章:
0
发表于 2015-01-10 20:18 |显示全部楼层
本帖最后由 netaha 于 2015-01-10 21:29 编辑

问题背景是这样的,原先只有一个Child类,实现单例模式如下:
typedef enum EState{
        Start,
        Stop,
        End
}EState;

class Child
{
public:
     static Child& GetInstance()
         {
                 static Child instance;
                 return instance;
         }
         string EStateAsText(EState state)
        {
                return EStateTotxt[state];
        }
private:

    std::map<EState,string> EStateTotxt;
   
   ~Child(){};
    Child()
        {       
            EStateTotxt[Start] = "Start";
            EStateTotxt[Stop] = "Stop";
            EStateTotxt[End] = "End";
        };
};

// call
Child::GetInstance().EStateAsText((EState)1)


目前有一个变动,需要一个Base类,Child当中的部分数据需要移动到Base当中,Base类大概功能和之前的Child类似,
两者的区别只是,Base类会放在一些通用的code当中来使用,而Child类会放在一些特殊化的code当中使用,这里Child类要继承Base类。
C++新手,请问Base类能做成单例模式不?
还是直接将Base类里面的数据成员直接做成类成员,在通用的code当中直接通过Base类去引用,在一些特殊化的code当中保留原先不变,仍使用Child单例模式,
Child::GetInstance().EStateAsText((EState)1)这样进行引用。



补充:



还请多多指教,thanks

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
发表于 2015-01-11 07:04 |显示全部楼层
singleton 最好跟具体的类实例是正交的,不直接侵入到实例类的代码中去,比如可以考虑这样的方式设计一个 singleton
  1.     template< typename T >
  2.     struct singleton
  3.     {
  4.             typedef T value_type;
  5.             typedef singleton self_type;

  6.         private:
  7.             struct constuctor
  8.             {
  9.                 constuctor() { self_type::instance(); }
  10.                 inline void null_action() const { }
  11.             };

  12.             static constuctor constuctor_;

  13.         public:
  14.             static value_type& instance()
  15.             {
  16.                 static value_type instance_;
  17.                 constuctor_.null_action();
  18.                 return instance_;
  19.             }

  20.         private:
  21.             singleton( const self_type& );
  22.             self_type& operator = ( const self_type& );
  23.             singleton();
  24.             singleton( self_type&& );
  25.             self_type&( self_type&& );
  26.     };

  27.     template<typename T>
  28.     typename singleton<T>::constuctor singleton<T>::constuctor_;  
复制代码
然后直接组合使用

  1. struct base;
  2. struct child;
  3. auto& the_base_instance = singleton<base>::instance();
  4. auto& the_chile_instance = singleton<child>::instance();
复制代码

论坛徽章:
4
水瓶座
日期:2013-09-06 12:27:30摩羯座
日期:2013-09-28 14:07:46处女座
日期:2013-10-24 14:25:01酉鸡
日期:2014-04-07 11:54:15
发表于 2015-01-12 20:02 |显示全部楼层
最好这样保证单例初始化线程安全:

template <class T>
class Singleton {
public:
        static T& Instance() {
                pthread_once(InitObj, once_);
                return GetObj();
        }
private:
        static void InitObj() { GetObj(); }
        static T& GetObj() { T obj; return obj; }
        static pthread_once_t once_;
};

论坛徽章:
0
发表于 2015-01-14 19:16 |显示全部楼层
回复 2# lost_templar

回复 3# linux_c_py_php



多谢大侠的指点,目前采取的方案,是

class Base
{
public:
     static Base& GetBaseInstance()
         {
                 static Base instance;
                 return instance;
         }

.....
}


    class Child : public Base
{
public:
     static Child& GetInstance()
         {
                 static Child instance;
                 return instance;
         }
..........
}


就是在code中会有两个Base 类的instance,因为要复用Base类的数据成员,使用模板的话,还是会建立两个不同的类~~~  单例里面继承貌似是被禁止的,
所以暂定这种方案,也就两个instance,没什么影响。
thanks
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP