2921. Design Pattern - Command
Command Pattern


Behavioral Pattern: Command Pattern.

1. Command Pattern

The Command pattern allows requests to be encapsulated as objects, thereby allowing clients to be parametrized with different requests. Command decouples the object that invokes the operation from the one that knows how to perform it.

2. Example

2.1 Command

public interface Command {
    void execute();
}

public class Buy implements Command {
    public void execute() {
        System.out.println("Buy stock");
    }
}

public class Sell implements Command {
    public void execute() {
        System.out.println("Sell stock");
    }
}

2.2 Broker

public class Broker {
    private List<Command> cmdList = new ArrayList<>();

    public void acceptCommand(Command cmd){
        cmdList.add(cmd);
    }

    public void executeCommand(){
        for (Command cmd : cmdList) {
            cmd.execute();
        }
        cmdList.clear();
    }
}

2.3 Client

public class Client {
    public void run() {
        Buy buyStock = new Buy();
        Sell sellStock = new Sell();

        Broker broker = new Broker();
        broker.acceptCommand(buyStock);
        broker.acceptCommand(sellStock);

        broker.executeCommand();
    }
}

Output.

Buy stock
Sell stock

3. Source Files

4. References