Java設計模式 create Singleton

單態定義:
Singleton
模式主要做用是保證在Java應用程序中,一個類Class只有一個實例存在。 java

在不少操做中,好比創建目錄 數據庫鏈接都須要這樣的單線程操做。數據庫

還有, singleton可以被狀態化;這樣,多個單態類在一塊兒就能夠做爲一個狀態倉庫同樣向外提供服務,好比,你要論壇中的帖子計數器,每次瀏覽一次須要計數,單態類可否保持住這個計數,而且能synchronize的安全自動加1,若是你要把這個數字永久保存到數據庫,你能夠在不修改單態接口的狀況下方便的作到。安全

另外方面,Singleton也可以被無狀態化。提供工具性質的功能,Singleton模式就爲咱們提供了這樣實現的可能。使用Singleton的好處還在於能夠節省內存,由於它限制了實例的個數,有利於Java垃圾回收(garbage collection)。ide

咱們經常看到工廠模式中類裝入器(class loader)中也用Singleton模式實現的,由於被裝入的類實際也屬於資源。工具

public class SingletonA  {
    public static boolean instanceFlag = false; //true if 1 instance
    //private SingletonA instance;
    private int i = 0;
    
    // Set constructor private and do nothing
    // Can not new a instance outside class
    private SingletonA() {
    }

    public int getNum() {
        return i;
    }
    
    public void setNum() {
        i++;
    }
    
    public static SingletonA getInstance() {
        if(! instanceFlag) {
            instanceFlag = true;
            return new SingletonA();
        }
        return null;
    }

    public void finalize() {
        instanceFlag = false;
    }
}

測試A測試

public class TestA  {
    public static void main(String[] args) {
        // Can not create a instance !
        //SingletonA instance1 = new SingletonA();  
        SingletonA instance2 = SingletonA.getInstance();
        if(instance2 != null) {
            instance2.setNum();
            System.out.println("i is:" + instance2.getNum());
        }
        SingletonA instance3 = SingletonA.getInstance();
        if(instance3 == null) {
            System.out.println("Can not get instance twice !");   
        }
    }
}

實例2this

public class SingletonB  {
    static private Hashtable registry = new Hashtable();
    //static private SingletonB instance;
    
    public static void Register(String name, SingletonB aInstance) {
        registry.put(name, aInstance);
    }
    public static SingletonB GetInstance(String name) {
        return LookUp(name);
    }
    
    protected static SingletonB LookUp(String name) {
        return (SingletonB)registry.get(name);
    }
}
public class SubSingletonB extends SingletonB {
    public static boolean instanceFlag = false; //true if 1 instance
    //private int i = 0;
    
    public SubSingletonB() throws SingletonException {
        if(instanceFlag) {
            throw new SingletonException("Only can create a instance !");
        } else {
            instanceFlag = true;
            super.Register("Sub1", this);
        }
    }
    
    public void finalize() {
        instanceFlag = false;
    }
}
public class SingletonException extends RuntimeException {
    public SingletonException() {
        super();
    }
    
    public SingletonException(String s) {
        super(s);
    }
    
}

測試2.。spa

public class TestB  {
    public static void main(String[] args) {
        // First we get a instance from SingletonB
        SingletonB instance1 = SingletonB.GetInstance("Sub1");
        if(instance1 == null) {
            System.out.println("There is no such instance in registry !");
        } else {
            System.out.println(instance1.getClass());
        }
        
        // Then we register a new instance
        try {
            SingletonB instance2 = new SubSingletonB();
            System.out.println("We had created a new instance named \"Sub1\" now");
        } catch (SingletonException e) {
            System.out.println(e.getMessage());
        }
        
        // To get instance again
        instance1 = SingletonB.GetInstance("Sub1");
        if(instance1 == null) {
            System.out.println("There is no such instance in registry !");
        } else {
            System.out.println(instance1.getClass());
        }

        // Finally we create a new instance again
        try {
            SingletonB instance3 = new SubSingletonB();
            System.out.println("We had created a new instance named \"Sub1\" now");
        } catch (SingletonException e) {
            System.out.println(e.getMessage());
        }
        
    }
}

結果線程

There is no such instance in registry !code

We had created a new instance named "Sub1" now

class corner.dp.create.Singleton.SubSingletonB

Only can create a instance !

相關文章
相關標籤/搜索