Singletons

Singletons are part of many architecture designs. We like to create the object once and use it multiple times with easy access. Developers like to make DBHelpers and SharedPreferences manager classes singleton. But not all developers know how to create the best singleton class. The best singleton class is that which supports every scenario of multi-threading. A singleton class has various object creation and initialization methods such as eager initialization, lazy initialization, double check, and so on.

A vastly used Java code to achieve singleton design pattern and object initialization will look as follows:

    public class Singleton {      private static Singleton instance = null;      private Singleton(){    }      private synchronized static ...

Get Kotlin Blueprints now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.