Prodigal son Tang Shao 2022-02-13 07:29:26 阅读数:438
The more common is the singleton mode , Factory mode, etc .
Specifically, single case mode , The main function of singleton mode is to ensure that Java In the program , One class Class Only one instance exists . such as spring The default is the singleton and the one that generates the unique sequence UUID Used in
The main function of singleton mode is to ensure that Java In the program , One class Class Only one instance exists . Apply to
1. The environment that needs to generate unique sequences , For example, generate a unique sequence
2. Only one thread pool object is needed . This ensures that a class used globally is not frequently created and destroyed , Save system resources .
The implementation of a singleton is mainly through the following two steps :
1. Create a privatization static for this class final Class object
2. Privatized construction method
3. Create a static method and return the created class object
1. Hungry Chinese style ( Thread safety )
Initialize the instance when the class is loaded , Avoid the problem of multithreading synchronization
public class Singleton {
private static Singleton instance = new Singleton();
private Singleton (){}
public static Singleton getInstance() {
return instance;
}
}
2. Slacker type ( Thread safety )
Get instance of getInstance() Method with synchronization lock . Single instance in multithreading scenario is guaranteed
public class Singleton {
private static Singleton instance;
private Singleton (){}
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
3. Slacker type ( Thread unsafe )
The first call initializes , Implemented lazy loading features . It is forbidden to use... In the multithreading scenario , Because there may be multiple objects , It's no longer a single case
public class Singleton {
private static Singleton instance;
private Singleton (){}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
1. Thread safety : Hungarian is inherently thread safe , Can be used directly for multithreading without problems , Laziness itself is not thread safe .
2. Resource loading and performance :
Hungry Chinese type instantiates a static object while creating a class , Whether or not you will use this single example later , Will occupy a certain amount of memory , But the corresponding , It's also faster on the first call , Because its resources have been initialized .
And the slob, as the name suggests , Load will be delayed , The object is instantiated the first time the singleton is used , The first call is initialized , If there is more work to be done , There will be some delay in performance , After that, it was the same as the hungry Han style .
Factory mode is divided into simple factory mode , Factory method model , Abstract factory pattern
The simple factory pattern is that a specific class does the factory class , Create instances of other classes
The factory method pattern has an abstract parent class that defines the public interface , Subclasses are responsible for generating concrete objects
The abstract factory pattern provides an interface for creating a series of related or interdependent objects , Without specifying their specific classes .
copyright:author[Prodigal son Tang Shao],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130729242384.html