Command Pattern: Decoupling Command Issuers from Executors
Overview
The Command Pattern encapsulates requests as command objects, allowing parameterization of clients with different requests, queuing of requests, and supporting undoable operations. The pattern decouples the object invoking the operation from the one that knows how to perform it.
Consider a typical project scenario: a client needs to coordinate multiple teams (SA, UI, Development) to handle requirements. Without a mediating layer, the client must directly communicate with each team, creating tight coupling.
Base Abstraction for Teams
public abstract class Team {
public abstract void addRequirement(String id, String details);
public abstract void removeRequirement(String id);
public abstract void reschedule();
}
Concrete Team Implementations
public class Developer extends Team {
@Override
public void addRequirement(String id, String details) {
System.out.println("Writing code: " + details);
}
@Override
public void removeRequirement(String id) {
System.out.println("Removing code for requirement: " + id);
}
@Override
public void reschedule() {
System.out.println("Adjusting development timeline");
}
}
public class BusinessAnalyst extends Team {
@Override
public void addRequirement(String id, String details) {
System.out.println("Adding requirement: " + details);
}
@Override
public void removeRequirement(String id) {
System.out.println("Removing requirement: " + id);
}
@Override
public void reschedule() {
System.out.println("Updating project timeline");
}
}
public class Designer extends Team {
@Override
public void addRequirement(String id, String details) {
System.out.println("Designing interface: " + details);
}
@Override
public void removeRequirement(String id) {
System.out.println("Removing design for requirement: " + id);
}
@Override
public void reschedule() {
System.out.println("Redrawing design schedule");
}
}
The Coupling Problem
Without the Command Pattern, clients must directly interact with each team:
public class ClientApp {
public static void main(String[] args) {
Team analyst = new BusinessAnalyst();
analyst.addRequirement("1", "API optimization");
analyst.reschedule();
Team designer = new Designer();
designer.addRequirement("1", "Modify page layout");
designer.reschedule();
Team dev = new Developer();
dev.addRequirement("1", "Optimize API endpoints");
dev.reschedule();
}
}
This approach forces the client to know which team handles which task, creating tight coupling.
Command Abstraction
The solution introduces a command layer that abstracts the execution logic:
public abstract class Command {
protected Developer developer = new Developer();
protected Designer designer = new Designer();
protected BusinessAnalyst analyst = new BusinessAnalyst();
public abstract void execute();
}
Invoker: The Coordinator
public class Coordinator {
private Command command;
public Coordinator(Command command) {
this.command = command;
}
public void dispatch() {
command.execute();
}
}
Concrete Commands
public class NewRequirementCommand extends Command {
@Override
public void execute() {
super.designer.addRequirement("1", "New landing page");
super.developer.addRequirement("1", "Implement REST endpoints");
super.analyst.reschedule();
}
}
Decoupled Client Code
public class ClientApp {
public static void main(String[] args) {
Command cmd = new NewRequirementCommand();
Coordinator coordinator = new Coordinator(cmd);
coordinator.dispatch();
}
}
Pattern Benefits and Trade-offs
The Command Pattern provides several advantages:
- Decoupling: Command issuers no longer need to know which executors handle specific tasks
- Extensibility: New commands can be added without modifying existing code
- Undo/Redo Support: Commands can store state for rollback operations
- Queue Execution: Commands can be queued and executed asynchronously
However, the pattern has potential drawbacks:
- When the number of commands grows significantly, the Command class hierarchy can become large and unwieldy
- Each command object requires additional memory overhead
This pattern is particularly useful in scenarios involving task scheduling, transaction management, and macro operations that require coordination across multiple subsystems.