单例模式:Singleton Pattern
对象类型的创建只有一个实例,单例类必须自己创建自己的唯一实例。
优点:1、在内存里只有一个实例,减少了内存的开销,尤其是频繁的创建和销毁实例(比如管理学院首页页面缓存)。 2、避免对资源的多重占用(比如写文件操作)。
缺点:没有接口,不能继承,与单一职责原则冲突,一个类应该只关心内部逻辑,而不关心外面怎么样来实例化。
方式一:
[java]package com.proliu.designPatterns.single;
/**
- @author duliu
- 没有加锁,执行效率会提高, 类加载时就初始化,浪费内存。
- /
public class SingleObject {
private static SingleObject singleObject = new SingleObject();
public SingleObject(){
}
public static SingleObject getInstance(){
return singleObject;
}
public void outMsg(){
System.out.println(“Hello, This is Single Object.” + this.getClass().hashCode());
}
}[/java]
方式二:
[java]package com.proliu.designPatterns.single;
/**
- @author duliu
- 这种方式是最基本的实现方式,这种实现最大的问题就是不支持多线程。因为没有加锁 synchronized.
- /
public class SingleObjectTwo {
private static SingleObjectTwo singleObjectTwo ;
public SingleObjectTwo(){
}
public static SingleObjectTwo getInstance(){
if(singleObjectTwo == null){
singleObjectTwo = new SingleObjectTwo();
}
return singleObjectTwo;
}
public void outMsg(){
System.out.println(“Hello, This is Single Object Two.” + this.getClass().hashCode());
}
}[/java]
方式三:
[java]package com.proliu.designPatterns.single;
/**
*
- @author duliu
- 第一次调用才初始化,避免内存浪费, 必须加锁 synchronized 才能保证单例,但加锁会影响效率。
- /
public class SingleObjectThree {
private static SingleObjectThree singleObjectThree ;
public SingleObjectThree(){
}
/**
- 同步块大家都比较熟悉,通过 synchronized 关键字来实现,所有加上synchronized 和 块语句,
- 在多线程访问的时候,同一时刻只能有一个线程能够用synchronized 修饰的方法 或者 代码块。
- @return
- /
public static synchronized SingleObjectThree getInstance(){
if(singleObjectThree == null){
singleObjectThree = new SingleObjectThree();
}
return singleObjectThree;
}
public void outMsg(){
System.out.println(“Hello, This is Single Object Three.” + this.getClass().hashCode());
}
}[/java]
方式四:
[java]package com.proliu.designPatterns.single;
/**
- @author duliu
- 这种方式采用双锁机制,安全且在多线程情况下能保持高性能。
- /
public class SingleObjectFour {
/**
- 用volatile修饰的变量,线程在每次使用变量的时候,都会读取变量修改后的最的值
- /
private volatile static SingleObjectFour singleObjectFour ;
public SingleObjectFour(){
}
public static SingleObjectFour getInstance(){
if(singleObjectFour == null){
synchronized (SingleObjectFour.class){
if(singleObjectFour == null)
singleObjectFour = new SingleObjectFour();
}
}
return singleObjectFour ;
}
public void outMsg(){
System.out.println(“Hello, This is Single Object Four.” + this.getClass().hashCode());
}
}[/java]
方式五:
[java]package com.proliu.designPatterns.single;
/**
- @author duliu
- 通过静态内部类来实现类的实例化
- /
public class SingleObjectFive {
private static class SingleObjectHolder{
private static final SingleObjectFive INSTANCE = new SingleObjectFive();
}
public SingleObjectFive(){
}
public static final SingleObjectFive getInstance(){
return SingleObjectHolder.INSTANCE ;
}
public void outMsg(){
System.out.println(“Hello, This is Single Object Five.” + this.getClass().hashCode());
}
}[/java]
方式六:
[java]package com.proliu.designPatterns.single;
/**
- @author duliu
- 参与枚举模式
- /
public enum SingleObjectSix {
INSTANCE;
public void outMsg(){
System.out.println(“Hello, This is Single Object Six.” + this.getClass().hashCode());
}
}[/java]