How Singleton pattern with Early Initialization vs Lazy Loading in Apex differs? - Salesforce Stack Exchange

PHOTO EMBED

Tue May 16 2023 11:46:19 GMT+0000 (Coordinated Universal Time)

Saved by @hurrand

public with sharing class LazyInitializedSingleton {

//private static instance of the class
private static LazyInitializedSingleton instance = null;

//private constructor to avoid creating an instance anywhere outside of this class
private LazyInitializedSingleton(){}

public static LazyInitializedSingleton getInstance(){
    if(instance == null){
        instance = new LazyInitializedSingleton();
    }
    return instance;
}
content_copyCOPY

Singleton Lazy

https://salesforce.stackexchange.com/questions/391882/how-singleton-pattern-with-early-initialization-vs-lazy-loading-in-apex-differs