#UpExperts is back! This week Lea shares Salesforce design patterns every developer should know!
In Salesforce development, design patterns help ensure code is scalable, maintainable, and follows best practices in the multi-tenant, cloud-based architecture of the platform.
Below, we can list some of important salesforce design patterns.
Trigger Handler Pattern (Trigger Framework)
This design keeps triggers clean, improve readability, supports scalability and simplifies unit testing.
Pattern: Use a separate handler class to contain logic, with one trigger per object.
Example:
- Trigger
- Trigger Handler Class
Advanced Enhancements: It’s a good option to use a Base Trigger Handler which can be reuse by another Trigger.
Example:
- Base Trigger handler
- Trigger Handler Class: Who will extend the base trigger class
Service Layer Pattern
Encapsulates business logic into a dedicated class (often used with Apex controllers). This class will centralize business logic and can be embed inside controllers, triggers, or other places.
This promotes separation of concerns, improves testability, and supports code reuse and maintainability.
To allow recognize easy the business logic class, we used this nomenclature: Object Name + ‘Service’.
Example: OpportunityService
- Trigger or Lightning controller:
- Service Class
Unit of Work Pattern
Collects DML operations and executes them in bulk, improving performance and respecting limits. Often implemented via frameworks
Prevents DML inside loops, handles rollbacks and orchestration.
Example:
- UnitOfWork:
- How to use it
Factory Pattern
Provides a way to create objects (often mock objects or service implementations) without tightly coupling code.
Promotes dependency injection and easier unit testing.
The main responsibilities are:
- Decouple object creation from usage.
- Handle multiple related classes with the same interface.
- Simplify object creation logic in one place.
- Improve code maintainability and scalability.
Strategy Pattern
It is a behavioural design pattern used to define a family of algorithms, encapsulate each one, and make them interchangeable. It allows the algorithm to vary independently from the clients that use it.
In Salesforce (Apex), you can implement the Strategy pattern to make your code more flexible, testable, and maintainable—especially when dealing with different business rules or calculations that may change frequently.
Singleton Pattern
It is a creational design pattern that ensures a class has only one instance and provides a global point of access to that instance. In Salesforce (Apex), it’s commonly used to manage shared resources like caching, configuration settings, or utility classes that maintain state during a transaction.
In Apex, static variables are reset after each transaction. However, within the same transaction (like during a trigger invocation), the Singleton pattern helps ensure that:
- You only create an object once.
- You avoid redundant queries or computations.
- You preserve state across different trigger contexts or method calls.
Example
- Basic singleton Pattern in Apex:
- Usage:
These patterns are essential to know—but choosing the right one always depends on your specific project needs. Balancing performance, scalability, and development effort is key.
At UpCRM, we’ve worked with projects of all sizes and complexities. Whether you’re starting fresh or refactoring existing code, our team can help you design smart, future-ready Salesforce solutions.
Need support? We’re just a message away:
👉 Contact us