Singleton Design Pattern in Apex
Amit Chaudhary

Amit Chaudhary

Amit Chaudhary is Salesforce Application & System Architect and working on Salesforce Platform since 2010. He is Salesforce MVP since 2017 and have 17 Salesforce Certificates. He is a active blogger and founder of Apex Hours.

Singleton Design Pattern in Apex

In this post/session we will talk about Singleton Design Pattern in Apex and when to use singleton design pattern in Salesforce? This pattern restricts the instantiation of a class to one “single” instance only within a single transaction context. Check our complete Apex Design Patterns training to learn more about design pattern.

What is Singleton Design Pattern in Apex

The Singleton pattern is a creational pattern – it helps you to instantiate objects in a particular way. It allows for the existence of only one instance of an object, but everyone has global access to that object. Singletons can help improve performance and reduce memory footprint.

Singleton design 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.

Implement Singleton pattern in Apex

We should consider below point while implementing the singleton pattern in Apex.

  • Private Constructor : Create one private constructor to restrict instantiation of the apex class from other classes.
  • Private static variable: create private static variable of the same class that is the only instance of the class.
  • public method getInstance(): Create one public static method that returns the instance of the class. getInstance method will only initiate instance of class if it does not exist

Singleton Class Diagram

Singleton Class Diagram in Apex

When we should use Singleton Design Pattern?

Singleton pattern can be used for logging and caching related implementation. This pattern can also be used with other design patterns like Abstract Factory and more.

Singleton vs. Static

What’s the difference Singleton and Static?

SingletonStatic
Singletons can implement interfaces and extend classesstatic methods cannot
Singletons can be instantiated immediately or lazy-loadedStatics must be instantiated at load
Singletons follow object oriented principles more closelySingletons follow object oriented principles more closely than statics.

Singleton Design Pattern Example in Apex

Here is example of Singleton design pattern in Salesforce.

public with sharing class Singleton {
    
    //private static instance of the class
    private static Singleton instance = null;
    
    //private constructor to avoid creating an instance

    private Singleton(){}
    
    public static Singleton getInstance(){
           if(instance == null){
              instance = new Singleton();
            }
        return instance;
    }
}

In this pattern, instance will only be instantiated when we call the getInstance() method. This will reduce the effort to load large resources at the time of initialization. Like below code

Singleton instance = Singleton.getInstance();

Singleton Design pattern in Apex Video

Summary

I hope this helped you to understanding Singleton design pattern in Salesforce. Please share your thoughts and suggestions in comments section.

Share this article

2 Comments

  • Hi Amit,
    I am trying to implement this in my org, but getting this error.
    Final members can only be assigned in their declaration, init blocks, or constructors: instance.

    Even I tried creating your class, but still same error.

Leave a reply

Keep in Touch

Subscribe for Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 3,230 other subscribers

Search

Our Supporter

RECENT POSTS

Apex Hours

Apex Hours is one stop platform to learn Salesforce skills and technology

Join our Newsletter and get tips and tricks how to explore the salesforce for free!