2 singleton design pattern

solved problem

Singleton pattern to solve the problem is: to ensure that a class can instantiate an object.

Implementation

Generally when we ensure only one instance of a class of objects, practices are:


public class car{
    private Brake brake;
    public void useBreak(){
        if (brake == null)
            brake = new Brake();
        brake.run();
    }
}

If the brake object is not only a Car class uses this class do? Doing so only to ensure that there is a brake, more car objects or other types of brake instance of an object in a car brake would have appeared more (of course not the case here, a car has always been a brake). So we can not guarantee only one instance of the rights to someone else, you should make their own class to ensure that only instance of an object.
There are several categories Methods:

Lazy law

Lazy method is to wait until you need a singleton instance, I will go to instantiate an object comes to you.

public final class Singleton{
    private static Singleton singleton;
    
    private Singleton(){};
    
    public static Singleton getSingleton(){
        if (singleton == null)
            singleton = new Singleton();
        return singleton;
    }
}

Hungry Chinese and French

Hungry man law is to help you instantiate an object, when you need it to get me. There are several methods to realize a method starving:

Below this method is the simplest and best understood, but there are security risks in the case of multi-threaded.

    
public final class Singleton{

    private static Singleton singleton = new Singleton();
    
    private Singleton(){};
    
    public static Singleton getSingleton(){
        return singleton;
    }
}

This method utilizes the following class initialization by Examples of methods and principles of the code block performing a static class member variable sequence by the code position. By class instantiation The method according to first perform location codes and block codes Examples member variables in the constructor is executed. and The method only once (also executed only in the case of multi-threaded once), so in the case of multi-thread safe.


public final void Singleton{
    
    private static Singleton singleton;
    static{
        singleton = new Singleton();
    }
    private Singleton(){};
    
    public static Singleton getSingleton(){
        return singleton;
    }

}

This embodiment is based on the following enum (enumeration) a manner, it is the best way to achieve single-mode embodiment. This implementation in a multi-threaded, the case is still under serialization is safe.


public enum Singleton{
    SINGLETON;
    //  接下来就是定义变量和方法(值得注意的是:变量和方法最好不是静态,不然SINGLETON无法使用,只能通过Singleton使用静态变量和方法)
}

Double-checked locking

Double-checked locking in view of the multi-threaded lazy method.


public final class Singleton{
    
    private static Singleton singleton;
    
    private Singleton(){};
    
    private static Singleton getSingleton(){
        if (singleton == null){
            sychronized(Singleton.class){
                if (singleton == null)
                        singleton = new Singleton();
            }
            
        }
        return singleton;
    }
}

Points

  1. Construction method to ensure that a single case is private, so can not be instantiated by a single new embodiment (of course, by reflection or multiple objects can be instantiated)

  2. Singleton guarantee can not be inherited.

  3. Examples of instances to ensure the function before returning to obtain a single example of embodiment examples.

Such benefits realized

  1. Control the use of resources by thread synchronization to control concurrent access to resources

  2. Generating a control example, in order to save resources

  3. Shared control data, under conditions not directly related to the establishment, so that communication between multiple processes or threads irrelevant

Guess you like

Origin www.cnblogs.com/zhangchenwei/p/12409950.html