- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Command
- {
- class CRunMain
- {
- static void Main(string[] args)
- {
- int i = 0;
- //ctrl command
- CRemote objRemote= new CRemote(6);
- //work object
- CLight objLightLivingRoom = new CLight("Living Room");
- CLight objLightKitch = new CLight("Kitch");
- CStereo objStereoLivingRoom = new CStereo("Living Room");
- CCeilingFan objCeilingFan = new CCeilingFan("Living Room");
- CGarageDoor objGrageDoor = new CGarageDoor("Garage");
- CHottub objHottu = new CHottub();
- //commad object
- CLightOnCmd objLightLivingRoomOn = new CLightOnCmd(objLightLivingRoom);
- CLightOnCmd objLightLivingRoomOff = new CLightOnCmd(objLightLivingRoom);
- CLightOnCmd objLightKitchOn = new CLightOnCmd(objLightKitch);
- CLightOnCmd objLightKitchOff = new CLightOnCmd(objLightKitch);
- CGarageDoorUpCmd objGarageDoorUp = new CGarageDoorUpCmd(objGrageDoor);
- CGarageDoorDownCmd objGarageDorrDown = new CGarageDoorDownCmd(objGrageDoor);
- CCmdStereoOnWithCD objStereoOnWithCD = new CCmdStereoOnWithCD(objStereoLivingRoom);
- CCmdStereoOffWithCD objStereoOffWithCD = new CCmdStereoOffWithCD(objStereoLivingRoom);
- CCeilingFanOnCmd objCeilingFangOn = new CCeilingFanOnCmd(objCeilingFan);
- CCeilingFanOffCmd objCeilingFangOff = new CCeilingFanOffCmd(objCeilingFan);
- CHottubOnCmd objHottubOn = new CHottubOnCmd(objHottu);
- CHottubOffCmd objHottubOff = new CHottubOffCmd(objHottu);
- //set command object
- objRemote.SetCmd(0, objLightLivingRoomOn, objLightLivingRoomOff);
- objRemote.SetCmd(1, objLightKitchOn, objLightKitchOff);
- objRemote.SetCmd(2, objGarageDoorUp, objGarageDorrDown);
- objRemote.SetCmd(3, objStereoOnWithCD, objStereoOffWithCD);
- objRemote.SetCmd(4, objCeilingFangOn, objCeilingFangOff);
- objRemote.SetCmd(5, objHottubOn, objHottubOff);
- //execute event
- for (i = 0; i < 6; i++)
- {
- Console.WriteLine(objRemote.BtnOnWasPushed(i));
- Console.WriteLine(objRemote.BtnOffWasPushed(i));
- }
- }
- };
- public class CStereo
- {
- public CStereo(string strLocation)
- {
- this.m_strLocation = strLocation;
- }
- public string On()
- {
- return this.m_strLocation + "stereo is on";
- }
- public string Off()
- {
- return this.m_strLocation + "stereo if off";
- }
- public string SetCD()
- {
- return this.m_strLocation + "stereo is set for CD input";
- }
- public string SetDVD()
- {
- return this.m_strLocation + "stereo is set for DVD input";
- }
- public string SetRadio()
- {
- return this.m_strLocation + "stereo is set for Radio";
- }
- public string SetVolume(int nVolume)
- {
- return this.m_strLocation + "stereo volume set to " + nVolume;
- }
- protected string m_strLocation;
- };
- public interface ICommand
- {
- object Execute();
- };
- public class CCmdStereoOnWithCD : ICommand
- {
- public CCmdStereoOnWithCD(CStereo objStereo)
- {
- this.m_objStereo = objStereo;
- }
- public object Execute()
- {
- return this.m_objStereo.On() + "/n" + this.m_objStereo.SetCD() + "/n"
- + this.m_objStereo.SetVolume(11);
- }
- private CStereo m_objStereo;
- };
- public class CCmdStereoOffWithCD : ICommand
- {
- public CCmdStereoOffWithCD(CStereo objStereo)
- {
- this.m_objStereo = objStereo;
- }
- public object Execute()
- {
- return this.m_objStereo.Off();
- }
- private CStereo m_objStereo;
- }
- public class CLight
- {
- public CLight(string strLocation)
- {
- this.m_strLocation = strLocation;
- }
- public string On()
- {
- return this.m_strLocation + " light is on";
- }
- public string Off()
- {
- return this.m_strLocation + " light is off";
- }
- private string m_strLocation;
- };
- public class CNoCmd : ICommand
- {
- public CNoCmd()
- {
- }
- public object Execute()
- {
- return null;
- }
- };
- public class CRemote
- {
- public CRemote(int nCmd)
- {
- int i = 0;
- CNoCmd objNoCmd = new CNoCmd();
- this.m_arrCmdOff = new ICommand[nCmd];
- this.m_arrCmdOn = new ICommand[nCmd];
- for (i = 0; i < nCmd; i++)
- {
- this.m_arrCmdOn[i] = objNoCmd;
- this.m_arrCmdOff[i] = objNoCmd;
- }
- }
- public void SetCmd(int nIndex, ICommand objCmdOn, ICommand objCmdOff)
- {
- if ((nIndex >= m_arrCmdOn.Length) || (nIndex < 0))
- {
- return;
- }
- this.m_arrCmdOn[nIndex] = objCmdOn;
- this.m_arrCmdOff[nIndex] = objCmdOff;
- }
- public object BtnOnWasPushed(int nIndex)
- {
- if (nIndex >= this.m_arrCmdOn.Length)
- {
- return null;
- }
- return this.m_arrCmdOn[nIndex].Execute();
- }
- public object BtnOffWasPushed(int nIndex)
- {
- if (nIndex >= this.m_arrCmdOff.Length)
- {
- return null;
- }
- return this.m_arrCmdOff[nIndex].Execute();
- }
- public override string ToString()
- {
- int i = 0;
- StringBuilder strbd = new StringBuilder();
- strbd.Append("/n----------- Remote Control --------------/n");
- for (i = 0; i < this.m_arrCmdOn.Length; i++)
- {
- strbd.Append("[slot" + i + "]" + this.m_arrCmdOn.GetType().Name
- + "/t" + this.m_arrCmdOff[i].GetType().Name + "/n");
- }
- return strbd.ToString();
- }
- private ICommand[] m_arrCmdOn;
- private ICommand[] m_arrCmdOff;
- };
- public class CLightOnCmd : ICommand
- {
- public CLightOnCmd(CLight objLight)
- {
- this.m_objLight = objLight;
- }
- public object Execute()
- {
- return this.m_objLight.On();
- }
- private CLight m_objLight;
- };
- public class CLightOffCmd : ICommand
- {
- public CLightOffCmd(CLight objLight)
- {
- this.m_objLight = objLight;
- }
- public object Execute()
- {
- return this.m_objLight.Off();
- }
- private CLight m_objLight;
- };
- public class CHottub
- {
- public CHottub()
- {
- }
- public bool On()
- {
- this.m_bOn = true;
- return this.m_bOn;
- }
- public bool Off()
- {
- this.m_bOn = false;
- return this.m_bOn;
- }
- public string BubblesOn()
- {
- if (this.m_bOn)
- {
- return "Hottub is bubbling!";
- }
- return null;
- }
- public string BubblesOff()
- {
- if (this.m_bOn)
- {
- return "Hottub is not bubbling!";
- }
- return null;
- }
- public string JetsOn()
- {
- if (this.m_bOn)
- {
- return "Hottub jets are on!";
- }
- return null;
- }
- public string JetsOff()
- {
- if (this.m_bOn)
- {
- return "Hottub jets are not on!";
- }
- return null;
- }
- public int SetTemperature(int nTemperature)
- {
- this.m_nTemperature = nTemperature;
- return this.m_nTemperature;
- }
- public string Heat()
- {
- this.m_nTemperature = 105;
- return "Hottub is heating to a steaming 105 degrees";
- }
- public string Cool()
- {
- this.m_nTemperature = 98;
- return "Hottub is cooling to 98 degrees";
- }
- private bool m_bOn;
- private int m_nTemperature;
- };
- public class CHottubOnCmd : ICommand
- {
- public CHottubOnCmd(CHottub objHottub)
- {
- this.m_objHottub = objHottub;
- }
- public object Execute()
- {
- return this.m_objHottub.On() + "/n" + this.m_objHottub.Heat() + "/n" + this.m_objHottub.BubblesOn();
- }
- private CHottub m_objHottub;
- }
- public class CHottubOffCmd : ICommand
- {
- public CHottubOffCmd(CHottub objHottub)
- {
- this.m_objHottub = objHottub;
- }
- public object Execute()
- {
- return this.m_objHottub.Off();
- }
- private CHottub m_objHottub;
- }
- public class CGarageDoor
- {
- public CGarageDoor(string strLocation)
- {
- this.m_strLocation = strLocation;
- }
- public string Up()
- {
- return "Garage door is up";
- }
- public string Down()
- {
- return "Garage door is down";
- }
- public string Stop()
- {
- return "Garage door movement is stopped";
- }
- public string LightOn()
- {
- return "Garage door light is on";
- }
- public string LightOff()
- {
- return "Garage door light is off";
- }
- private string m_strLocation;
- };
- public class CGarageDoorUpCmd : ICommand
- {
- public CGarageDoorUpCmd(CGarageDoor objGarageDoor)
- {
- this.m_objGarageDoor = objGarageDoor;
- }
- public object Execute()
- {
- return this.m_objGarageDoor.Up();
- }
- private CGarageDoor m_objGarageDoor;
- };
- public class CGarageDoorDownCmd : ICommand
- {
- public CGarageDoorDownCmd(CGarageDoor objGarageDoor)
- {
- this.m_objGarageDoor = objGarageDoor;
- }
- public object Execute()
- {
- return this.m_objGarageDoor.Down();
- }
- private CGarageDoor m_objGarageDoor;
- };
- public class CCeilingFan
- {
- public CCeilingFan(string strLocation)
- {
- this.m_strLoation = strLocation;
- this.m_nLevel = 0;
- }
- public string High()
- {
- this.m_nLevel = HIGH;
- return this.m_strLoation + " ceiling fan is on high";
- }
- public string Medium()
- {
- this.m_nLevel = MEDIUM;
- return this.m_strLoation + " ceiling fan is on medium";
- }
- public string Low()
- {
- this.m_nLevel = LOW;
- return this.m_strLoation + " ceiling fan is on low";
- }
- public string Off()
- {
- this.m_nLevel = 0;
- return this.m_strLoation + " ceiling fan is off";
- }
- public int GetSpeed()
- {
- return this.m_nLevel;
- }
- private string m_strLoation;
- private int m_nLevel;
- public const int HIGH = 2;
- public const int MEDIUM = 1;
- public const int LOW = 0;
- };
- public class CCeilingFanOnCmd : ICommand
- {
- public CCeilingFanOnCmd(CCeilingFan objCeilingFan)
- {
- this.m_objCeilingFan = objCeilingFan;
- }
- public object Execute()
- {
- return this.m_objCeilingFan.High();
- }
- private CCeilingFan m_objCeilingFan;
- }
- public class CCeilingFanOffCmd : ICommand
- {
- public CCeilingFanOffCmd(CCeilingFan objCeilingFan)
- {
- this.m_objCeilingFan = objCeilingFan;
- }
- public object Execute()
- {
- return this.m_objCeilingFan.Off();
- }
- private CCeilingFan m_objCeilingFan;
- }
- };
Command Pattern(命名模式)C#源代码
最新推荐文章于 2025-08-12 14:27:48 发布