单例模式与多例模式
来源:互联网 发布:骇客帝国软件 编辑:程序博客网 时间:2024/05/16 10:12
单例模式
1、单例模式的特点:
单例类只能有一个实例。
单例类必须自己创建自己的唯一的实例。
单例类必须给所有其他对象提供这一实例。
2、饿汉式单例类:
public classEagerSingleton {
private EagerSingleton() { }
public static EagerSingleton getInstance(){
return m_instance;
}
private static final EagerSingletonm_instance = new EagerSingleton();
}
3、懒汉式单例类:
public classLazySingleton
{
private LazySingleton() { }
synchronized publicstatic LazySingleton getInstance()
{
if (m_instance == null)
{
m_instance = new LazySingleton();
}
return m_instance;
}
private static LazySingleton m_instance = null;
}
单从资源利用效率角度来讲,饿汉式单例类比懒汉式单例类稍差些;从速度和反应时间角度来讲,则比懒汉式单例类稍好些。然而,懒汉式单例类在实例化时,必须处理好在多个线程同时首次引用此类时的访问限制问题。
饿汉式单例类和懒汉式单例类的构造函数是私有的,不能被继承。
4、登记式单例类
克服饿汉式单例类及懒汉式单例类均不可继承的缺点而设计的。
publicclass RegSingleton {
protected RegSingleton() {}
staticpublic RegSingletongetInstance(String name)
{
if (name ==null)
{
name = "com.javapatterns.singleton.demos.RegSingleton";
}
System.out.println("From RegSingleton: requesting for " + name );
if (m_registry.get(name) ==null)
{
try
{
m_registry.put( name, Class.forName(name).newInstance()) ;
}
catch(ClassNotFoundExceptione)
{
System.out.println("Class " + name +" is notfound.");
}
catch(InstantiationExceptione)
{
System.out.println("Class " + name +" can not beinstantiated.");
}
catch(IllegalAccessExceptione)
{
System.out.println("Class " + name +" can not beaccessed.");
}
}
return (RegSingleton) (m_registry.get(name) );
}
staticprivateHashMap m_registry =newHashMap();
static
{
RegSingleton x = new RegSingleton();
m_registry.put(x.getClass().getName() , x);
}
public String about()
{
return"Hello, I am RegSingleton.";
}
}
登记式单例类的子类:
publicclass RegSingletonChildextends RegSingleton
{
public RegSingletonChild() {}
staticpublicRegSingletonChild getInstance()
{
return (RegSingletonChild)RegSingleton.getInstance("com.javapatterns.singleton.demos.RegSingletonChild" );
}
public String about()
{
return"Hello, I am RegSingletonChild.";
}
}
5、有状态的单例类与没有状态的单例类:
在任何使用了EJB、RMI、JINI技术的分散式系统中,应当避免使用有状态的单例模式。
多例模式
1、多例模式的特点:
多例类可有多个实例。
多例类必须自己创建、管理自己的实例,并向外界提供自己的实例。
2、多例类:
publicclass Die
{
privatestatic Diedie1 =new Die();
privatestatic Diedie2 =new Die();
private Die()
{
}
publicstatic Die getInstance(int whichOne)
{
if (whichOne == 1)
{
returndie1;
}
else
{
returndie2;
}
}
publicsynchronizedint dice()
{
Date d = new Date();
Random r = new Random( d.getTime() );
int value = r.nextInt();
value = Math.abs(value);
value = value % 6;
value += 1;
System.out.println(value);
return value;
}
}
- 单例模式与多例模式
- 单例模式与多例模式
- 单例模式与多例模式
- [设计模式] 多例模式与单例模式区别
- java与模式(1)单例模式
- 单例模式与双重检测模式
- 单例模式与工厂模式
- 单例模式与工厂模式
- PHP单例模式与工厂模式
- PHP (单例模式与工厂模式)
- 《JAVA与模式》之单例模式
- 享元模式与单例模式
- 《JAVA与模式》之单例模式
- 《JAVA与模式》之单例模式
- 语言与模式-01单例模式
- 《JAVA与模式》之单例模式
- 《JAVA与模式》之单例模式
- 《JAVA与模式》之单例模式
- svn同步和导入导出
- 获取uiview的uiviewcontroller
- jQuery 使用大全
- 借助Spy++向指定的窗口发送消息—2
- 国嵌视频学习1-3嵌入式linux概述
- 单例模式与多例模式
- setpwet()函数
- mysql导出特定的数据表结构及数据
- Android AudioManager获取媒体焦点
- 使用Doxygen生成代码文档
- Hibernate通用多条件不定参数查询
- 高效程序员秘籍(7):使用Diagram Designer快速绘制流程图、UML等图形
- ios 应用上传发布流程
- GoldenGate 单向DDL同步