Salesforce Apex Design Patterns
In this article, you'll:
What Are Apex Design Patterns?
Design patterns are best practices and proven solutions to to recurring design problems encountered during software development. These design patterns serve as guiding principles, aiding in structuring and organizing code to enhance efficiency, maintainability, and scalability of applications. The patterns not only streamline the development process but also contribute to building a robust foundation for future modifications and expansions.
Design patterns can be grouped into three main categories based on their purpose and usage.
Type Of Apex Design Patterns
Here are some common patterns we will be discussing in this article:
Let's now go into the details of each design pattern.
1. Singleton Pattern
A fundamental creational design pattern, which ensures that a class has only one instance within a specific transaction context. This restriction is pivotal for scenarios where a singular instance of a class is sufficient to manage a particular resource or configuration throughout the application.
Problem Scenario
Let's see a code snippet below where we want to access application settings using a utility class. Without singleton pattern, one might unintentionally end up making many class instances. This could lead to unnecessary database queries impacting governor limits and potentially performance issues.
The Solution: Implementing Singleton Pattern
Let's modify the code snippet using the Singleton pattern. By applying the Singleton pattern, we ensure a single instance of ConfigurationManager is created. Subsequent requests for an instance will return the existing one, reducing redundant queries and enhancing efficiency.
Advantage of Singleton Pattern
2. Strategy Pattern
It’s design behavioral pattern that comes into play when multiple solutions are available for a single problem, and the choice of solution or strategy needs to be made dynamically at runtime. It offers a structure to select the most right strategy based on the specific context.
Problem Scenario
Let's see a code snippet below where we want to handle different SMS gateways based on for distinct countries, such as UAE and Pakistan. Not using the Strategy Pattern can lead to complex, hard-to-maintain code and hinder extensibility. Additionally, it can pose challenges when multiple developers collaborate on the same class.
The Solution: Implementing Strategy Pattern
Advantages of Strategy Pattern
3. Decorator Pattern
It’s is a structural design pattern that allows behavior to be added to individual objects, either statically or dynamically, without affecting the object structure.
领英推荐
Sometimes during code processing, we encounter scenarios where temporary additional fields are needed for intermediate calculations. Using the Decorator pattern, we can dynamically extend the behavior of objects by adding these transient fields at runtime, avoiding the need to create permanent custom fields in the database.
We can use decorator pattern in below example use cases.
Problem Scenario
Imagine a scenario where a developer needed to calculate a complex amount that's meant for temporary use within a single class.
Without using the Decorator Pattern, developers might opt for common solutions like creating a formula field or a currency field and later updating or calculating the value.
The Solution: Implementing Decorator Pattern
Advantages of Decorator Pattern
4. Facade Pattern
It's is a structural design pattern that provides a simplified interface to a complex system. It essentially offers a high-level view and hides the complex workings of the subsystems.
Problem Scenario
Let's see a code snippet below where a developer needed to handle email and SMS sending, account updating, and logging. In absence of Facade pattern, code code can become cluttered and hard to manage, making it prone to errors and difficult to extend or modify in the future.
The Solution: Implementing Facade Pattern
Advantages of Facade Pattern
5. Bulk State Transition Pattern
It’s a behavioral design pattern to efficiently handle the transition of multiple records from one state to another. This pattern is particularly valuable when dealing with a significant number of records and needing to update or modify them collectively based on certain conditions or triggers. Instead of processing records one by one, process them in bulk minimizing the use of database operations and enhancing performance.
Problem Scenario
Let's see a code snippet below where order object being created once opportunity is closed won. In absence of bulk state transition pattern, hitting governor limits is likely due to excessive database operations in a loop, affecting performance and potentially causing the trigger to fail for a significant number of records.
The Solution: Implementing Bulk State Transition
Advantages of Bulk State Transition Pattern
Conclusion
Apex design patterns serve as essential principles that enable developers to build software solutions that are both efficient and easy to maintain. The discussed patterns, such as Singleton, Strategy, Decorator, Facade, and Bulk State Transition, significantly enhance code quality. Integrating these patterns into development practices elevate modularity, flexibility, and overall performance, laying the foundation for resilient and flexible applications.
Dear @Waqas, Very nice article.? I do agree with @Gaurav as regards Strategy Pattern. It would be nice to show Strategy implemented with Dependency Injection. Great article. All the best.
Sr IT Architect at IQVIA | Salesforce Certified Application Architect | Salesforce Certified Integration Architect | Salesforce Certified Data Architect | 9X Salesforce Certified | 4X Trailhead Ranger | Copado Certified
1 年Nice article! However few comments - Singleton pattern :If you can add that how to make it thread safe, that would be great? E.g. getInstance() is being called from two different places and for both of them instance is null. Then will both thread not enter in the if statement and initialized it twice? How to prevent that ? Strategy Pattern: One of the main advantage of strategy pattern is , how to get rid of If and else statement. so e.g, if tomorrow you want to implement SMSSender for India , you need to write another if statement for India. I would rather change the design of SMSSender class and pass the interface variable in the SMSSender and use that to call. That way you can get rid of if and else as well. But overall you have summarized it very well!
Building Digital Experiences
1 年Thank you for Sharing Waqas Ali - Vincent Oliver, Kevin Snell