码迷,mamicode.com
首页 > 其他好文 > 详细

设计模式(四) : 创建型模式--单例模式

时间:2014-06-03 13:48:06      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:des   c   style   class   blog   code   

单例模式的话,类图上来看是最简单的设计模式,就是一个类只能有一个自己的实例。

单例模式通常来说我们就有Lazy loading的和不是Lazy loading的。《java与模式》里面的关于这两种的类图,:

bubuko.com,布布扣bubuko.com,布布扣

可以看到一个是现开始就实例话的,这样的话不符合我们的lazy loading,还有一种是在getinstance方法里头去new的,这样的话会有线程安全的问题,我们提供了双重检查锁。

下面看示意代碼︰

1. 静态初始化:

bubuko.com,布布扣
package com.javadesignpattern.Singleton;

public class SingletonLazyLoading {
    
    private static SingletonLazyLoading instance = new SingletonLazyLoading();
    
    private SingletonLazyLoading(){
        
    }
    
    public static SingletonLazyLoading getInstance(){
        return instance;
    }
    
}
bubuko.com,布布扣

2.双重检察锁

bubuko.com,布布扣
package com.javadesignpattern.Singleton;

public class SingletonNonLazyLoading {
    
    private static volatile SingletonNonLazyLoading instance;
    
    private SingletonNonLazyLoading(){
        
    }
    
    public static SingletonNonLazyLoading getInstance(){
        synchronized(SingletonNonLazyLoading.class){
            if(instance == null){
                instance =  new SingletonNonLazyLoading();
            }
        }        
        return instance;
    }
    

}
bubuko.com,布布扣

3.静态内部类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.javadesignpattern.Singleton;
 
public class SingletonStaticInnerClass {
     
     
    private SingletonStaticInnerClass(){
         
    }
     
    static class InnerClass{
        private static SingletonStaticInnerClass instance = new SingletonStaticInnerClass();
    }
     
    public static SingletonStaticInnerClass getInstance(){
        return InnerClass.instance;
    }
 
}

 4. 枚举

bubuko.com,布布扣
package com.citi.designpattern.singleton;

public class SingletonEnum {
    
    public static void main(String[] args){
        System.out.println(Singleton.INSTANCE == Singleton.getInstance());
    }
    

}

enum Singleton { INSTANCE;
    
    public static Singleton getInstance(){
        return INSTANCE;
    }
}
bubuko.com,布布扣

测试代码:

bubuko.com,布布扣
package com.javadesignpattern.Singleton;


public class TestMain {
    
    public static void main(String[] args){
        //1.LazyLoading
        /*SingletonLazyLoading singleton1 = SingletonLazyLoading.getInstance();
        SingletonLazyLoading singleton2 = SingletonLazyLoading.getInstance();
        
        System.out.println(singleton1 == singleton2);*/
        
        //2.No-LazyLoading
        SingletonNonLazyLoading instance1 = SingletonNonLazyLoading.getInstance();
        SingletonNonLazyLoading instance2 = SingletonNonLazyLoading.getInstance();
        System.out.println(instance1 == instance2);
        
        //3. static inner class
        SingletonStaticInnerClass sInstance1 = SingletonStaticInnerClass.getInstance();
        SingletonStaticInnerClass sInstance2 = SingletonStaticInnerClass.getInstance();
        System.out.println(sInstance1 == sInstance2);
        
        //4. Enum
        System.out.println(Singleton.INSTANCE == Singleton.getInstance());
        
    }

}


enum Singleton { INSTANCE;
    
    public static Singleton getInstance(){
        return INSTANCE;
    }
}
bubuko.com,布布扣

枚举的方式始effective java推荐的。序列化的时候能保证单例,而且比较简单。当然别的方式的序列化可以用重写 readreslove 来保证单例。

//readResolve to prevent another instance of Singleton
    private Object readResolve(){
        return INSTANCE;
    }

设计模式(四) : 创建型模式--单例模式,布布扣,bubuko.com

设计模式(四) : 创建型模式--单例模式

标签:des   c   style   class   blog   code   

原文地址:http://www.cnblogs.com/ChristyorRuth/p/3759793.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!