The Command Pattern is a behavioral design pattern that allows you to encapsulate a request as an object, thereby allowing you to parameterize clients with different requests, queue or log requests, and support undoable operations. This pattern is useful in many scenarios where you need to decouple the requester of an action from the actual receiver of that action.
In this blog post, we'll explore the Command Pattern in detail and provide a practical example of how it can be used in a Salesforce application.
Overview of the Command Pattern
The Command Pattern is based on the principle of separating the request for an action from its actual execution. This is done by encapsulating the request as an object that contains all the necessary information needed to perform the action.
The key components of the Command Pattern are:
Command: This is an interface or abstract class that defines the common operations for all concrete command classes. It usually contains an execute() method that performs the actual action.
Concrete Command: This is a class that implements the Command interface and contains the actual logic for performing the action.
Invoker: This is a class that holds the Command object and invokes its execute() method when necessary.
Receiver: This is a class that contains the logic for performing the action.
Client: This is a class that creates the Command object and sets its receiver.
Example of the Command Pattern in Salesforce
Let's say you have a custom object called "Order__c" that tracks customer orders. You want to create a Command Pattern to handle the business logic of your application, such as creating new orders, updating existing orders, and calculating totals.
1. Create a Command Interface: First, you need to create a Command interface that defines the common operations for all concrete command classes.
public interface Command {
void execute();
}
2. Create Concrete Command Classes: Next, you need to create concrete command classes that implement the Command interface and contain the actual logic for performing the action.
public class CreateAccountCommand implements Command {
private Account account;
public CreateAccountCommand(AccountReceiver account_receiver) {
this.account = account_receiver.account;
}
@Override
public void execute() {
insert account;
}
}
3. Invoker: We create an invoker object that has a setCommand() method to set the Command object and an executeCommand() method to invoke the Command's execute() method:
public class AccountCreator {
private Command command;
public void setCommand(Command command) {
this.command = command;
}
public void createAccount() {
command.execute();
}
}
4. Create a Receiver Class: Finally, you need to create a Receiver class that contains the logic for performing the action.
public class AccountReceiver {
public Account account {get;set;}
public AccountReceiver(String name, Double amount) {
this.account = new Account();
this.account.name = name;
this.account.amount = amount;
}
}
Now, we can use the Command Design Pattern to create an Account object. Here's an example:
AccountReceiver account = new AccountReceiver('Test Account', 1000.0);
CreateAccountCommand createAccountCommand = new CreateAccountCommand(account);
AccountCreator accountCreator = new AccountCreator();
accountCreator.setCommand(createAccountCommand);
accountCreator.createAccount();
In this example, we created an Account object and encapsulated the logic for creating an Account object using the Command Design Pattern. We created a CreateAccountCommand object, passed the Account object to the constructor, and set it as the Command object of the AccountCreator. Finally, we invoked the createAccount() method of the AccountCreator to execute the Command's execute() method, which creates the Account object.
The Command Pattern is a powerful design pattern that allows you to decouple the requester of an action from the actual receiver of that action. It provides a way to encapsulate business logic as objects and allows you to easily queue, log, or undo operations.
I appreciate your efforts in producing great content , Keep posting more information https://nareshit.com/salesforce-online-training/