Using Command Design pattern

后端 未结 6 1140
误落风尘
误落风尘 2020-11-27 18:26

Can any one explain with simple example of Command Pattern. I refer in internet but i got confused.

6条回答
  •  日久生厌
    2020-11-27 18:51

    You can think of Command pattern workflow as follows.

    Client calls Invoker => Invoker calls ConcreteCommand => ConcreteCommand calls Receiver method, which implements abstract Command method.

    UML Diagram from dofactory article:

    Key features:

    1. Command declares an interface for all commands, providing a simple execute() method which asks the Receiver of the command to carry out an operation.

    2. The Receiver has the knowledge of what to do to carry out the request.

    3. The Invoker holds a command and can get the Command to execute a request by calling the execute method.

    4. The Client creates ConcreteCommands and sets a Receiver for the command.

    5. The ConcreteCommand defines a binding between the action and the receiver.

    6. When the Invoker calls execute the ConcreteCommand will run one or more actions on the Receiver.

    Code snippet:

    interface Command {
        void execute();
    }
    interface Receiver {
        public  void switchOn();
    
    }
    class OnCommand implements Command{
        private Receiver receiver;
    
        public OnCommand(Receiver receiver){
            this.receiver = receiver;
        }
        public void execute(){
            receiver.switchOn();
        }
    }
    class Invoker {
        private Command command;
    
        public Invoker(Command command){
            this.command = command;
        }
        public void execute(){
            this.command.execute();
        }
    }
    
    class TV implements Receiver{
    
         public void switchOn(){
            System.out.println("Switch on from TV");
        }
    }
    class DVDPlayer implements Receiver{
    
        public void switchOn(){
             System.out.println("Switch on from DVDPlayer");
        }
    }
    
    public class CommandDemoEx{
        public static void main(String args[]){
            // On command for TV with same invoker 
            Receiver receiver = new TV();
            Command onCommand = new OnCommand(receiver);
            Invoker invoker = new Invoker(onCommand);
            invoker.execute();
    
            // On command for DVDPlayer with same invoker 
            receiver = new DVDPlayer();
            onCommand = new OnCommand(receiver);
            invoker = new Invoker(onCommand);
            invoker.execute();            
        }
    }
    

    output:

    Switch on from TV
    Switch on from DVDPlayer
    

    Explanation:

    In this example,

    1. Command interface defines execute() method.
    2. OnCommand is ConcreteCommand, which implements execute() method.
    3. Receiver is an interface and implementers have to provide implementation for the methods.
    4. TV and DVDPlayer are two types of Receivers, which are passed to ConcreteCommand like OnCommand.
    5. Invoker contains Command. It's the key to de-couple Sender from Receiver.
    6. Invoker receives OnCommand -> which calls Receiver (TV) to execute this command.

    By using Invoker, you can switch on TV and DVDPlayer. If you extend this program, you switch off both TV and DVDPlayer too.

    You can use Command pattern to

    1. Decouple the sender & receiver of command

    2. Implement callback mechanism

    3. Implement undo and redo functionality

    4. Maintain a history of commands

    Have a look at this dzone and journaldev and Wikipedia articles.

    Source code as Wikipedia page is simple, cleaner and self explanatory.

    You can implement Undo and Redo if you follow the steps as quoted in this article

提交回复
热议问题