This is the fourth day of my participation in Gwen Challenge

This article is participating in “Java Theme Month – Java Development in Action”, see the activity link for details

Without further ado, let’s move on to design patterns in Spring

The singleton pattern

The Singleton Pattern is one of the simplest design patterns in Java. This type of design pattern is the creation pattern, which provides the best way to create objects.

As an aside, what is the creative pattern? The 24 design patterns can be divided into three categories: creation pattern, structure pattern and behavior pattern

Creative pattern: As the name implies, this is the pattern used to create objects. It abstracts the instantiation process and helps a system be independent of how its objects are created, composed, and represented. Contains the

  • Factory Pattern
  • Abstract Factory Pattern
  • Singleton Pattern
  • Builder Pattern
  • Prototype Pattern

Among the nine design modes designed by Spring Boot, simple factory mode and abstract factory mode belong to factory mode, which is also a creation mode

Okay, back to the theme singleton pattern

How to determine if it is a singleton pattern?

  1. A class has one and only one instance.
  2. A private constructor
  3. A private variable of the class type must have a common method of the class type that returns the unique variable

Simple interest model

public class SingletonPatternDemo {
    public static void main(String[] args) {
        Singleton s1=Singleton.getInstance();
        Singleton s2=Singleton.getInstance();
        if(s1==s2)
            System.out.println("This class is a singleton pattern");
        else
            System.out.println("This class is not a singleton pattern"); }}// Define a class
 class Singleton{
    private Singleton(a){}    //private can only be accessed internally
    private static Singleton instance=new Singleton();// Define an instance of yourself internally
    public static Singleton getInstance(a){         // This static method is for external direct access
        returninstance; }}Copy the code

** Non-singleton mode **

class Singleton{
    private Singleton(a){}
    private static Singleton instance;
    public static Singleton getInstance(a){         
        return instance = new Singleton(); // Each time the getInstance method is called, a new object is created}}Copy the code

The singleton pattern is implemented as follows:

LanHanShi

This is similar to the case of the non-singleton pattern above. “lazy” is not strictly a singleton pattern, but it doesn’t matter

public class Singleton {  
    private static Singleton instance;  
    private Singleton (a){}  
  
    public static Singleton getInstance(a) 
    Public static synchronized getInstance() public static synchronized getInstance(
    {  
    if (instance == null) {  
        instance = new Singleton();  shi
    }  
    returninstance; }}Copy the code

The hungry mode

“Hungry” refers to the instance initialization, call the instance directly feedback, the disadvantage is that the initialization may not be called, so that the initialization resources are wasted

public class Singleton {  
    private static Singleton instance = new Singleton();  
    private Singleton (a){}  
    public static Singleton getInstance(a) {  
    returninstance; }}Copy the code

The singleton pattern can be implemented in two other ways:

Double-checked locking (DCL, double-checked locking)

1. Add the “volatile” keyword. 2.

public class Singleton {  
    private volatile static Singleton singleton;  
    private Singleton (a){}  
    public static Singleton getSingleton(a) {  
    if (singleton == null) {  
        synchronized (Singleton.class) {  
        if (singleton == null) {  
            singleton = newSingleton(); }}}returnsingleton; }}Copy the code

In addition to the above implementation, there are static inner classes, enumerations and so on

Spring dependency injection Bean instances are singletons by default in Spring.

Spring’s dependency injection (including lazy-init) takes place in AbstractBeanFactory’s getBean. The doGetBean method of getBean calls getSingleton to create the bean

Summary of the day: I have a preliminary understanding of how to use the singleton pattern, but I still need a deeper understanding of its application in Spring IOC