- 工作半年,今天第一次把简单的单态模式使用到了工作之中
- (vc 2009)
- #include <stdafx.h>
- #include <iostream>
- #include <cstdlib>
- #include <boost/scoped_ptr.hpp>
- using namespace std;
- using namespace boost;
- template <class T>
- class Singleton
- {
- public:
- static inline T* instance();
- private:
- Singleton(void){}
- ~Singleton(void){}
- Singleton(const Singleton&){}
- Singleton & operator= (const Singleton &){}
- static scoped_ptr<T> _instance;
- };
- template <class T>
- boost::scoped_ptr<T> Singleton<T>::_instance;
- template <class T>
- inline T* Singleton<T>::instance()
- {
- if( 0== _instance.get())
- {
- _instance.reset ( new T);
- }
- return _instance.get();
- }
- #define DEFINE_SINGLETON_CLASS(type) /
- friend scoped_ptr<type>;/
- friend Singleton<type>;
- class MyClass
- {
- public:
- void Run()
- {
- cout<<"ni hao"<<endl;
- }
- virtual ~MyClass(void)
- {
- }
- private:
- MyClass(void)
- {
- }
- DEFINE_SINGLETON_CLASS(MyClass);
- };
- typedef Singleton<MyClass> SSManger;
- int main()
- {
- SSManger::instance()->Run();
- return 1;
- }
我想,对于基本的单态框架,最好还是设计为一个单独的模块微妙
在以后的使用过过程中就可以直接加上去
上面的代码由一篇网络网站改造而成
核动力机器人
核动力爬虫
核动力王者
天空未留下鸟儿的痕迹,但我已飞过
2009.01.09