Parsing the use of command patterns in Java design pattern programming _java

Source: Internet
Author: User
Tags abstract

definition: encapsulates a request into an object, allowing you to parameterize clients using different requests, queuing up requests, or logging request logs, to provide revocation and recovery capabilities for commands.
Type: behavior class pattern
class Diagram:

Structure of command mode
as the name implies, the command mode is the encapsulation of the command, first look at the command mode class diagram in the basic structure:
Command class: An abstract class that declares a command that needs to be executed, and generally publishes an Execute method to execute a command.
Concretecommand Class: The implementation class of the command class that implements the methods declared in an abstract class.
Client class: The End call class.
The role of the above three classes should be better understood, below we focus on the Invoker class and Recevier class.
Invoker class: Caller, responsible for invoking the command.
Receiver class: receiver, responsible for receiving commands and executing commands.
The so-called encapsulation of the command, plainly, nothing more than a series of operations written to a method, and then for the client to call on the line, reflected on the class diagram, only need a Concretecommand class and client class can complete the package of the command, even if further, in order to increase flexibility, You can add a command class to the appropriate abstraction, what does the caller and receiver really do?
In fact, we can change an angle to think: if simply to encapsulate some of the operations as a command for others to call, how can it be called a pattern? Command mode as a behavior-class pattern, the first thing to do is low coupling, low coupling to improve flexibility, and to join the caller and receiver two roles for this purpose.
Example:
simulation of the operation of the television has a boot, shutdown, change the command. The code is as follows

The interface that executes the command is public interface command {void execute (); 
 
  }//Command recipient receiver public class Tv {public int currentchannel = 0; 
  public void Turnon () {System.out.println ("The Televisino are on."); 
  The public void turnoff () {System.out.println ("the television are off."); 
    The public void Changechannel (int channel) {this.currentchannel = channel; 
  System.out.println ("Now TV Channel is" + channel); 
 
  }//power-on command Concretecommand public class Commandon implements command {private Tv MyTV; 
  Public Commandon (TV TV) {MYTV = TV; 
  public void Execute () {Mytv.turnon (); 
 
  }//Shutdown command Concretecommand public class Commandoff implements command {private Tv MyTV; 
  Public Commandoff (TV TV) {MYTV = TV; 
  public void Execute () {Mytv.turnoff (); 
 
  //Channel Switch command Concretecommand 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 considered remote Invoker 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 Client public class client {public static void main (string[] args) {//Command recipient receiver Tv MyTV = 
    New Tv (); 
    power-on command Concretecommond Commandon on = new Commandon (MYTV); 
    Shutdown command Concretecommond commandoff off = new Commandoff (MYTV); 
    Channel switch command Concretecommond commandchange channel = new Commandchange (MyTV, 2); Command-controlled object Invoker control = new Controls (on, off, ChannEL); 
    Boot Control.turnon (); 
    Switch channel Control.changechannel (); 
  Shutdown Control.turnoff (); 

 } 
}

Execution results

The Televisino is on. 
Now TV Channel are 2 the television is off 
. 

Advantages and Disadvantages of command mode
First of all, the command mode of encapsulation is very good: each command is encapsulated, for the client, what functionality is required to invoke the corresponding command, without knowing how the command specific implementation. For example, a set of file operations commands: Create a new file, copy a file, delete a file. If these three operations are encapsulated into a command class, the client only needs to know that there are three command classes, as well as the encapsulated logic in the command class, which the client does not need to know.
Second, the command mode is very extensible, and in command mode, in the receiver class is generally the most basic encapsulation of the operation, the command class through these basic operations two encapsulation, when adding new commands, the command class writing is generally not zero-based, there are a large number of receiver classes to call, There are also a number of command classes available for invocation, and code reusability is good. For example, in the operation of the file, we need to add a cut file command, then only need to copy the files and delete the two commands together on the line, very convenient.
Finally, the disadvantage of command mode, that is, command if a lot of development will be a headache. In particular, a lot of simple commands, to achieve a few lines of code, and the use of command mode, without the command more simple, you need to write a command class to encapsulate.

Scenario for Command mode
For most of the request-response mode functions, it is more appropriate to use the command mode, as the command mode definition says, Command mode is convenient for the implementation of logging, revocation and other functions.

Summarize
This is a very tangled issue for all developers, for example, the use of a pattern in a situation. Sometimes, because of the anticipation of certain changes in demand, for the flexibility and scalability of the system to use a certain design pattern, but this predictable demand is not, on the contrary, did not anticipate the demand has come a lot, resulting in the modification of the code, the use of the design model has played the opposite role, That the whole project team complained. Such examples, I believe, have been encountered by every program designer. So, based on the principles of agile development, when we design a program, if the current requirements, not using a pattern can be a good solution, then we do not introduce it, because it is not difficult to introduce a design pattern, we can really need to use when the system again to introduce this design pattern.
Take command mode, we develop a request-response pattern that is very common, and in general we encapsulate the response to a request in a method that can be called a command, but not a command pattern. Whether to take this design up to the height of the pattern is to be considered separately, because if you use the command mode, you will introduce the caller, the receiver two roles, originally placed in one of the logic dispersed into three classes, design, must consider the cost is worth it.

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.