Apex Design Pattern

Apex Design Patterns are go-to solutions for handling common problems smartly when building apps on the Salesforce platform.

Why Apex Design Pattern?

Pick the right design pattern, and your Apex code becomes easier to reuse β€” not just for today’s needs, but for whatever comes next.

πŸ” Commonly Used Design Patterns in Apex

When building scalable and maintainable applications on the Salesforce platform, these design patterns can help structure your code effectively:

  • Singleton Pattern – Ensures a class has only one instance and provides a global point of access to it. Useful for managing shared resources like configuration or caching logic.
  • Strategy Pattern – Enables you to select an algorithm or behavior at runtime. Ideal for replacing long if-else or switch statements with cleaner, interchangeable logic.
  • Factory Pattern – Provides a way to instantiate objects without exposing the creation logic to the client. Great for decoupling object creation from business logic.
  • Unit of Work Pattern – Tracks a set of changes and commits them together. Helps in managing multiple DML operations in a single transaction and avoiding DML in loops.
  • Decorator Pattern – Adds new responsibilities to objects dynamically. Useful for extending behavior of classes without modifying the original code.
  • Facade Pattern – Provides a simplified interface to a complex system. Helps in abstracting multiple subsystems into a unified API for easier use.

Singleton Pattern

🧩 Ever Faced This Problem in Apex?

Have you ever run into a situation where anyone can instantiate a utility or service class by calling its constructor β€” even though it’s already been initialized once in the same transaction?

This often leads to:

  1. Wasted governor limits
  2. Unnecessary memory usage
  3. Inconsistent behavior

If that sounds familiar, the Singleton Pattern is exactly what you need. It ensures that only one instance of a class exists per transaction, even if multiple parts of your code try to initialize it. Instead of repeating logic or state, you centralize and reuse a single shared object.

πŸ› οΈ How to Implement the Singleton Pattern in Apex

βœ… Make the constructor private

This prevents other classes from directly creating instances using new.

βœ… Create a static method to manage the instance

Inside this method:

  • Check if an instance already exists
  • If it does, return it
  • If not, create it and return the newly created instance

The example below demonstrates how to initialize a class only once per transaction, regardless of how many times it’s accessed β€” using the Singleton Pattern.

This image has an empty alt attribute; its file name is image-6.png

πŸ”œ Up Next: The Strategy Pattern in Apex

Struggling with too many if-else blocks in your code? In my next post, I’ll break down the Strategy Pattern β€” a powerful way to handle dynamic logic like discounts, lead scoring, or validation rules without clutter.

I’ll share a real Salesforce use case, reusable Apex code, and how this pattern can make your services and trigger logic more scalable.

πŸ‘‰ Stay tuned β€” or better yet, [subscribe/follow] to get notified when it drops!