设计模式--SINGLETON
2007-12-26 17:15
92 查看
多种因素,决定在现在的项目中尝试使用标准设计模式来规范自己写的代码,兼做学习
![](/Editor/FCKeditor/editor/images/smiley/msn/tounge_smile.gif)
SINGLETON—俺有6个漂亮的老婆,她们的老公都是我,我就是我们家里的老公
Sigleton,她们只要说道“老公”,都是指的同一个人,那就是我(刚才做了个梦啦,哪
有这么好的事)
单例模式:单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个
实例单例模式。单例模式只应在有真正的“单一实例”的需求时才可使用。
-----------以上引用网络关于设计模式的经典文章
默写一遍代码
class Singleton{
public:
~Singleton()//added myself,you can have you own way to manage it;
static Singleton* instance();
protected:
Singleton();//you can not get a instance by the direct way,or there will be an error
private:
static Singleton* _instance;
}
使用的时候
Singleton* Singleton::instance =0;
Singleton* Singleton::instance(){
if(_instance == NULL)
return new Singleton();
}
在结构上Singleton 很简单,没有什么好多说的。GOF上还扩充了,如何维护一类Singleton,只选用其中的一个,也不麻烦,就不多说,static 关键字。类的static成员变量在整个类实例类共享,存储在特定的地方。没有文件域的限制。
![](/Editor/FCKeditor/editor/images/smiley/msn/tounge_smile.gif)
SINGLETON—俺有6个漂亮的老婆,她们的老公都是我,我就是我们家里的老公
Sigleton,她们只要说道“老公”,都是指的同一个人,那就是我(刚才做了个梦啦,哪
有这么好的事)
单例模式:单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个
实例单例模式。单例模式只应在有真正的“单一实例”的需求时才可使用。
-----------以上引用网络关于设计模式的经典文章
默写一遍代码
class Singleton{
public:
~Singleton()//added myself,you can have you own way to manage it;
static Singleton* instance();
protected:
Singleton();//you can not get a instance by the direct way,or there will be an error
private:
static Singleton* _instance;
}
使用的时候
Singleton* Singleton::instance =0;
Singleton* Singleton::instance(){
if(_instance == NULL)
return new Singleton();
}
在结构上Singleton 很简单,没有什么好多说的。GOF上还扩充了,如何维护一类Singleton,只选用其中的一个,也不麻烦,就不多说,static 关键字。类的static成员变量在整个类实例类共享,存储在特定的地方。没有文件域的限制。
相关文章推荐
- 设计模式——Singleton单例模式
- Singleton设计模式活学活用: 请求单一 vs 线程单一
- 设计模式学习1 Singleton
- PHP设计模式----单例模式(singleton)
- 设计模式之单件(Singleton)的应用
- 设计模式之单例模式(Singleton)
- 单例(Singleton)设计模式
- 设计模式(Design Pattern) —— 单例模式(Singleton) (一)
- 26种设计模式之Singleton(单例模式)
- 设计模式之Singleton模式
- 设计模式:Singleton模式
- 在Java中应用设计模式--Singleton
- Java设计模式—singleton 单例
- 深入浅出单实例Singleton设计模式
- [设计模式]Singleton - 单例模式
- 设计模式学习笔记(一)——Singleton
- Java设计模式透析之 —— 单例(Singleton)
- Singleton 设计模式
- 设计模式之Singleton(单态)
- 【设计模式学习笔记】Singleton单件模式