Design mode-command pattern Multi-command specific explanation

Source: Internet
Author: User

command Pattern Multi-command specific explanation


This address: Http://blog.csdn.net/caroline_wendy


Reference Command mode: http://blog.csdn.net/caroline_wendy/article/details/31379977


Specific steps:

1. multiple commands , the unused command is initialized to an empty object (Nocommand), according to the parameters (slot), select the output command.

/** * @time June 16, 2014 */package command;/** * @author C.l.wang * */public class Remotecontrol {command[] oncommands;//Open C Ommand[] Offcommands; Off public Remotecontrol () {oncommands = new command[7];offcommands = new Command[7]; Command Nocommand = new Nocommand (); for (int i=0; i<7; ++i) {//initialize oncommands[i] = nocommand;offcommands[i] = Nocommand ;}} public void SetCommand (int slot, command oncommand, command Offcommand) {This.oncommands[slot] = Oncommand;this.offcomma Nds[slot] = Offcommand;} public void onbuttonwaspushed (int slot) {oncommands[slot].execute ();} public void offbuttonwaspushed (int slot) {offcommands[slot].execute ();} Public String toString () {StringBuffer stringbuffer = new StringBuffer (); Stringbuffer.append ("\ n------Remote Control-- ----\ n "); for (int i=0; i<oncommands.length; ++i) {stringbuffer.append (" [slot "+ i +"] "+ oncommands[i].getclass (). GE Tname () + "" + Offcommands[i].getclass (). GetName () + "\ n"); return stringbuffer.tostring ();}}

2. empty Command (Nocommand) object, Inherit command interface.

Package Command;public class Nocommand implements command {public void execute () {}}

3. The test object is given a multi-command Assignment (SetCommand), which is called by the number of parameters (slot) .

/** * @time June 16, 2014 */package command;import javax.crypto.spec.ivparameterspec;/** * @author C.l.wang * */public class RemoteLoader {/** * @param args */public static void main (string[] args) {//TODO auto-generated method Stubremotecontrol Remotecontrol = new Remotecontrol (); Light livingroomlight = new Light ("Living"); Light kitchenlight = new Light ("Kitchen"); Ceilingfan Ceilingfan = new Ceilingfan ("Living"); Garagedoor Garagedoor = new Garagedoor (""); Stereo Stereo = new Stereo ("Living"); Lightoncommand Livingroomlighton = new Lightoncommand (livingroomlight); Lightoffcommand Livingroomlightoff = new Lightoffcommand (livingroomlight); Lightoncommand Kitchenlighton = new Lightoncommand (kitchenlight); Lightoffcommand Kitchenlightoff = new Lightoffcommand (kitchenlight); Ceilingfanoncommand Ceilingfanon = new Ceilingfanoncommand (Ceilingfan); Ceilingfanoffcommand Ceilingfanoff = new Ceilingfanoffcommand (Ceilingfan); Garagedooroncommand Garagedooron = new Garagedooroncommand (GaragedoOR); Garagedooroffcommand Garagedooroff = new Garagedooroffcommand (Garagedoor); Stereoonwithcdcommand STEREOONWITHCD = new Stereoonwithcdcommand (stereo); Stereooffcommand Stereooffcommand = new Stereooffcommand (stereo); Remotecontrol.setcommand (0, Livingroomlighton, Livingroomlightoff); Remotecontrol.setcommand (1, Kitchenlighton, Kitchenlightoff); Remotecontrol.setcommand (2, Ceilingfanon, Ceilingfanoff); Remotecontrol.setcommand (3, STEREOONWITHCD, Stereooffcommand); System.out.println (Remotecontrol); remotecontrol.onbuttonwaspushed (0); remotecontrol.offbuttonwaspushed (0); Remotecontrol.onbuttonwaspushed (1); remotecontrol.offbuttonwaspushed (1); remotecontrol.onbuttonwaspushed (2); Remotecontrol.offbuttonwaspushed (2); remotecontrol.onbuttonwaspushed (3); remotecontrol.offbuttonwaspushed (3);}}


4. Output:

------Remote Control------[slot 0] command. Lightoncommand    command. Lightoffcommand[slot 1] command. Lightoncommand    command. Lightoffcommand[slot 2] command. Ceilingfanoncommand    command. Ceilingfanoffcommand[slot 3] command. Stereoonwithcdcommand    command. Stereooffcommand[slot 4] command. Nocommand    command. Nocommand[slot 5] command. Nocommand    command. Nocommand[slot 6] command. Nocommand    command. Nocommandliving-Onliving-the-is-offkitchen-light-is-onkitchen-is-offliving---------ceiling fan is On highliving-ceiling fan is offliving-Stereo is onliving-stereo-is-set for CD inputliving-Stereo Me set to 11Living the stereo is off



The rest of the code download: http://download.csdn.net/detail/u012515223/7506695






Design mode-command pattern Multi-command specific explanation

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.