"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > command

command

Published on 2024-11-04
Browse:841

Command pattern is one of the behavioral patterns, where a request is wrapped inside an object as command and that object is passed to an invoker, the invoker then looks for an appropriate object that can handle this command and passes the command to the corresponding object, the object then executes the command.

This also follows the OCP solid principle

Let's take an example of the stock market, where Stock is a request that should be bought or sold(command), this Stock is wrapped under Order, this Order is then sent to the broker (Invoker), the broker then analyses the Order to decide whether this is BuyOrder or SellOrder and finally Buy or sell order is performed(execution of the command/request)

command

Stock (request)

public class Stock {
    private String name ;
    private int quantity;
    public Stock(String n, int q){
        this.name = n;
        this.quantity = q;
    }
    public void sell(){
        System.out.println("[Sell order of quantity "  quantity  " for stock " name  " has been performed]");
    }
    public void buy(){
        System.out.println("[Buy order of quantity "  quantity  " for stock " name  " has been performed]");
    }
}

Order (request wrapped inside an object as a command)

public interface Order {
    public void execute();
}

Concrete Orders

public class BuyOrder implements Order {
    private Stock stock;
    public BuyOrder(Stock s){
        this.stock = s;
    }
    @Override
    public void execute(){
        stock.buy();
    }
}

public class SellOrder implements Order {
    private Stock stock;
    public SellOrder(Stock s){
        this.stock = s;
    }
    @Override
    public void execute(){
        stock.sell();
    }
}

Broker(Invoker that chooses appropriate Object that can handle the command/Order)

import java.util.ArrayList;
import java.util.List;

public class Broker {
    List orders;
    public Broker(){
        orders = new ArrayList();
    }

    public void addOrder(Order e){
        orders.add(e);
    }

    public void placeOrder(){
        for(Order e : orders){
            e.execute();
        }
        orders.clear();// once all the orders are placed by the broker then, the list should be emptied
    }
}

Main

public class Main {
    public static void main(String args[]){
        //requests
        Stock stock = new Stock("TCS",20);
        Stock stock2 = new Stock("Infy",10);
        //requests wrapped inside object(order) as commands
        Order order1 = new BuyOrder(stock);
        Order order2 = new SellOrder(stock2);

        //order is sent to the broker 
        Broker broker = new Broker();
        broker.addOrder(order1);
        broker.addOrder(order2);

        //broker at runtime decides the appropriate Object for the reference Order
        //in other words the invokers decide which object is appropriate and can handle this command/Order
        broker.placeOrder();
    }
}

Output:

[Buy order of quantity 20 for stock TCS has been performed]
[Sell order of quantity 10 for stock Infy has been performed]

Key points for this pattern

  • Follows OCP, and SIP solid principles.
  • Type of order is known at runtime
Release Statement This article is reproduced at: https://dev.to/prashantrmishra/command-p16?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3