Java Command design pattern detailed _java

Source: Internet
Author: User

Pass a request from the client to an object so that you can parameterize the customer with different requests. The decoupling of the behavior requester and the behavior performer enables loose coupling between the two to accommodate the change. Separation of changes and invariant factors.

I. Role
Command
Defines the interface of a command that declares the method of execution.
Concretecommand
The command interface implements the object, which is a "virtual" implementation, usually holding the receiver and invoking the receiver's function to complete the action to be performed by the command.
Receiver
Receiver, the object that actually executes the command. Any class can be a recipient, as long as it implements the functionality required by the command.
Invoker
Requires a command object to execute a request, usually holding a command object and holding a large number of command objects. This is where the client actually triggers the command and asks the command to perform the appropriate action, which is equivalent to using the Command object's entry.
Client
Creates a specific command object and sets the recipient of the Command object. Note that this is not our regular client, but rather the assembly of the Command object and the receiver, perhaps it would be better to refer to this client as an assembler, since the clients that actually use the command trigger execution from the invoker.

Second, the advantages

    • 1. Reduce the coupling between objects.
    • 2. New commands can be easily added to the system.
    • 3. A combination command can be designed more easily.
    • 4. Invoke the same method to implement different functions

Third, shortcomings
Using command mode may cause some systems to have too many specific command classes. Because you need to design a specific command class for each command, some systems may require a large number of specific command classes, which will affect the use of command patterns.

Iv. Application

    • The system needs to decouple the requesting caller and the request receiver so that the caller and receiver do not interact directly.
    • The system needs to specify the request at different times, queue the request, and execute the request.
    • The system needs to support the undo (undo) operation and the recovery (Redo) operation of the command.
    • The system needs to combine a set of actions to support macro commands.

V. Application
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 is 2
The television is off.

Vi. Summary
1. The essence of the command mode is to encapsulate the order, separating the responsibility for issuing the order and the responsibility for executing the order.
2. Each command is an operation: the requesting party makes a request to perform an action, the receiving party receives the request, and performs the action.
3. The command mode allows the requesting party to be independent of the receiving party so that the requesting party does not have to know the interface of the party receiving the request, not to mention how the request was received, and whether the operation was executed, when it was executed, and how it was executed.
4. The command mode makes the request itself an object that can be stored and delivered as well as other objects.
5. The key to command mode is the introduction of the abstract command interface, and the sender for the abstract Command interface programming, only the implementation of the abstract command interface to the specific command to be associated with the receiver.

The above is about the Java command design mode of detailed introduction, I hope to help you learn.

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.