Design mode----Command mode

Source: Internet
Author: User

In order to execute a method, it is called directly to call this method, there is a situation does not apply to the direct call method, that is, the future execution of a method system must meet a specific execution order or context, but developers can not control the order, One solution to this problem is to divide the method into one object, which is the command pattern.

Equivalent to having a herald, encapsulating the entire command, and then executing it in order of command.

Definition: A request is encapsulated as an object that allows the user to parameterize the customer with different requests, queue the request or log the request, and support the revocable operation.

Basic code:

123456789101112131415161718 publicabstract classCommand {    public abstract voidexecute();    protectedReceiver receiver;    publicCommand(Receiver receiver){        this.receiver = receiver;    }}publicclassConcreteCommand extendsCommand {    publicConcreteCommand(Receiver receiver){        super(receiver);    }    @Override    publicvoidexecute() {        receiver.action();    }}

Convey the code:

123456789 publicclassInvoker {    privateCommand command;//这里可以很方便的形成一个命令队列,对不要的命令可以删除和增加    publicvoidsetCommand(Command command) {        this.command = command;    }    public voidexecuteCommand(){        command.execute();    }}
123456 //命令执行者publicclass Receiver {    public voidaction(){        System.out.println("execute the request");    }}

Client:

1234567 public staticvoidmain(String[] args) {        Receiver receiver= newReceiver();        Command command = newConcreteCommand(receiver);        Invoker invoker = newInvoker();        invoker.setCommand(command);        invoker.executeCommand();    }

The advantage of the command pattern is obvious, first of all, the command mode can easily design a command queue, can be more convenient to record the command to the log. It can be easier to implement revocation and redo of requests by allowing the party accepting the request to decide whether to veto the request. Finally, the revocation and redo of the request can be implemented more easily.

The command mode is suitable for the following situations:

1. When you need to abstract an action to be executed in order to parameterize an object.

2. When you need to perform, arrange, and execute requests at different times.

3, when the system needs to support the cancellation operation.

4, when the system needs to record, modify the log.

Command mode can completely decouple the sender and receiver, the sender and receiver do not have a direct reference relationship, sending the requested object only need to know how to send the request, rather than know how to complete the request, this is the mode of the command mode motive.

The command pattern allows the requesting party and the receiving party to be independent of each other, which makes it unnecessary for the requesting party to know the interface of the party receiving the request, and not to know how the request was received. And whether the operation is executed, and how it is executed. The key to the command pattern is the introduction of an abstract command interface, and the sender is programmed for an abstract command interface, and only specific commands that implement an abstract command interface can be associated with the receiver.

The advantages of the command pattern are as follows:

1, reduce the coupling degree of the system.

2. New commands can be easily added to the current system.

3. It is easy to design a command queue and a combination command.

4, can easily realize the revocation and redo of the request.

?

Design mode----Command mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.