Digression :

Recently, due to the new virus epidemic situation is more serious , On the principle of cherishing life , I can only stay at home and isolate myself , But I don't want to waste that time , I think I've always been right android A little knowledge of common design patterns in , It's time to review and learn at the same time , Record it by the way , Hope to help myself in the future . Finally, with a little prayer, the epidemic will pass quickly , Wuhan refueling , Go China !

brief introduction :

What is design pattern ?

Design patterns are a set that is used repeatedly , Most people know , Classified and catalogued , Summary of code design experience . Design patterns are used for reusable code , Make code easier for others to understand , Ensure code reliability , Reusability of program . To put it bluntly, it is the simplified reusable code or code habit summed up by industry leaders , Then it was recognized by most people , Used by the gourd , So as to achieve the reliability and readability of the code .

Why learn design patterns ?

1, First of all, it is a recognized knowledge , It's a shortcut to personal ability improvement , It is also the knowledge point that most interviews will involve .

2, It's used in most code , Learning it can improve the ability to read code , It also avoids repeated wheel building .

Singleton mode :

Singleton is one of the simplest design patterns . This type of design pattern belongs to creative mode , It provides one of the best ways to create objects . This single pattern involves a class , This class is responsible for creating its own objects , At the same time, make sure that only a single object is created . This class provides a way to access its unique object , Can be accessed directly , There is no need to instantiate an object of this class .

To put it bluntly, when this object does not exist in the entire application memory , We can create this object by calling the methods provided by this class , Once the object has been created and has not been recycled by the system , Then we call this method at this time without creating the object , Instead, it gets a reference to an object in memory directly . This ensures that there is only one object in the whole application .

Android Use in :

Sluggard , Thread unsafe
public class Singleton { private static Singleton instance; private Singleton
(){} public static Singleton getInstance() { if (instance == null) { instance =
new Singleton(); } return instance; } }

This method uses the non null judgment of static variables to realize delay initialization , But there is no lock on the static method synchronized, This leads to a problem , There is no guarantee of singletons in multithreaded programs , So it's thread unsafe . So here's a lazy singleton that's thread safe .

Sluggard , Thread safety
public class Singleton { private static Singleton instance; private Singleton
(){} public static synchronized Singleton getInstance() { if (instance == null)
{ instance = new Singleton(); } return instance; } }

This method only adds a synchronization lock to the previous static method synchronized, So it can be used in multithreading . However, due to the synchronization lock on the class , It leads to inefficiency , Because in most cases, synchronization is not required . In order to improve the efficiency of thread evolution .

Hungry Han style
public class Singleton { private static Singleton instance = new Singleton();
private Singleton (){} public static Singleton getInstance() { return instance;
} }

This method uses class loading to initialize singleton objects , When multiple threads call getInstance() When you get an object, you just get a reference to an object that already exists in memory , Thus, the security of multithreading is guaranteed . However, this method is initialized when the class is loaded , Maybe this object is not used in a program at all , Waste memory , Causes a memory leak .

double-checked locking( Double check lock )
public class Singleton { private volatile static Singleton singleton; private
Singleton (){} public static Singleton getSingleton() { if (singleton == null)
{ synchronized (Singleton.class) { if (singleton == null) { singleton = new
Singleton(); } } } return singleton; } }

This is a common singleton pattern , utilize volatile ensure Singleton Visibility of variables . Then, double non null checking and checking are used Singleton.class Class implements a singleton of multithreading security , The delay initialization is guaranteed , It also ensures efficiency .

Static inner class
public class Singleton { private static class SingletonHolder { private static
final Singleton INSTANCE = new Singleton(); } private Singleton (){} public
static final Singleton getInstance() { return SingletonHolder.INSTANCE; } }
This way is an extension of the hungry Han style , Although it is also used classloader principle , But the static inner class is used to realize the delayed initialization , Only called through the display getInstance
Method , Will be loaded explicitly SingletonHolder class , To instantiate instance. This approach is only applicable to static domains , The double locking method can be used when the instance domain needs to delay initialization .

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

Enumeration is a method that has not been widely used to implement singleton , It's more concise , Support serialization mechanism and absolutely prevent multiple instantiation , It also prevents deserialization from re creating new objects . Therefore, when it comes to deserialization to create an object , You can try using .

Technology
©2019-2020 Toolsou All rights reserved,
One is called “ Asking for the train ” A small village Finally got the train Spring Boot Lesson 16 :SpringBoot Implementation of multithreading with injection class Chrome OS, For programmers and Windows What does it mean ? Internet Marketing JAVA Convert a string to a numeric type I've been drinking soft water for three years ? What is the use of soft water and water softener You don't know ——HarmonyOS Talking about uni-app Page value transfer problem JavaScript Medium Call and ApplySparkSQL Achieve partition overlay write Character recognition technology of vehicle license plate based on Neural Network