Design Mode (14)-behavior-command mode)

Source: Internet
Author: User
Overview

Encapsulate a request as an object so that you can parameterize the customer with different requests, queue requests or record request logs, and support unrecoverable operations.

Applicability

  • Abstract The Action to be executed to parameterize an object. You can use the callback (c a l B A C K) function in the process language to express this parameterization mechanism. A callback function is a function that is registered at a certain place and will be called at a later time.The c o m a n d mode is an object-oriented alternative to the callback mechanism..
  • Specify, arrange, and execute requests at different times. A c o m a n d object can have a lifetime unrelated to the initial request. If the recipient of a request can be expressed in a way unrelated to the address space, the command object responsible for the request can be sent to another different process and implemented there.
  • Cancel the operation.The c o m a n d e x c u t e operation can store the status before performing the operation. This status is used to eliminate the impact of the operation when the operation is canceled. The c o m a n d interface must add a u n e x e c u t e operation, which cancels the effect of the last e x e c u t e call. The executed command is stored in a history list. You can traverse this list backward and forward and call u n e x e c u t e and redo ".
  • Logs can be modified so that the modifications can be redone when the system crashes.Add the loading and storage operations to the c o m a n d interface to maintain a consistent change log. Recovery from a crash involves re-reading the recorded commands from the disk and re-executing them with the e x e c u t e operation.
  • Construct a system with high-level operations built on primitive operations. Such a structure is common in information systems that support transactions (t r a n s a c t I o n.A transaction encapsulates a group of changes to the data. The c o m a n d mode provides a method for modeling transactions. C o m a n d has a common interface that allows you to call all transactions in the same way. At the same time, it is easy to add new transactions to expand the system.
Structure

Participants

1. Command declares the interface for executing the operation. 2. concretecommand binds a receiver object to an action. Call the corresponding operation of the receiver to execute. 3. The client creates a specific command object and sets its receiver. 4. invoker requires this command to execute this request. 5. The aggreger knows how to perform operations related to executing a request. Any class can be used as a receiver.

Example

package com.sql9.actioned;class Receiver {    public void request() {        System.out.println("receiver here does request.");    }}abstract class Command {    protected Receiver receiver;    public Command(Receiver receiver) {        this.receiver = receiver;    }    public abstract void execute();}class ConcreteCommand extends Command {    public ConcreteCommand(Receiver receiver) {        super(receiver);    }    @Override    public void execute() {        receiver.request();        System.out.println("command executed in ConcreteCommand");        // you can do something none of this    }}class AnotherCommand extends Command {    public AnotherCommand(Receiver receiver) {        super(receiver);    }        private void doSomeOtherWork() {        System.out.println("Do some other work in AnotherCommand");    }        @Override    public void execute() {        // ignore receiver        doSomeOtherWork();    }    }class Invoker {    private Command command;    public void setCommand(Command command) {        this.command = command;    }    public void execute() {        command.execute();    }}public class CommandTest {    public static void main(String[] args) {        Receiver receiver = new Receiver();        Invoker invoker = new Invoker();        invoker.setCommand(new ConcreteCommand(receiver));        invoker.execute();        invoker.setCommand(new AnotherCommand(receiver));        invoker.execute();    }}

Result

receiver here does request.command executed in ConcreteCommandDo some other work in AnotherCommand

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.