Skip to main content

Apex design patterns: Singleton, Strategy, Decorator, Facade, Composite, Bulk State Transition

About Us
Published by jetbi2
24 February 2023
486

Apex design patterns are reusable solutions to commonly occurring programming problems that are specifically tailored for the Salesforce Platform. These patterns allow developers to write efficient, reliable, and scalable code that follows best practices and improves the overall quality of their applications.

Let’s review each of these patterns in more detail!

 

Singleton

The Singleton pattern is a widely used design pattern that ensures that there is only one instance of a class throughout the lifetime of an application. The Singleton pattern is useful when you need to share an object across different parts of your code, but you don't want to create multiple instances of the same object.

In Apex, you can implement the Singleton pattern using a private constructor and a static method that returns an instance of the class. Here is an example:

1. Abstract class AbstractPickListValues.cls

apex-design-patterns

 

2. Singleton implementation AccountPickListValues.cls

apex-design-pattern1

 

3. Call Singleton class from Anonymous apex

apex-design-patterns-2

In conclusion, the Singleton pattern is a useful design pattern in Apex that can help you manage instances of objects throughout your application. With the implementation of a private constructor and a static method that returns an instance of the class, you can ensure that only one instance of the class exists throughout the lifetime of the application.

 

Strategy

The strategy pattern ensures that different solutions are available for the same problem. It chooses the appropriate solution based on the user input at the runtime. This design pattern is also called the policy pattern. When do we want to use the strategy pattern? We want to use the strategy pattern whenever we have a deeply nested switch or conditional if-else statement within our application’s core logic.

1. Interface IWheatherService.cls

apex-design-patterns-3

 

2. EuropeForcastType.cls which implements IWeatherService.cls

apex-design-patterns-4

 

3. AmericaForcastType.cls which implements IWeatherService.cls

apex-design-patterns-5

 

4. WheatherFactory.cls

apex-design-patterns-6

In conclusion, the Strategy pattern is a useful design pattern in Apex that can help you encapsulate different algorithms and make them interchangeable. With the implementation of an interface that defines the contract for the different strategies and separate classes that implement each strategy, you can change the behavior of an object at runtime without changing its implementation.

 

Decorator

The Decorator pattern is a structural design pattern that allows you to add new behaviors to an object dynamically by wrapping it with another object that has the desired behavior. The Decorator pattern provides a flexible alternative to subclassing for extending functionality.

In Apex, you can implement the Decorator pattern using a base class or interface that defines the common functionality, and then create decorator classes that wrap the base object and add new functionality. Here is an example:

1. DecoratorExample.cls

apex-design-patterns-7

 

2. Call DecoratorExample.cls from Anonymous Apex

apex-design-patterns-8

In conclusion, the Decorator pattern is a useful design pattern in Apex that can help you add new behaviors to an object dynamically. With the implementation of a base class or interface that defines the common functionality and decorator classes that wrap the base object and add new functionality, you can extend the functionality of an object without changing its implementation.

 

Facade

The Facade pattern is a structural design pattern that provides a simplified interface to a complex system or set of objects. It allows you to encapsulate the complexity of a system and provide a simpler interface for clients to interact with. In this article, we will discuss the implementation of the Facade pattern in Apex.

In Apex, you can implement the Facade pattern using a single class that provides a simplified interface to a complex subsystem. Here is an example:

1. OrderFacade.cls

apex-design-patterns-9
 

2. ShippingService.cls

apex-design-patterns-10

 

3. PaymentService.cls

apex-design-patterns-11

 

4. OrderService.cls

apex-design-patterns-12

 

5. Call OrderFacade.cls from Anonymous Apex

apex-design-patterns-13

In conclusion, the Facade pattern is a useful design pattern in Apex that can help you simplify the interface to a complex system or set of objects. With the implementation of a single class that provides a simplified interface to a complex subsystem, you can encapsulate the complexity of the system and provide a simpler interface for clients to interact with.

 

Composite

Composite is a structural design pattern that lets you compose objects into tree structures and then work with these structures as if they were individual objects.

The composite design pattern treats each node in two ways:

  • Composite – Composite means it can have other objects below it.
  • leaf – leaf means it has no objects below it.

1. Employee.cls

apex-design-patterns-14

 

2. CompositePatternDemo

apex-design-patterns-15

 

3. Call CompositePatternDemo.cls from Anonymous Apex

apex-design-patterns-16

Overall, the Composite pattern provides a flexible way to work with complex hierarchical structures in Apex. By using this pattern, you can easily apply operations to both individual components and groups of components without needing to treat them differently.

 

Bulk State Transition

The Bulk State Transition pattern is a design pattern used in Apex, the programming language used in Salesforce, that is particularly useful for streamlining processes that require changes to be made to multiple records in one go. This pattern is often used in conjunction with triggers to automate processes and improve the efficiency of Salesforce applications.

Here's some sample code that demonstrates this pattern:

1. AccountTrigger.trigger

apex-design-patterns-17

 

2. AccountTriggerHandler.cls

apex-design-patterns-17

 

3. TriggerDispatcher.cls

apex-design-patterns-18

 

4. ITriggerHandler.cls

apex-design-patterns-19

Overall, using the Bulk State Transition pattern in this way allows us to efficiently process multiple records and automate the follow-up process for negative feedback. By gathering all the records that need to be updated into a list, we can reduce the number of database queries required and improve the performance of our Salesforce application.

 

Conclusion

The Singleton pattern ensures that only one instance of a class exists, which can be particularly useful in managing shared resources. The Strategy pattern provides a flexible way to switch between algorithms or behaviors, which can be particularly useful in managing complex business rules. The Decorator pattern allows for the addition of new functionality to existing objects without changing their code, which can be particularly useful in managing complex application logic. The Facade pattern provides a simplified interface to complex subsystems, which can be particularly useful in managing complex integrations. The Composite pattern provides a way to treat groups of objects and individual objects in the same way, which can be particularly useful in managing complex hierarchies. And the Bulk State Transition pattern provides a way to process multiple records efficiently, which can be particularly useful in managing complex processes that involve many records.

By using these patterns, developers can write more efficient, scalable, and maintainable code that meets the needs of their users and stakeholders.


Pavel Siarzhantau
Salesforce Developer
image
Expertise
image

We have available resources to start working on your project within 5 business days

1 UX Designer

image

1 Admin

image

2 QA engineers

image

1 Consultant

image

Steps following request submission

schema

icon

After receiving your request, we analyze it and we offer free online meeting slots (via email) so that we can discuss your needs in as much detail as possible

icon

We begin gathering all necessary requirements to create comprehensive estimates, including timelines, resource allocations, risk assessments, and underlying assumptions.

icon

Once all preparations are in place, we will initiate the project and move forward with the planned tasks