October 2018
Intermediate to advanced
370 pages
9h 15m
English
The singleton pattern is used when we want to be sure that only one instance of a certain class is ever created. According to this pattern, a class is responsible for providing a reference to the same underlying instance. The following example shows the easiest way to implement the singleton pattern in Java:
public class Singleton {
private Singleton() {} private static final Singleton INSTANCE = new Singleton();
public static Singleton getInstance() {
return INSTANCE;
}
}
However, Kotlin supports the concept of object declaration. For instance, we need to create an object that represents a user who is currently logged in:
object User { var firstName: String? = null var lastName: String? = null}
The version of this code that is ...
Read now
Unlock full access