c++设计模式1:单态模板

  1. 工作半年,今天第一次把简单的单态模式使用到了工作之中
  2. (vc 2009)

  3. #include <stdafx.h>
  4. #include <iostream>
  5. #include <cstdlib>
  6. #include <boost/scoped_ptr.hpp>
  7. using namespace std;
  8. using namespace boost;
  9. template <class T>
  10. class Singleton
  11. {
  12. public:
  13.      static inline T* instance();
  14. private:
  15.      Singleton(void){}
  16.      ~Singleton(void){}
  17.      Singleton(const Singleton&){}
  18.      Singleton & operator= (const Singleton &){}
  19.      static scoped_ptr<T> _instance;
  20. };
  21. template <class T>
  22. boost::scoped_ptr<T> Singleton<T>::_instance;
  23. template <class T>
  24. inline T* Singleton<T>::instance()
  25. {
  26.      if( 0== _instance.get())
  27.      {
  28.          _instance.reset ( new T);
  29.      }
  30.      return _instance.get();
  31. }
  32. #define DEFINE_SINGLETON_CLASS(type) /
  33.               friend scoped_ptr<type>;/
  34.               friend Singleton<type>;
  35.  
  36. class MyClass
  37. {
  38. public:
  39.        void Run()
  40.        {
  41.              cout<<"ni hao"<<endl;
  42.        }      
  43.        virtual ~MyClass(void)
  44.        {
  45.        }
  46. private:
  47.        MyClass(void)
  48.        {
  49.        }
  50.  
  51.       DEFINE_SINGLETON_CLASS(MyClass);
  52. };
  53. typedef Singleton<MyClass> SSManger;
  54. int main()
  55. {
  56.     SSManger::instance()->Run();
  57.     return 1;    
  58. }

我想,对于基本的单态框架,最好还是设计为一个单独的模块微妙
在以后的使用过过程中就可以直接加上去

上面的代码由一篇网络网站改造而成


核动力机器人
核动力爬虫
核动力王者


天空未留下鸟儿的痕迹,但我已飞过
2009.01.09

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值