Apex Design Pattern: Singleton Design
Apex Design Pattern: Singleton Design

Apex Design Pattern: Singleton Design

Singleton is one of the Gangs of Four Design Patterns and comes in the Creational Design Pattern category.

Here we will learn about Singleton design pattern principles, different ways to implement it and best practices to follow along with its usage.

Singleton Pattern

  • Singleton pattern restricts the instantiation of a class and ensures that only one instance of the class exists per transaction in apex.
  • Singleton class must provide a public access point to get the instance of the class for any transaction.
  • Singleton pattern can be used for logging and caching related implementation.
  • Singleton pattern can also be used with other design patterns like Abstract Factory, Fa?ade, etc.

Singleton pattern implementation in Apex

  • Private constructor to restrict instantiation of the class from other classes.
  • Private static variable of the same class that is the only instance of the class.
  • Public static method that returns the instance of the class, this is the public access point for other classes to get the instance of the singleton class.

Different approaches to implement Singleton pattern

  1. Eager Initialization
  2. Lazy Initialization

Eager Initialization

  • The instance of singleton class is created at the time of class loading.
  • This is the easiest method to create a singleton class.
public with sharing class EagerInitializedSingleton {
    
    //private static instance of the class
    private static final EagerInitializedSingleton instance = new EagerInitializedSingleton();
    
    //private constructor to avoid creating an instance anywhere outside of this class
    private EagerInitializedSingleton(){}
    
    public static EagerInitializedSingleton getInstance(){
        return instance;
    }


}

If your singleton class is not using lot of resources, this is the approach to use. Otherwise we should avoid the instantiation unless client calls the getInstance method.

Lazy Initialization

Lazy initialization creates the instance in the public access method, if the instance is not created before in the same transaction.

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;
    }


}

In this pattern, instance will only be instantiated when client will call the getInstance method. This will reduce the effort to load large resources at the time of initialization.


I hope this article helps you in understanding Singleton design pattern. Please share your thoughts and suggestions in comments section.

Gaurav Sadhwani

Salesforce Solution Architect at GovTech

4 年

precisely explained Rajat. Eagerly waiting for other articles of this series.

Cesar Murilo Ribeiro

Crm Specialist I English I Spanish I Portuguese

4 年

??????

Chetan Devraj

Salesforce Innovation and Growth

4 年

Good article Rajat. Well explained.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了