The command of the design pattern

Source: Internet
Author: User

If the boss orders you to complete an OA project is an order, then look at its features:

1. In the above command, the executor of the command must be smart for you. The specific execution method, either through vs or through eclipse, appears to be that the command has to have a command executor and a command execution method.

2, the order of the issuer is obviously the boss, the boss also has a way to send you, probably by telephone to you, may also give you an email to say, may also be a meeting to you to say. So the issuer of the order must have an order and a method to be issued.

3, finally look at the command, the command has a name, the command must be executed. And the command is executed after the boss gives you notice.

definition: encapsulates a request as an object, allowing you to parameterize the client with different requests, queue requests, or log request logs, which can provide undo and redo functions for the command.

It mainly solves the problem: in the process of software construction, "behavior requestor" and "behavior realization person" usually presents a "tight coupling" problem

Command mode can be applied to
A) The entire invocation process is cumbersome, or there are many such calls. At this point, the command class is used to encapsulate the call and facilitate the reuse of the function.
b) Some processing of call parameters is required before and after the call.
c) Some additional processing needs to be done before and after the call, such as log, cache, record history operation, etc.

The command mode has the following effects:
A) decouple the object that invokes the operation and the object that knows how to implement the operation.
b) command is the first-class object. They can be manipulated and extended like other objects.
c) You can assemble multiple commands into a single compliant command.
D) It is easy to add a new command because it does not need to change the existing class.

2.UML

3. Code

Public interface Command {
public void execute ();
}


public class Concretecommand implements Command {

Private receiver receiver = NULL;
Private String State;

Public Concretecommand (receiver receiver) {
This.receiver = receiver;
}
public void execute () {
Receiver.action ();
}
}


public class Receiver {
public void action () {
function code that actually executes the command operation
}
}


public class Invoker {
Private command command = NULL;

public void SetCommand (Command command) {
This.command = command;
}

public void RunCommand () {
Command.Execute ();
}
}

public class Client {
public void assemble () {
Create a recipient
Receiver receiver = new receiver ();
Create a Command object, set its recipient
Command command = new Concretecommand (receiver);
Create a invoker and set the Command object in
Invoker Invoker = new Invoker ();
Invoker.setcommand (command);
}
}
Here is an example of analog to the operation of the TV has a boot, shutdown, change the command. The code is as follows

Command recipient
public class Tv {
public int currentchannel = 0;

public void TurnOn () {
System.out.println ("The Televisino is on.");
}

public void turnoff () {
System.out.println ("The television is off.");
}

public void Changechannel (int channel) {
This.currentchannel = Channel;
System.out.println ("Now TV Channel is" + channel);
}
}

Interface for executing commands
Public interface Command {
void execute ();
}

Boot command
public class Commandon implements Command {
Private Tv myTv;

Public Commandon (TV TV) {
MYTV = TV;
}

public void execute () {
Mytv.turnon ();
}
}

Shutdown command
public class Commandoff implements Command {
Private Tv myTv;

Public Commandoff (TV TV) {
MYTV = TV;
}

public void execute () {
Mytv.turnoff ();
}
}

Channel Toggle Command
public class Commandchange implements Command {
Private Tv myTv;

private int channel;

Public Commandchange (TV TV, int channel) {
MYTV = TV;
This.channel = Channel;
}

public void execute () {
Mytv.changechannel (channel);
}
}

Can be seen as a remote.
public class Control {
Private Command OnCommand, Offcommand, Changechannel;

Public Control (command on, command off, command channel) {
OnCommand = on;
Offcommand = off;
Changechannel = Channel;
}

public void TurnOn () {
Oncommand.execute ();
}

public void turnoff () {
Offcommand.execute ();
}

public void Changechannel () {
Changechannel.execute ();
}
}

Test class
public class Client {
public static void Main (string[] args) {
Command recipient
TV myTv = new TV ();
Boot command
Commandon on = new Commandon (MYTV);
Shutdown command
Commandoff off = new Commandoff (MYTV);
Channel Toggle Command
Commandchange channel = new Commandchange (myTv, 2);
Command Control Object
Control control = new control (on, off, channel);

Boot
Control.turnon ();
Switching channels
Control.changechannel ();
Shutdown
Control.turnoff ();
}
}


The result of the execution is:
The Televisino is on.
Now TV Channel is 2
The television is off.


Reference: http://www.cnblogs.com/devinzhang/archive/2012/01/06/2315235.html
Http://www.cnblogs.com/lzhp/p/3395320.html
http://haolloyin.blog.51cto.com/1177454/339076/
http://blog.csdn.net/jason0539/article/details/45110355
Http://www.cnblogs.com/ikuman/archive/2013/08/06/3233092.html

The command of the design pattern

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.