💃 Design Patterns for Salesforce🕺

More codebase strategies you need

Good morning, Salesforce Nerds! It’s that time again. ⌚️ I’ve said it before - the ChaCha loves a good pattern!

Often times, the simplest solutions can provide the greatest benefits.

Today’s design pattern fits squarely within this mantra. 🧘 

Let’s check out a fundamental pattern Salesforce devs can use to mitigate those pesky governor limits!

Read on for our take on the Singleton Pattern. 👇️ 

TABLE OF CONTENTS

Quick Recap

Design Patterns

From a high-level, design patterns are solutions to problems developer’s often face. They’re blueprints we can follow. 🏗️ 

These should be treated as guiding principles for Salesforce devs. 💯 

They'll aid in the structure and organization of your codebase and help set you on a path to:

Efficiency

Maintainability

Scalability

So, let’s dig into one of my fav’s! 🤩 

Singling one out

Singleton Pattern

Officially, The Gang of Four(GoF) came up with this definition:

The Singleton Design Pattern is used when there should only be one instance of a given class. It uses static, class methods and private constructors to strictly control creation of new instances of the class.

Gang of Four(GoF)

Let’s break that down a bit. 😅 It’s actually a very straightforward implementation that might have you wondering where the benefits are.

👉️ This is a Creational Pattern.

👉️ Provides a single, global point of access to a class instance throughout the codebase.

👉️ Ensures only one instance of this class is created.

👉️ Minimizes memory usage by reducing object instantiation & query execution. Boosting performance & mitigating governor limits.

👉️ Lazy-loaded instantiation improves performance.

Let’s see an example

Simple Problem

There are tons of use cases for a Singleton.

Maybe there’s a SOQL query you need to run often throughout a transaction. For example, if you’re using SObject’s for configuration or metadata-like data.

Maybe you’re regularly access Custom Metadata Type records for calculations.

Maybe you’re accessing & using data from Platform Cache

All of these (and way more) are legit …

Right now, I’m going to show another use case I regularly rely on the Singleton Pattern for. 🥁 

Invoking a call to Schema.getGlobalDescribe(); 🫢 

Map<string, Schema.SObjectType> describeResult = Schema.getGlobalDescribe();

If you’re a Salesforce dev you’re probably already dreading the performance impact on this one.

Especially if it’s invoked multiple times in a single transaction. 🤦 

Or what about if the invocation is placed inside of a loop! ⛔️ 

These types of hidden defects in a codebase can become major issues when the org needs to scale.

Just a little bit of Singleton code will help enormously here.

Show me the code!

The Solution

Here’s how I’ve implemented this Singleton before:

Step 1 - Create a public class. I always like to name my things clearly so I’ve gone with GlobalDescribeSingleton 

Step 2 - Declare a private static variable of type GlobalDescribeSingleton

Step 3 - Declare a public get/private set variable of type Map<string, Schema.SObjectType>

Step 4 - Create a private constructor and populate your Map variable from Step 3

Step 5 - Create a public static method returning your class type that checks if your variable from Step 2 has already been initialized before returning it.

public class GlobalDescribeSingleton {
    private static GlobalDescribeSingleton instance = null;
    public Map<string, Schema.SObjectType> describeResult { get; private set; }
    
    private GlobalDescribeSingleton() {
        describeResult = Schema.getGlobalDescribe();
    }

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

Wrapping it up

Takeaway

That’s really it. There’s not any more to it that that. 🤷 

When a client calls this code it guarantees only a single instance of the class exists and that the getGlobalDescribe() is invoked only once! 💥 

Speaking of a client. Now you can call this code in your application and you automatically pick up all the benefits! 👇️ 

GlobalDescribeSingleton.getInstance()

SOUL FOOD

Today’s Principle

"Indeed, no one can thwart the purposes of your mind - for they can’t be touched by fire, steel, tyranny, slander, or anything.”

Marcus Aurelius

and now....Salesforce Memes

What did you think about today's newsletter?

Login or Subscribe to participate in polls.