单例模式的实现


 1.利用枚举类型实现(单多线程均可)

[java]  view plain copy
  1. public enum SingletonWithEnum {  
  2.     /** 
  3.      * @author boker 
  4.      */  
  5.     instance;  
  6.     public static SingletonWithEnum getInstance() {  
  7.           
  8.         return instance;  
  9.     }  
  10. }  

原理是:枚举类型当只有一个成员时,就是一个最简单的单例模式实现方式。
Effective Java作者Josh Bloch 提倡这种方式,它不仅能避免多线程同步问题,而且还能防止反序列化重新创建新的对象,但很少有人这么用,或许是应为enum在jdk1.5之后才出来,大家都已经用习惯用常规的设计模式实现了。
枚举单例模式实例:
package zqd.demo;


public class Easy {
	
	public static void main(String[] args)  {
		MyThread t1 = new MyThread();
		MyThread t2 = new MyThread();
		MyThread t3 = new MyThread();
		t1.start();
		t2.start();
		t3.start();
	}
}
class MyThread extends Thread{
	@Override
	public void run() {
		super.run();
		for (int i = 0; i < 5; i++) {
			System.out.println(MyObject.objectFactory.getObject().hashCode());
		}
	}
}
enum MyObject{	
	
	objectFactory;
	private Object object;
	private MyObject(){
		object = new Object();
	}
	public Object getObject(){
		return object;
	}
}
输出:
37621924
37621924
37621924
37621924
37621924
37621924
37621924
37621924
37621924
37621924
37621924
37621924
37621924
37621924
37621924
可见是一样的,是线程安全的


    2.单线程实现(非线程安全,不适用与多线程)

	   1> 定义一个类(该类最好定义成final类型的可以防止被继承)
	2>声明一个private的该类 类型的静态实例成员instance
	3>实现一个private的构造函数
	4>实现一个public静态方法getInstance(),在其中调用私有构造函数创建该类的一个实例,返回给调用者
代码实现如下:

[java]  view plain copy
  1. public class Singleton {  
  2.   
  3.     /** 
  4.      * @author boker 
  5.      */  
  6.     private static Singleton instance;  
  7.   
  8.     private Singleton() {  
  9.   
  10.     }  
  11.   
  12.     public static Singleton getInstance() {  
  13.         if (instance == null) {  
  14.             instance = new Singleton();  
  15.         } 
  16.         return instance;  
  17.     }  
  18. }  

    3.线程安全的实现方式(适用于多线程,但效率低)

[java]  view plain copy
  1. public class Singleton3 {  
  2.   
  3.     /** 
  4.      * @author boker 
  5.      */  
  6.     private static Singleton3 instance = null;  
  7.   
  8.     private Singleton3() {}  
  9.   
  10.     public static synchronized Singleton3 getInstance() {  
  11.           
  12.         return instance==null?instance=new Singleton3():instance;  
  13.     }  
  14. }  
    4.双重校验锁( 适用于jdk1.5之后)

[java]  view plain copy
  1. public class Singleton4 {  
  2.   
  3.     /** 
  4.      * @author boker 
  5.      */  
  6.     private volatile static Singleton4 instance;  
  7.   
  8.     private Singleton4() {}  
  9.   
  10.     public static Singleton4 getInstance() {  
  11.         if (instance==null) {  
  12.             synchronized (Singleton4.class) {  
  13.                 if (instance==null) {  
  14.                       
  15.                     instance = new Singleton4();  
  16.                 }  
  17.             }  
  18.         }   
  19.         return instance;  
  20.     }  

  21. 5、静态内置类实现
  22. //静态内置类实现
  23. public class Easy {

    public static void main(String[] args) throws ParseException {
    MyThread t1 = new MyThread();
    MyThread t2 = new MyThread();
    MyThread t3 = new MyThread();
    t1.start();
    t2.start();
    t3.start();
    }
    }
    class MyThread extends Thread{
    @Override
    public void run() {
    super.run();
    System.out.println(MyObject.getInstance().hashCode());
    }
    }
    class MyObject{
    private static class MyObjectHandler{
    private static MyObject myObject = new MyObject();
    }
    private MyObject(){

    }
    public static MyObject getInstance(){
    return MyObjectHandler.myObject;
    }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值