Serial articles

  • 1. Singleton mode, you used it today
  • 2. Builder mode, you used it today
  • 3. Observer mode, you used it today

1. Introduction

When it comes to design patterns, some of you may feel unfamiliar, while others may feel used frequently. In fact, when it comes to design patterns, there is no need to feel strange and afraid. In fact, we use or touch design patterns more or less inadvertently every day. This article is designed to introduce you to the most basic design pattern, the singleton pattern!

2. Introduction

The singleton pattern means that there is only one instantiated object of the class, and the instantiation process is implemented by the class itself. The constructor of this class is private.

3. Several common singleton patterns

Hunchman singleton pattern

/** * Created by JTL * date :Createdin2019/1/25 15:48 * Description: Hungry singleton mode * Advantages :1. Space for time, each time directly loaded, saving the time to create instances * Disadvantages :1. Because instances are created directly regardless of whether the class is used, Public class HungrySingleton {private static HungrySingleton sSingleton = new HungrySingleton(); privateHungrySingleton(){}

    public static HungrySingleton getInstance() {returnsSingleton; }}Copy the code

Lazy singleton pattern

/** * Created by JTL * date :Createdin2019/1/25 15:45 * Description: Lazy singleton mode * Advantages :1. Time for space, the instance is created on the call, saving space * 2. The instance is created on each getInstance, saving resources * * Disadvantages :1. 2. Each call to getInstance requires thread synchronization. Public class LazySingleton {private static LazySingleton sSingleton; privateLazySingleton(){}

    public static synchronized LazySingleton getInstance() {if (sSingleton==null){
            sSingleton=new LazySingleton();
        }

        returnsSingleton; }}Copy the code

Double Check Lock (DCL) singleton mode

/** * Created by JTL * date :Createdin2019/1/25 16:16 * Description: Double Check Lock (DCL) singleton mode * Advantages :1. It's judged on a lazy basis that thread synchronization is only done when the first instance is created. High resource utilization * Disadvantages :1. Problems in high concurrency * 2. Problems before JDK5. Since the Java compiler allows processors to execute out of order, the JMM prior to JDK specifies the write order of Cache registers to main memory, which may be 1-2-3 or 1-3-2. (1) Allocate memory to DclSingleton() * (2) call the constructor of DclSingleton() to initialize the member field; * (3) point the mSingleton to the allocated memory space (mSingleton is not null) * 3. After JDK5, Java uses a new memory model, JSR-133, which enhances the memory semantics of volatile. */ public class DclSingleton {private static volatile DclSingleton mSingleton=null; privateDclSingleton(){}

    public static DclSingleton getInstance() {if (mSingleton==null){
            synchronized (DclSingleton.class){
                if(mSingleton==null){ mSingleton=new DclSingleton(); }}}returnmSingleton; }}Copy the code

Static inner class singleton pattern

/** * Created by JTL * date :Createdin2019/1/25 16:14 * Description: Static inner class singleton pattern * Advantages :1. */ Public Class StaticSingleton {privateStaticSingleton(){}

    private static StaticSingleton getInstance() {returnStaticSingletonHolder.sSingleton; } private static class StaticSingletonHolder{ private static final StaticSingleton sSingleton=new StaticSingleton(); }}Copy the code

4. Subsequent

Several common singleton patterns are listed above, of which the most common is the static inner class singleton pattern. Of course, these four singletons are not all singletons. There are also enumerated singletons, and singletons implemented using maps. If you want to use a singleton directly, just copy the code. If you want to know their strengths and weaknesses, you can read the comments. I will wait for you in the wind and rain. One of your likes or collections is the motivation for me to write. I hope we can make progress together. If there is any mistake, please put forward in time and I can correct. GitHub source code:Github.com/13046434521…