6. Command mode

Source: Internet
Author: User

Command pattern definition: Encapsulates a request into an object so that different requests, queues, or logs are used to parameterize other objects.
  • The command mode decouples the requesting object (Remotecontrol remote control Class) from the object executing the request (illumination Class);
  • Communicate between the decoupled objects by command object (for example, the light class Lightcommand and the Turn off command);
  • The caller makes a request by invoking the command execute (), and execute () invokes the actual performer;
  • The caller can receive command arguments, even dynamically at run time (dynamically binds the command to the caller's remote, and the caller's remote is programmed for the interface);
  • Command can support undo undo operation, on the caller (remote control) can record the last executed command, call undo undo the last command;
  • Command mode enables logging and queuing systems:
Log system: Each command executed can be logged to the log system, when the system crashes or freezes, the system can be re-executed according to the command of the log system to restore; Queue system: Work queue, the actual work object and the caller decoupling, such as: thread pool, etc.; Defining a command interface: a way to standardize the implementation of each command
 Public Interface Command {    void  execute ();     void undo ();}
Define LAMP class: Under the light includes open and close two commands.
 Public class Light {    private  String name;      Public Light (String name) {        this. Name = name;    }       Public void On () {        System.out.println ("open Light");    }      Public void off () {        System.out.println ("Off Light");}    }
Define the Light command class: The light-on and off-lights command for encapsulating lights.
 Public class Implements Command {    private light light ;      Public Lightcommand (light) {        this. Light = light ;    }    @Override    publicvoid  execute () {        light.on ();    }    @Override    publicvoid  undo () {        light.off ();    }}
Defines the Nocommand command for numeric keys that do not have commands on the remote.
 Public class Implements Command {    public  Nocommand () {    }    @Override    publicvoid  Execute () {        System.out.println ("no command opened");    }    @Override    publicvoid  undo () {        System.out.println ("no Close Command") ;    }}
Defines the remote control class: Defines an array of commands to be stored, and two commands for opening and closing a user;
 Public classRemotecontrol {command[] commands;  PublicRemotecontrol () {commands=NewCommand[10];  for(inti=0;i<10;i++) {Commands[i]=NewNocommand (); }    }     Public voidSetcommands (intSlot,command OnCommand) {Commands[slot]=OnCommand; }     Public voidOnbutton (intslot)    {Commands[slot].execute (); }     Public voidOffbutton (intslot)    {Commands[slot].undo (); }}
Test class:
 Public classTest { Public Static voidMain (string[] args) { light light=NewLight ("Electric bulb"); Lightcommand Lightcommand=NewLightcommand (light); Remotecontrol Control=NewRemotecontrol (); Control.setcommands (0, Lightcommand); Control.onbutton (0); Control.offbutton (0); Control.onbutton (1); Control.offbutton (1); }} results: Turn off the lights without opening the command without closing the command

6. 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.