设计模式--单例模式
2016-03-28 20:09
218 查看
单例模式:Singleton的作用是保证在应用程序中,一个类Class只有一个实例存在。并提供全局访问。
例子:(java)
public class LazySingleton
{
private static LazySingleton newInstance = null;
private LazySingleton ()
{
}
public static synchronized LazySingleton getInstance ()
{
if (newInstance == null)
{
newInstance = new LazySingleton ();
}
return newInstance;
}
}
singleton限制了实例个数,有利于gc的回收。
例子(c++):
class ObStatSingleton
{
public:
static void init(ObEventManager *mgr);
static ObEventManager *get_instance()
{
return mgr_;
}
private:
ObStatSingleton() {}
DISALLOW_COPY_AND_ASSIGN(ObStatSingleton);
private:
static ObEventManager *mgr_;
};
例子:(java)
public class LazySingleton
{
private static LazySingleton newInstance = null;
private LazySingleton ()
{
}
public static synchronized LazySingleton getInstance ()
{
if (newInstance == null)
{
newInstance = new LazySingleton ();
}
return newInstance;
}
}
singleton限制了实例个数,有利于gc的回收。
例子(c++):
class ObStatSingleton
{
public:
static void init(ObEventManager *mgr);
static ObEventManager *get_instance()
{
return mgr_;
}
private:
ObStatSingleton() {}
DISALLOW_COPY_AND_ASSIGN(ObStatSingleton);
private:
static ObEventManager *mgr_;
};
相关文章推荐
- PropertyChangeListener简单理解
- 什么是设计模式
- 设计模式之创建型模式 - 特别的变量问题
- 七、设计模式——装饰模式
- 设计模式总结
- 设计模式之创建型模式
- 浅谈设计模式的学习
- Ruby设计模式编程之适配器模式实战攻略
- 实例讲解Ruby使用设计模式中的装饰器模式的方法
- 设计模式中的模板方法模式在Ruby中的应用实例两则
- Ruby设计模式编程中对外观模式的应用实例分析
- 实例解析Ruby设计模式编程中Strategy策略模式的使用
- Ruby中使用设计模式中的简单工厂模式和工厂方法模式
- Ruby使用设计模式中的代理模式与装饰模式的代码实例
- 详解组合模式的结构及其在Ruby设计模式编程中的运用
- C#编程中使用设计模式中的原型模式的实例讲解
- 使用设计模式中的工厂方法模式进行C#编程的示例讲解
- 实例解析C#设计模式编程中简单工厂模式的使用
- 详解C#设计模式编程中生成器模式的使用
- 深入解析C#设计模式编程中对建造者模式的运用