java improve the design patterns

This note is mainly taken from rookie tutorials, code re-implemented again, to facilitate their review. After we will continue to add other design patterns, to perfect, if you want to seriously study, you can go to the official website to learn rookie.

Singleton

1. lazy type, thread safe

Whether lazy initialization: Yes

Are multi-thread safe: No

Achieve Difficulty: Easy

Description: This is the most basic way of implementation, the biggest problem is not multi-threaded support, because it is not locked in the strict sense is not a singleton, this way lazyloading obviously, does not require thread safety.

public class Singleton {
    private static Singleton instance;
    private Singleton(){}
    
    public static Singleton getInstance(){
        if(instance==null){// 很多线程都会进来
            instance = new Singleton();
        }
        return instance;
    }
}

2. lazy type, thread-safe

Whether lazy initialization: Yes

Are multi-thread safe: Yes

Achieve Difficulty: Easy

Description: in this way have good lazyloading, able to work well in multi-thread, but inefficient.

Pros: The first call was initialized, to avoid wasting memory

Cons: Must be locked to ensure that a single case, but locking affect efficiency.

public class Singleton {
    private static Singleton instance;
    private Singleton(){}

    public static synchronized Singleton getInstance(){
        if(instance==null){
            instance = new Singleton();
        }
        return instance;
    }
}

3. starving formula

Whether lazy initialization: No

Are multi-thread safe: Yes

Achieve Difficulty: Easy

Description: This method is more common, but easy to produce garbage objects

Advantages: no lock, high efficiency

Disadvantages: initialized when the class is loaded, wasting memory

Based classloader mechanism to avoid synchronization of multiple threads, however, when the class loader instance to be instantiated,

public class Singleton {
  // 静态就已经new了
    private static Singleton instance = new Singleton();
    private Singleton(){}

    public static Singleton getInstance(){
        return instance;
    }
}

4. Double-checked locking / lock double check

DCL即double-checked locking

JDK Version: 1.5 after

Whether lazy initialization: Yes

Are multi-thread safe: Yes

Realize the difficulty: more complex

Description: double locking mechanism, safe and in a multi-threaded to ensure high performance

public class Singleton {
    private volatile static Singleton singleton;
    private Singleton(){}

    public static Singleton getInstance(){
      // 大部分情况下都不为空,直接返回,提高效率
        if(singleton==null){
            synchronized (Singleton.class){
              // 解决单例的判断
                if(singleton==null){
                    singleton=new Singleton();
                }
            }
        }
        return singleton;
    }
}

The register-based / static inner classes

Whether lazy initialization: Yes

Are multi-thread safe: Yes

Realize the difficulty: General

Description: This embodiment can achieve the same effects as a double lock object, but the implementation is simpler. Also use classloader mechanism to ensure that only one thread at initialization instance. Only by an explicit call getInstance method will explicitly load SingletonHolder class to instantiate instance. On the one hand instantiated instance consumption of resources, on the other hand, when you do not want to instantiate Singleton class loaders, because they can not take the initiative to ensure that Singleton class also possible to use so as to be loaded in other places, this time instantiation inappropriate.

public class Singleton {
  // 静态内部类,只有调用它的时候才会初始化
    private static class SingletonHolder{
        private static final Singleton INSTANCE = new Singleton();
    }
    private Singleton(){}
    public static final Singleton getInstance(){
        return SingletonHolder.INSTANCE;
    }
}

6. Enumeration

JDK Version: 1.5 after

Whether lazy initialization: No

Are multi-thread safe: Yes

Achieve Difficulty: Easy

Description: This is the best way to implement the method of Singleton and more concise, automatic support serialization mechanism, absolutely prevent multiple instantiated

Because only joined after 1.5, less practical application

public enum Singleton {
    INSTANCE;
    public void whateverMethod(){}
}

to sum up

Generally recommend the use of a third way a hungry man, only clear lazyloading, using a fifth static inner classes, involving the de-serialization, using the sixth enumeration method. There are other special needs, consider the fourth double check the lock mode.

Guess you like

Origin blog.csdn.net/wjl31802/article/details/91360815