Pius Adams Ijachi
3 min readJan 12, 2024

Command Design Pattern

The command pattern is a behavioral design pattern that allows you to encapsulate a request as an object and execute it later. It is useful when you want to decouple the sender and the receiver of the request, or when you want to implement undo/redo functionality.

Some advantages of the command pattern are:

  1. It makes your code more modular and reusable, as you can create different commands for different actions and use them interchangeably.
  2. It simplifies the logic of the sender, as it only needs to invoke the command object without knowing the details of the receiver or the action.
  3. It allows you to implement undo/redo functionality by storing the command objects in a stack and reversing their execution.

The command pattern is similar to the strategy pattern, but they have some differences. The strategy pattern defines a family of algorithms and lets you choose one at runtime, while the command pattern defines a set of actions and lets you execute them later. The strategy pattern is used to change the behavior of an object, while the command pattern is used to change the state of an object.

To implement the command pattern in PHP, you need to create three components:

  1. A command interface that defines a common method for executing the command.
  2. Concrete command classes that implement the command interface contain the logic for acting.
  3. An invoker class that holds a reference to a command object and invokes its execute method.

For example, we want to create a simple e-commerce system that allows users to create and cancel orders. We can use the command pattern to implement this functionality as follows:

// The command interface
interface Command
{
public function execute();
}
// The concrete command classes
class CreateOrderCommand implements Command
{
private $orderData;
private $orderService;
public function __construct($orderData, OrderService $orderService)
{
$this->orderData = $orderData;
$this->orderService = $orderService;
}
public function execute()
{
// Call the order service to create an order with the given data
$this->orderService->createOrder($this->orderData);
}
}
class CancelOrderCommand implements Command
{
private $orderId;
private $orderService;
public function __construct($orderId, OrderService $orderService)
{
$this->orderId = $orderId;
$this->orderService = $orderService;
}
public function execute()
{
// Call the order service to cancel an order with the given id
$this->orderService->cancelOrder($this->orderId);
}
}
// The invoker class
class OrderInvoker
{
private $command;
public function __construct(Command $command)
{
$this->command = $command;
}
public function execute()
{
// Invoke the execute method of the command object
$this->command->execute();
}
}
// The order service class that handles the business logic
class OrderService
{
public function createOrder($orderData)
{
// Validate and save the order data in a database
echo "Order created with data: " . json_encode($orderData) . "\n";
}
public function cancelOrder($orderId)
{
// Delete the order data from the database
echo "Order canceled with id: " . $orderId . "\n";
}
}
// The client code that uses the invoker and the commands// Create a command object for creating an order
$createOrder = new CreateOrderCommand(
[
'product_id' => 1,
'quantity' => 2,
'customer_id' => 1
],
new OrderService()
);
// Create an invoker object and pass it the command object
$orderButton = new OrderInvoker(
$createOrder
);
// Execute the command through the invoker
$orderButton->execute();
// Create a command object for canceling an order
$canceledOrder = new CancelOrderCommand(
1,
new OrderService()
);
// Create another invoker object and pass it the command object
$orderButton = new OrderInvoker(
$canceledOrder
);
// Execute the command through the invoker
$orderButton->execute();

That’s all folks,I hope you found it useful and informative. If you have any questions or feedback, please leave a comment below. Thank you for reading!

GitHub link

https://www.youtube.com/watch?v=9qA5kw8dcSU

Pius Adams Ijachi
Pius Adams Ijachi

No responses yet