Preface
The single-interest mode is inevitable in the process of writing code. Let me summarize the five commonly used ways of writing single-interest. Without further ado, let’s take a look at the detailed introduction together.
Create a simple profit when loading a class
Java implementation
public class Config{ private static Config INSTANCE=new Config(); private Config(){ //Constructor } public static Config getInstance(){ return INSTANCE; } }
Kotlin implementation
object Config{}
The above writing method is simple and crude, and it is created directly when loading the class, but this will slow down the start of the process. Therefore, it can be loaded when used, such as the following writing method
Lazy loading writing
Java implementation
public class Config{ private static Config INSTANCE; private Config(){ //Constructor } public static Config getInstance(){ if(null==INSTANCE){ INSTSANCE=new Config(); } return INSTANCE; } }
Kotlin implementation
public class Config{ companion object{ val instance by lazy(){ Config() } } }
Although lazy loading avoids creating when loading classes, threads are not safe. If multiple classes obtain single profits at the same time, multiple single profits may be created. Therefore, you can add thread locks when creating single profits, such as the following writing method:
Synchronous lock writing method
Java implementation
public class Config{ private static Config INSTANCE; private Config(){ //Constructor } public static synchronized Config getInstance(){ if(null==INSTANCE){ INSTANCE=new Config(); } return INSTANCE; } }
Kotlin implementation
class Config{ companion object{ private var instance:Config?=null @Synchronized fun get():Config{ if(nnull==instance) instance=Config() return instance } } }
Synchronous locks avoid single profits not being created repeatedly, but synchronous locks
Double verification writing method
Java implementation
public class Config{ private static volatile Config INSTANCE; private Config(){ //Constructor } public static Config getInstance(){ if(null==INSTANCE){ synchronized(){ if(null==INSTANCE){ INSTSANCE=new Config(); } } } return INSTANCE; } }
Kotlin implementation
class Config{ companion object{ val instance by lazy(){ Config() } } }
Static internal class writing
This writing method avoids the initialization of single profits when class loading, and at the same time, it handes the synchronization lock problem to the virtual machine for processing. It is considered the most elegant writing method. Java and Kotlin writing methods are almost exactly the same
Java implementation
public class Config{ private static class Helper{ private static Config INSTANCE=new Config(); } private Config(){ //Constructor } public static Config getInstance(){ return ; } }
Kotlin implementation
class Config private constructor(){ companion object{ fun getInstance = } private object Helper{ val instance = Config() } }
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.