参考《设计模式的艺术软件开发人员内功修炼之道》-刘伟 著
实验目的
对于单例模式,有多种不同的实现方式,简单的情况只适用于单线程场景,对于多线程情况,可以分为饿汉模式,lazy模式;对于lazy模式,又可以细分为整个函数加锁,双重检查锁模式;最终实现一种静态内部类的方式,该方式利用了虚拟机内部同步功能,避免显示加锁。
实验代码
interface SingleInstance{
public default void printDividingLine(){
System.out.println("===========================");
}
public void printName();
public static SingleInstance getSingleInstance(){
return null;
}
}
class SimpleSingleInstance implements SingleInstance {
private static SimpleSingleInstance inst;
private SimpleSingleInstance(){}
public static SingleInstance getSingleInstance(){
if (inst == null)
inst = new SimpleSingleInstance();
return inst;
}
@Override
public void printName() {
// TODO Auto-generated method stub
if(inst == null )
System.out.print("inst is null ...");
else
System.out.print("inst is not null ...");
System.out.println("SimpleSingleInstance...");
}
}
class EagerSingleInstance implements SingleInstance{
private static final EagerSingleInstance inst = new EagerSingleInstance();
private EagerSingleInstance(){}
public static SingleInstance getSingleInstance(){
return inst;
}
@Override
public void printName() {
// TODO Auto-generated method stub
if(inst == null )
System.out.print("inst is null ...");
else
System.out.print("inst is not null ...");
System.out.println("EagerSingleInstance...");
}
}
class LazySingleInstance implements SingleInstance{
private static LazySingleInstance inst;
private LazySingleInstance(){}
public synchronized static SingleInstance getSingleInstance(){
if (inst == null)
inst = new LazySingleInstance();
return inst;
}
@Override
public void printName() {
// TODO Auto-generated method stub
if(inst == null )
System.out.print("inst is null ...");
else
System.out.print("inst is not null ...");
System.out.println("LazySingleInstance...");
}
}
class LazyDoubleCheckSingleInstance implements SingleInstance{
@Override
public void printName() {
// TODO Auto-generated method stub
if(inst == null )
System.out.print("inst is null ...");
else
System.out.print("inst is not null ...");
System.out.println("LazyDoubleCheckSingleInstance...");
}
private static LazyDoubleCheckSingleInstance inst;
private LazyDoubleCheckSingleInstance(){}
public static SingleInstance getSingleInstance(){
if(inst == null){
synchronized(LazyDoubleCheckSingleInstance.class){
if(inst == null)
inst = new LazyDoubleCheckSingleInstance();
}
}
return inst;
}
}
class FinalSingleInstance implements SingleInstance{
private FinalSingleInstance(){};
public static SingleInstance getSingleInstance(){
return InnerBuilder.inst;
}
static class InnerBuilder{
public final static FinalSingleInstance inst = new FinalSingleInstance();
}
@Override
public void printName() {
// TODO Auto-generated method stub
if(InnerBuilder.inst == null )
System.out.print("inst is null ...");
else
System.out.print("inst is not null ...");
System.out.println("FinalSingleInstance...");
}
}
public class SingleInstanceTest {
public static void main(String[] args){
SingleInstance simpleInst = SimpleSingleInstance.getSingleInstance();
simpleInst.printName();
simpleInst.printDividingLine();
SingleInstance eagerSingleInstance = EagerSingleInstance.getSingleInstance();
eagerSingleInstance.printName();
eagerSingleInstance.printDividingLine();
SingleInstance lazySingleInstance = LazySingleInstance.getSingleInstance();
lazySingleInstance.printName();
lazySingleInstance.printDividingLine();
SingleInstance lazyDoubleCheckSingleInstance = LazyDoubleCheckSingleInstance.getSingleInstance();
lazyDoubleCheckSingleInstance.printName();
lazyDoubleCheckSingleInstance.printDividingLine();
SingleInstance finalSingleInstance = FinalSingleInstance.getSingleInstance();
finalSingleInstance.printName();
finalSingleInstance.printDividingLine();
}
}
结果输出
inst is not null ...SimpleSingleInstance...
===========================
inst is not null ...EagerSingleInstance...
===========================
inst is not null ...LazySingleInstance...
===========================
inst is not null ...LazyDoubleCheckSingleInstance...
===========================
inst is not null ...FinalSingleInstance...
===========================
总结
- 饿汉模式在类加载时候就进行实例化
- 屏蔽类外访问时用private,如 inst , 构造函数
- 对外接口声明为public,如 getSingleInstance
- 类外无法访问构造函数,要取得类实例,只能通过public static 的函数访问 ,如getSingleInstance
- 静态内部类与外部类加载无必然联系,通过将类实例的实例化与静态内部类加载过程绑定,实现lazy模式的单例模式