Comparison and Analysis of Singleton Mode and Java in Kotlin

  • 2021-09-20 21:25:20
  • OfStack

Preface

Singleton pattern, 1 has been the most commonly used design pattern in our daily development, and it is also a very important and easy question to be asked in interviews. In the daily development, we commonly used language or Java, but today I bring you is in Kotlin language, singleton mode is how to write, and will compare Java, the following words do not say much, to see a detailed introduction

1. Lazy writing (evil Chinese style)

In java


 public class Singleton{
  public static final Singleton instance = new Singleton();
  public Singleton getInstance() {
    return instance;
  }
 }

In kotlin

The simplest way to write it is to declare it directly with object


object Singleton{}

2. Basic lazy loading (thread synchronization is not implemented)

Note: This approach implements lazy loading, but it is not thread-safe and may create multiple different instances in multiple threads

In java


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

In kotlin


class Singleton private constructor{
 companion object {
   val intance by lazy(LazyThreadSafetyMode.NONE) { Singleton() }
 }
}

3. Thread Synchronization Singleton 1

Note: In fact, kotlin can be said to be literally translated from java. Although it is thread-safe, it affects efficiency too much. Mainly look at the following

In java


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

In kotlin


class Singleton private constructor(){
  companion object {
   lateinit var instance: Singleton
   @Synchronized
   fun get(): Singleton {
     if (instance == null) {
       instance = Singleton();
      }
    return instance!!
   }
  }
}

4. Thread Synchronization Singleton 2

Note: Efficiency problems caused by thread synchronization, lazy loading and no synchronization

Single example of double check lock in java


public class Singleton {
  private static Singleton instance = null;
  private Singleton (){}
  public static Singleton getInstance() {
   if (instance == null) {
   synchronized (Singleton.class) {
     if (instance == null) {
      instance = new Singleton();
     }
    }
  }
  return instance;
 }
}

kotlin


class Singleton private constructor(){
  companion object {
   val intance by lazy(LazyThreadSafetyMode.SYNCHRONIZED) { Singleton() }
 }
}

5. Static inner class singleton

In java


public class Singleton {
 private Singleton (){}
 private static class Holder {
 private static Singleton instance = new Singleton();
 }
 public static Singleton getInstance(){
  return Holder.instance;
 }
}

In kotlin


class Singleton private constructor(){
  companion object {
   fun getInstance(): Singleton {
    return Holder.instance
   }
  }
  private object Holder {
   val instance = Singleton()
  }
 }

Summarize


Related articles: