[转]【创建型】 之 单件模式
2010-12-30 21:44
246 查看
/*********************************************** * 【创建型】 之 单件模式 * * *摘自c++ 编程思想 第2卷 384页 2010-12-30 ************************************************/ #include <iostream> using namespace std; class Singleton { private: static Singleton s; int i; Singleton (int x):i(x) { }; Singleton& operator=(Singleton&); //disabled Singleton (const Singleton&); //disabled public: static Singleton &instance(){ return s; } int getValue(){ return i; } void setValue(int x){ i =x; } } Singleton::s(47); int main(){ Singleton& s = Singleton::instance(); cout <<s.getValue()<<endl; Singleton& s2 = Singleton::instance(); s2.setValue(9); cout << s.getValue()<<endl; }
相关文章推荐
- 设计模式笔记(2)---单件模式(创建型)
- 四、单件模式--创建型模式
- C#面向对象设计模式纵横谈(2):Singleton 单件(创建型模式) 笔记
- 笔记2:Singleton 单件(创建型模式)
- 设计模式笔记(2)---单件模式(创建型)
- 设计模式单件(Singleton)---对象创建型模式
- (创建型模式)Singleton 单件模式
- 【设计模式基础】创建型模式 - 1 - 单件(Singleton)
- C#面向对象设计模式第二讲:Singleton Pattern单件模式(创建型模式)
- 2. Singleton单件(创建型模式)
- 创建型模式-单件(Singleton)
- 1.5 Singleton(单件) -- 对象创建型模式
- 单列模式(Singleton Pattern、单态模式、单件模式、对象创建型模式)
- 设计模式笔记(2)---单件模式(创建型)
- 设计模式(五):创建型模式—Singleton(单件)
- 单件模式学习总结(创建型)
- Singleton(单件)- 对象创建型模式
- 设计模式(二)单件模式Singleton(创建型)
- C#面向对象设计模式纵横谈2 Singleton单件创建型模式
- C#面向对象设计模式:Singleton 单件(创建型模式)