Apex Design Patterns Series – The Singleton Pattern

Apex allows us to develop any custom solution. This post is an aggregate of common design patterns and associated best practices for Apex development. Knowing these patterns will not only help you achieve excellence in daily development but also will prepare you to rock any Developer interview.

Table of Contents

Introduction

Apex allows us to develop any custom solution. This post is an aggregate of common design patterns and associated best practices for Apex development. Knowing these patterns will not only help you achieve excellence in daily development but also will prepare you to rock any Developer interview.

Types of Design pattern

Singleton — minimizing object instantiation for improved performance and to mitigate the impact of governor limits
Strategy — defining a family of algorithms, encapsulating each one and making them interchangeable and selectable at runtime
Decorator — extending the functionality of a sObject in Apex
Bulk State Transition — efficiently tracking the change of a field value in a trigger and executing functionality based on this change
Composite — treating a group of objects in a similar manner to a single instance of that object

The Singleton Pattern

  • Ensure a class has only one instance, and provide a global point of access to it.
  • Encapsulated “just-in-time initialization” or “initialization on first use“.

Intent

The intent of the Singleton pattern is to provide only a single instance of the class and that instance can be used anywhere throughout the transaction.

In other words,

The application needs one, and only one, an instance of an object. Additionally, lazy initialization and global access are necessary.

Structure


The Singleton Pattern Structure

Example

The Singleton pattern ensures that a class has only one instance and provides a global point of access to that instance.

The office of the President of the India is a Singleton. The Indian Constitution specifies the means by which a president is elected, limits the term of office, and defines the order of succession. As a result, there can be at most one active president at any given time. Regardless of the personal identity of the active president, the title, “The President of the India” is a global point of access that identifies the person in the office.

Credit – https://sourcemaking.com/design_patterns/singleton

What iT takes to develop Singleton pattern

  • Public Property that needs to be accessed by developers/users
  • Private Instance of the Apex Class
  • Private constructor to run the code
  • Public static method that a developer can refer to in the code

Problem Statement

Let’s say you are integrating the Stripe Payment gateway with Salesforce and need to send the data to Stripe on certain actions. For Example, Account Creation.

You have developed the following apex call as initial version

InItial Version – v1


					
				

If you notice that, in the above code we are making the SOQL query for both the methods and there could be more methods. Now consider, that if there is bulk data then you will get into SOQL Governor Limit.

Let’s modify the code and have a separate method do make the SOQL Query

Modified – v2


					
				

We have made the changes and kept the query in a separate method. However still if there are many records you will get into Governor Limit.

Let’s develop the same using the Singleton pattern.

Using Single Pattern


					
				

In the above code, we have implemented the Singleton Pattern and now the instance will only be initialized once.

Check list

  • Define a private static attribute in the “single instance” class.
  • Define a public static accessor function in the class.
  • Do “lazy initialization” (creation on first use) in the accessor function.
  • Define all constructors to be protected or private.

Resources

Amit Singh
Amit Singh

Amit Singh aka @sfdcpanther/pantherschools, a Salesforce Technical Architect, Consultant with over 8+ years of experience in Salesforce technology. 21x Certified. Blogger, Speaker, and Instructor. DevSecOps Champion

Articles: 299

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Your email address will not be published. Required fields are marked *

Discover more from Panther Schools

Subscribe now to keep reading and get access to the full archive.

Continue reading