Lazy Loading Singleton Object

Singleton class has only 1 instance though out the application, there might be some scenarios where a Singleton class is used for other utility functions where a Singleton object is not required at all, since the object is created using a new keyword, so it will be initialized and loaded in memory at the time of class creation it is also called Eager Loading.

In the below example, the Apple class needs to be a Singleton class, it provides a utility function that is static in nature, hence it doesn't need an instance, but since a Singleton instance is eagerly loaded, it will be created at the time of class loading and will be saved in memory.
as below.

package org.wesome.dsalgo.design.pattern.singleton;

public class Apple {
    static Apple appleInstance = new Apple();

    public Apple() {
        System.out.println("Apple default constructor");
    }

    public static void favoriteFruitUtilityFunc() {
        System.out.println("Apple is my favorite fruit.");
    }
}
package org.wesome.dsalgo.design.pattern.singleton;

public class SingletonClass {
    public static void main(String[] args) {
        Apple.favoriteFruitUtilityFunc();
    }
}
plugins {
    id 'java'
    id "io.freefair.lombok" version "6.4.1"
}

group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
}

test {
    useJUnitPlatform()
}

Pros of Eager loading

  • Eager loading is very simple to implement
  • Singleton objects will be already available when required, the application will not spend time creating objects when called.

Cons of Eager loading

  • Singleton objects will be created but might not be used at all, which leads to resource wastage.

To avoid eagerly creating of singleton object, The object creation needs to be on-demand. in the below code, the object creation will only happen when the getAppleInstance method will be called. It will prevent resource wastage.

package org.wesome.dsalgo.design.pattern.singleton;

public class Apple {
    static Apple appleInstance;

    public Apple() {
        System.out.println("Apple default constructor");
    }

    public static void favoriteFruitUtilityFunc() {
        System.out.println("Apple is my favorite fruit.");
    }

    public static Apple getAppleInstance() {
        System.out.println("Apple getAppleInstance");
        appleInstance = new Apple();
        return appleInstance;
    }
}
package org.wesome.dsalgo.design.pattern.singleton;

public class SingletonClass {
    public static void main(String[] args) {
        Apple.favoriteFruitUtilityFunc();
        System.out.println(Apple.getAppleInstance());
    }
}
plugins {
    id 'java'
    id "io.freefair.lombok" version "6.4.1"
}

group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
}

test {
    useJUnitPlatform()
}

Pros of Lazy loading

  • Singleton objects will be created only when required.
  • An exception that occurs at the time of object creation can be handled.
  • Singleton objects will be already available when required, the application will not spend time creating objects when called.

Cons of Lazy loading

  • The application will require additional time in order to create a Singleton object for the first time.

follow us on