1. Definition:
Encapsulates a request into an object so that different requests, queues, or logs are used to parameterize other objects, and the command pattern supports revocable operations.
Commands can be used to implement logs and transaction systems.
2. Example:
1) Requirements: Design an appliance remote control API, the remote control has 7 programmable slots, each slot has a corresponding switch button, there is also a undo button, used to undo the operation of the previous step.
2) Code implementation:
A) command implementation
/* Remote command Interface, implement the remote Control action */public interface command {public void execute (); public void undo (); /* Empty object, used to return a meaningless object */public class Nocommand implements command{ @Override public Void execute () { } @ Override public void Undo () { }}
b) Command object
public class Light { private String name; Public Light (String name) { this.name = name; } Public Light () { } publicly void Lighton () { System.out.println (name + "Light is on!"); } public void Lightoff () { System.out.println (name + "Light is off!");} } public class Lightoncommand implements Command {Light light = new Light (); Public Lightoncommand (Light) { this.light = light; } @Override public Void execute () { Light.lighton (); } @Override public void Undo () { light.lightoff (); }} public class Lightoffcommand implements Command {Light light = new Light (); Public Lightoffcommand (Light) { this.light = light; } @Override public Void execute () { light.lightoff (); } @Override public void Undo () { Light.lighton (); }}
c) Command Object management
public class Remotecontrol {command[] oncommands; Command[] Offcommands; Command Undocommand; Public Remotecontrol () {oncommands = new command[7]; Offcommands = new Command[7]; Command Nocommand = new Nocommand (); for (int i=0;i<7;i++) {oncommands[i] = Nocommand; Offcommands[i] = Nocommand; } Undocommand = Nocommand; } public void SetCommand (int slot,command oncommand,command offcommand) {Oncommands[slot] = OnCommand; Offcommands[slot] = Offcommand; } public void onbuttonwaspushed (int slot) {oncommands[slot].execute (); Undocommand = Oncommands[slot]; } public void offbuttonwaspushed (int slot) {offcommands[slot].execute (); Undocommand = Offcommands[slot]; } public void undobuttonwaspushed () {Undocommand.undo (); } @Override Public String toString () {StringBuffer strbuff = new StringBuffer (); Strbuff.append ("\n===========remote control=============\n "); for (int i=0;i<oncommands.length;i++) {strbuff.append ("[Slot" +i+ "]" +oncommands[i].getclass (). GetName () + "" +offcommands[i].getclass (). GetName () + "\ n"); } return strbuff.tostring (); }}
D) test the code:
public class RemoteLoader {public static void main (string[] args) {Remotecontrol Remotecontrol = new Remotecon Trol (); Light livingroomlight = new Light ("living"); Light kitchenlight = new Light ("Kitchen"); Light washingroomlight = new Light ("washing"); Lightoncommand Livingroomlighton = new Lightoncommand (livingroomlight); Lightoffcommand Livingroomlightoff = new Lightoffcommand (livingroomlight); Lightoncommand Kitchenlighton = new Lightoncommand (kitchenlight); Lightoffcommand Kitchenlightoff = new Lightoffcommand (kitchenlight); Lightoncommand Washingroomlighton = new Lightoncommand (washingroomlight); Lightoffcommand Washingroomlightoff = new Lightoffcommand (washingroomlight); Remotecontrol.setcommand (0,livingroomlighton,livingroomlightoff); Remotecontrol.setcommand (1,kitchenlighton,kitchenlightoff); Remotecontrol.setcommand (2,washingroomlighton,washingroomlightoff); System.Out.println (Remotecontrol); remotecontrol.onbuttonwaspushed (0); Remotecontrol.onbuttonwaspushed (1); Remotecontrol.onbuttonwaspushed (2); remotecontrol.offbuttonwaspushed (0); Remotecontrol.offbuttonwaspushed (1); Remotecontrol.offbuttonwaspushed (2); Remotecontrol.undobuttonwaspushed (); }}
Test results:
===========remote control=============
[Slot 0]com.zte.common.utils.designpattern.commandpattern.lightoncommand Com.zte.common.utils.DesignPattern.CommandPattern.LightOffCommand
[Slot 1]com.zte.common.utils.designpattern.commandpattern.lightoncommand Com.zte.common.utils.DesignPattern.CommandPattern.LightOffCommand
[Slot 2]com.zte.common.utils.designpattern.commandpattern.lightoncommand Com.zte.common.utils.DesignPattern.CommandPattern.LightOffCommand
[Slot 3]com.zte.common.utils.designpattern.commandpattern.nocommand Com.zte.common.utils.DesignPattern.CommandPattern.NoCommand
[Slot 4]com.zte.common.utils.designpattern.commandpattern.nocommand Com.zte.common.utils.DesignPattern.CommandPattern.NoCommand
[Slot 5]com.zte.common.utils.designpattern.commandpattern.nocommand Com.zte.common.utils.DesignPattern.CommandPattern.NoCommand
[Slot 6]com.zte.common.utils.designpattern.commandpattern.nocommand Com.zte.common.utils.DesignPattern.CommandPattern.NoCommand
Living-on!
Kitchen Light is on!
Washing-on!
Living-off!
Kitchen Light is off!
Washing-off!
Washing-on!
Java design mode---command mode