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-elseorswitchstatements 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:
- Wasted governor limits
- Unnecessary memory usage
- 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.

π 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!