Five Commonly Used Writing Methods of Simple Benefit in Kotlin

  • 2021-09-12 02:05:15
  • OfStack

Preface

Simple profit mode is the process of writing code is inevitably used, below I summarize 1 simple profit commonly used 5 writing, words not much to say, to see a detailed introduction to it

Create simple benefits when loading classes

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 is simple and crude, and it is created directly when the class is loaded, but it will slow down the startup process, so it can be loaded when it is used, such as the following writing

Lazy loading writing

Implementation of Java


public class Config{
 private static Config INSTANCE;
 private Config(){
 // Constructor 
 }
 public static Config getInstance(){
 if(null==INSTANCE){
 INSTSANCE=new Config();
 }
 return INSTANCE;
 }
}

Implementation of Kotlin


public class Config{
 companion object{
 val instance by lazy(LazyThreadSafetyMode.NONE){
  Config()
 }
 }
}

Although lazy loading avoids creating when loading classes, threads are not safe. If multiple classes obtain simple benefits at the same time, multiple simple benefits may be created. Therefore, thread locks can be added when creating simple benefits, such as the following writing:

Synchronous lock writing method

Implementation of Java


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 simple benefits and will not be created repeatedly, but synchronous locks

Double check writing

Java Implementation


public class Config{
 private static volatile Config INSTANCE;
 private Config(){
 // Constructor 
 }
 public static Config getInstance(){
 if(null==INSTANCE){
  synchronized(Config.class){
  if(null==INSTANCE){
   INSTSANCE=new Config();
  }
  }
 }
 return INSTANCE;
 }
}

Kotlin Implementation


class Config{
 companion object{
 val instance by lazy(LazyThreadSafetyMode.SYNCHRONIZED){
  Config()
 }
 }
}

Static inner class writing

This writing method avoids the simple advantage of initialization when the class is loaded, and at the same time, the synchronization lock problem is handed over to the virtual machine, which is the most elegant writing method. Java and Kotlin writing methods are almost one model and one sample

Implementation of Java


public class Config{
 private static class Helper{
 private static Config INSTANCE=new Config();
 }
 private Config(){
 // Constructor 
 }
 public static Config getInstance(){
 return Helper.INSTANCE;
 }
}

Implementation of Kotlin


class Config private constructor(){
 companion object{
 fun getInstance = Helper.instance
 }
 private object Helper{
 val instance = Config()
 }
}

Summarize


Related articles: