Design mode-----command design mode (upgrade----plus one undo command)

Source: Internet
Author: User


Public class Remotecontrolwithundo {

Command[] Oncommands;

Command[] Offcommands;

Command Undocommand;

Public Remotecontrolwithundo () {

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 ();



It's plain to note that the last command object is logged when you execute a close and open button. Command Undocommand; inside.


Public class Lightoffcommand implements Command {

Light light;

Public Lightoffcommand (Light) {

this. Light = light;


Public void Execute () { ();


Public void undo () {

Light.on ();




Public class Lightoncommand implements Command {

Light light;

Public Lightoncommand (Light) {

this. Light = light;


Public void Execute () {

Light.on ();


Public void undo () { ();



The undo in the command object is always the same as the original idea. This one is either.

If it's more than one command, just add a constant.




    • @author Qingruihappy
    • @data September 28, 2018 11:53:23
    • Description

* This is the * Step 1: The specification of the defined interface adds an abstract method of revocation

* Step 2: Once the command instruction object is called in the switch, the object of the command instruction is assigned to the interface as the global variable defined. To make a record.

* Step 3: Once the Undo command is invoked, the command object that makes the record is given instructions to implement the last command.

* Step 4: At this point, you also need to record the last invocation's instance method (or not) in the object of the command instruction. * Why not, because if the last time we were a fan was a stroke, and now it's turned into a high wind, now if we undo it, we know that the call is now

* Stroke of the command object, that is the method of stroke, so there is no need to record. So, this code can be optimized.


Public class Test {

Public Static void Main (String[] args) {

Remotecontrolwithundo Remotecontrol =new Remotecontrolwithundo ();

Light Livingroomlight =new Light ("Living");

Lightoncommand Livingroomlighton =new

Lightoncommand (Livingroomlight);

Lightoffcommand Livingroomlightoff =new

Lightoffcommand (Livingroomlight);

Remotecontrol.setcommand (0, Livingroomlighton, Livingroomlightoff);

Remotecontrol.onbuttonwaspushed (0);

Remotecontrol.offbuttonwaspushed (0);

Remotecontrol.undobuttonwaspushed ();




Design mode-----command design mode (upgrade----plus one undo command)

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