Command Pattern(命名模式)C#源代码

本文通过实现一个遥控器的例子展示了命令设计模式的应用。遥控器上有多个按钮,每个按钮对应一个命令,可以控制不同的家用电器执行打开或关闭操作。文章详细介绍了各个类的设计与实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Command
  6. {
  7.     class CRunMain
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int i = 0;
  12.             //ctrl command
  13.             CRemote objRemote= new CRemote(6);
  14.             //work object
  15.             CLight objLightLivingRoom = new CLight("Living Room");
  16.             CLight objLightKitch = new CLight("Kitch");
  17.             CStereo objStereoLivingRoom = new CStereo("Living Room");
  18.             CCeilingFan objCeilingFan = new CCeilingFan("Living Room");
  19.             CGarageDoor objGrageDoor = new CGarageDoor("Garage");
  20.             CHottub objHottu = new CHottub();
  21.             //commad object
  22.             CLightOnCmd objLightLivingRoomOn = new CLightOnCmd(objLightLivingRoom);
  23.             CLightOnCmd objLightLivingRoomOff = new CLightOnCmd(objLightLivingRoom);
  24.             CLightOnCmd objLightKitchOn = new CLightOnCmd(objLightKitch);
  25.             CLightOnCmd objLightKitchOff = new CLightOnCmd(objLightKitch);
  26.             CGarageDoorUpCmd objGarageDoorUp = new CGarageDoorUpCmd(objGrageDoor);
  27.             CGarageDoorDownCmd objGarageDorrDown = new CGarageDoorDownCmd(objGrageDoor);
  28.             CCmdStereoOnWithCD objStereoOnWithCD = new CCmdStereoOnWithCD(objStereoLivingRoom);
  29.             CCmdStereoOffWithCD objStereoOffWithCD = new CCmdStereoOffWithCD(objStereoLivingRoom);
  30.             CCeilingFanOnCmd objCeilingFangOn = new CCeilingFanOnCmd(objCeilingFan);
  31.             CCeilingFanOffCmd objCeilingFangOff = new CCeilingFanOffCmd(objCeilingFan);
  32.             CHottubOnCmd objHottubOn = new CHottubOnCmd(objHottu);
  33.             CHottubOffCmd objHottubOff = new CHottubOffCmd(objHottu);
  34.             //set command object
  35.             objRemote.SetCmd(0, objLightLivingRoomOn, objLightLivingRoomOff);
  36.             objRemote.SetCmd(1, objLightKitchOn, objLightKitchOff);
  37.             objRemote.SetCmd(2, objGarageDoorUp, objGarageDorrDown);
  38.             objRemote.SetCmd(3, objStereoOnWithCD, objStereoOffWithCD);
  39.             objRemote.SetCmd(4, objCeilingFangOn, objCeilingFangOff);
  40.             objRemote.SetCmd(5, objHottubOn, objHottubOff);           
  41.             //execute event
  42.             for (i = 0; i < 6; i++)
  43.             {
  44.                 Console.WriteLine(objRemote.BtnOnWasPushed(i));
  45.                 Console.WriteLine(objRemote.BtnOffWasPushed(i));
  46.             }
  47.         }
  48.     };
  49.     public class CStereo
  50.     {
  51.         public CStereo(string strLocation)
  52.         {
  53.             this.m_strLocation = strLocation;
  54.         }
  55.         public string On()
  56.         {
  57.             return this.m_strLocation + "stereo is on";
  58.         }
  59.         public string Off()
  60.         {
  61.             return this.m_strLocation + "stereo if off";
  62.         }
  63.         public string SetCD()
  64.         {
  65.             return this.m_strLocation + "stereo is set for CD input";
  66.         }
  67.         public string SetDVD()
  68.         {
  69.             return this.m_strLocation + "stereo is set for DVD input";
  70.         }
  71.         public string SetRadio()
  72.         {
  73.             return this.m_strLocation + "stereo is set for Radio";
  74.         }
  75.         public string SetVolume(int nVolume)
  76.         {
  77.             return this.m_strLocation + "stereo volume set to " + nVolume;
  78.         }
  79.         protected string m_strLocation;
  80.     };
  81.     public interface ICommand
  82.     {
  83.         object Execute();
  84.     };
  85.     public class CCmdStereoOnWithCD : ICommand
  86.     {
  87.         public CCmdStereoOnWithCD(CStereo objStereo)
  88.         {
  89.             this.m_objStereo = objStereo;
  90.         }
  91.         public object Execute()
  92.         {
  93.             return this.m_objStereo.On() + "/n" + this.m_objStereo.SetCD() + "/n"
  94.                 + this.m_objStereo.SetVolume(11);
  95.         }
  96.         private CStereo m_objStereo;
  97.     };
  98.     public class CCmdStereoOffWithCD : ICommand
  99.     {
  100.         public CCmdStereoOffWithCD(CStereo objStereo)
  101.         {
  102.             this.m_objStereo = objStereo;
  103.         }
  104.         public object Execute()
  105.         {
  106.             return this.m_objStereo.Off();
  107.         }
  108.         private CStereo m_objStereo;
  109.     }
  110.     public class CLight
  111.     {
  112.         public CLight(string strLocation)
  113.         {
  114.             this.m_strLocation = strLocation;
  115.         }
  116.         public string On()
  117.         {
  118.             return this.m_strLocation + " light is on";
  119.         }
  120.         public string Off()
  121.         {
  122.             return this.m_strLocation + " light is off";
  123.         }
  124.         private string m_strLocation;
  125.     };
  126.     public class CNoCmd : ICommand
  127.     {
  128.         public CNoCmd()
  129.         {
  130.         }
  131.         public object Execute()
  132.         {
  133.             return null;
  134.         }
  135.     };
  136.     public class CRemote
  137.     {
  138.         public CRemote(int nCmd)
  139.         {
  140.             int i = 0;
  141.             CNoCmd objNoCmd = new CNoCmd();
  142.             this.m_arrCmdOff = new ICommand[nCmd];
  143.             this.m_arrCmdOn = new ICommand[nCmd];
  144.             for (i = 0; i < nCmd; i++)
  145.             {
  146.                 this.m_arrCmdOn[i] = objNoCmd;
  147.                 this.m_arrCmdOff[i] = objNoCmd;
  148.             }
  149.         }
  150.         public void SetCmd(int nIndex, ICommand objCmdOn, ICommand objCmdOff)
  151.         {
  152.             if ((nIndex >= m_arrCmdOn.Length) || (nIndex < 0))
  153.             {
  154.                 return;
  155.             }
  156.             this.m_arrCmdOn[nIndex] = objCmdOn;
  157.             this.m_arrCmdOff[nIndex] = objCmdOff;
  158.         }
  159.         public object BtnOnWasPushed(int nIndex)
  160.         {
  161.             if (nIndex >= this.m_arrCmdOn.Length)
  162.             {
  163.                 return null;
  164.             }
  165.             return this.m_arrCmdOn[nIndex].Execute();
  166.         }
  167.         public object BtnOffWasPushed(int nIndex)
  168.         {
  169.             if (nIndex >= this.m_arrCmdOff.Length)
  170.             {
  171.                 return null;
  172.             }
  173.             return this.m_arrCmdOff[nIndex].Execute();
  174.         }
  175.         public override  string ToString()
  176.         {
  177.             int i = 0;
  178.             StringBuilder strbd = new StringBuilder();
  179.             strbd.Append("/n----------- Remote Control --------------/n");
  180.            
  181.             for (i = 0; i < this.m_arrCmdOn.Length; i++)
  182.             {
  183.                 strbd.Append("[slot" + i + "]" + this.m_arrCmdOn.GetType().Name 
  184.                     + "/t" + this.m_arrCmdOff[i].GetType().Name + "/n");
  185.             }
  186.             return strbd.ToString();
  187.         }
  188.         private ICommand[] m_arrCmdOn;
  189.         private ICommand[] m_arrCmdOff;
  190.     };
  191.     public class CLightOnCmd : ICommand
  192.     {
  193.         public CLightOnCmd(CLight objLight)
  194.         {
  195.             this.m_objLight = objLight;
  196.         }
  197.         public object Execute()
  198.         {
  199.             return this.m_objLight.On();
  200.         }
  201.         private CLight m_objLight;
  202.     };
  203.     public class CLightOffCmd : ICommand
  204.     {
  205.         public CLightOffCmd(CLight objLight)
  206.         {
  207.             this.m_objLight = objLight;
  208.         }
  209.         public object Execute()
  210.         {
  211.             return this.m_objLight.Off();
  212.         }
  213.         private CLight m_objLight;
  214.     };
  215.     public class CHottub
  216.     {
  217.         public CHottub()
  218.         {
  219.         }
  220.         public bool On()
  221.         {
  222.             this.m_bOn = true;
  223.             return this.m_bOn;
  224.         }
  225.         public bool Off()
  226.         {
  227.             this.m_bOn = false;
  228.             return this.m_bOn;
  229.         }
  230.         public string BubblesOn()
  231.         {
  232.             if (this.m_bOn)
  233.             {
  234.                 return "Hottub is bubbling!";
  235.             }
  236.             return null;
  237.         }
  238.         public string BubblesOff()
  239.         {
  240.             if (this.m_bOn)
  241.             {
  242.                 return "Hottub is not bubbling!";
  243.             }
  244.             return null;
  245.         }
  246.         public string JetsOn()
  247.         {
  248.             if (this.m_bOn)
  249.             {
  250.                 return "Hottub jets are on!";
  251.             }
  252.             return null;
  253.         }
  254.         public string JetsOff()
  255.         {
  256.             if (this.m_bOn)
  257.             {
  258.                 return "Hottub jets are not on!";
  259.             }
  260.             return null;
  261.         }
  262.         public int SetTemperature(int nTemperature)
  263.         {
  264.             this.m_nTemperature = nTemperature;
  265.             return this.m_nTemperature;
  266.         }
  267.         public string Heat()
  268.         {
  269.             this.m_nTemperature = 105;
  270.             return "Hottub is heating to a steaming 105 degrees";
  271.         }
  272.         public string Cool()
  273.         {
  274.             this.m_nTemperature = 98;
  275.             return "Hottub is cooling to 98 degrees";
  276.         }
  277.         private bool m_bOn;
  278.         private int m_nTemperature;
  279.     };
  280.     public class CHottubOnCmd : ICommand
  281.     {
  282.         public CHottubOnCmd(CHottub objHottub)
  283.         {
  284.             this.m_objHottub = objHottub;
  285.         }
  286.         public object Execute()
  287.         {
  288.             return this.m_objHottub.On() + "/n" + this.m_objHottub.Heat() + "/n" + this.m_objHottub.BubblesOn();
  289.         }
  290.         private CHottub m_objHottub;
  291.     }
  292.     
  293.     public class CHottubOffCmd : ICommand
  294.     {
  295.         public CHottubOffCmd(CHottub objHottub)
  296.         {
  297.             this.m_objHottub = objHottub;
  298.         }
  299.         public object Execute()
  300.         {
  301.             return this.m_objHottub.Off();
  302.         }
  303.         private CHottub m_objHottub;
  304.     }
  305.     public class CGarageDoor
  306.     {
  307.         public CGarageDoor(string strLocation)
  308.         {
  309.             this.m_strLocation = strLocation;
  310.         }
  311.         public string Up()
  312.         {
  313.             return "Garage door is up";
  314.         }
  315.         public string Down()
  316.         {
  317.             return "Garage door is down";
  318.         }
  319.         public string Stop()
  320.         {
  321.             return "Garage door movement is stopped";
  322.         }
  323.         public string LightOn()
  324.         {
  325.             return "Garage door light is on";
  326.         }
  327.         public string LightOff()
  328.         {
  329.             return "Garage door light is off";
  330.         }
  331.         private string m_strLocation;
  332.     };
  333.     public class CGarageDoorUpCmd : ICommand
  334.     {
  335.         public CGarageDoorUpCmd(CGarageDoor objGarageDoor)
  336.         {
  337.             this.m_objGarageDoor = objGarageDoor;
  338.         }
  339.         public object Execute()
  340.         {
  341.             return this.m_objGarageDoor.Up();
  342.         }
  343.         private CGarageDoor m_objGarageDoor;
  344.     };
  345.     public class CGarageDoorDownCmd : ICommand
  346.     {
  347.         public CGarageDoorDownCmd(CGarageDoor objGarageDoor)
  348.         {
  349.             this.m_objGarageDoor = objGarageDoor;
  350.         }
  351.         public object Execute()
  352.         {
  353.             return this.m_objGarageDoor.Down();
  354.         }
  355.         private CGarageDoor m_objGarageDoor;
  356.     };
  357.     public class CCeilingFan
  358.     {
  359.         public CCeilingFan(string strLocation)
  360.         {
  361.             this.m_strLoation = strLocation;
  362.             this.m_nLevel = 0;
  363.         }
  364.         public string High()
  365.         {
  366.             this.m_nLevel = HIGH;
  367.             return this.m_strLoation + " ceiling fan is on high";
  368.         }
  369.         public string Medium()
  370.         {
  371.             this.m_nLevel = MEDIUM;
  372.             return this.m_strLoation + " ceiling fan is on medium";
  373.         }
  374.         public string Low()
  375.         {
  376.             this.m_nLevel = LOW;
  377.             return this.m_strLoation + " ceiling fan is on low";
  378.         }
  379.         public string Off()
  380.         {
  381.             this.m_nLevel = 0;
  382.             return this.m_strLoation + " ceiling fan is off";
  383.         }
  384.         public int GetSpeed()
  385.         {
  386.             return this.m_nLevel;
  387.         }
  388.         private string m_strLoation;
  389.         private int m_nLevel;
  390.         public const int HIGH = 2;
  391.         public const int MEDIUM = 1;
  392.         public const int LOW = 0;
  393.     };
  394.     public class CCeilingFanOnCmd : ICommand
  395.     {
  396.         public CCeilingFanOnCmd(CCeilingFan objCeilingFan)
  397.         {
  398.             this.m_objCeilingFan = objCeilingFan;
  399.         }
  400.         public object Execute()
  401.         {
  402.             return this.m_objCeilingFan.High();
  403.         }
  404.         private CCeilingFan m_objCeilingFan;
  405.     }
  406.     public class CCeilingFanOffCmd : ICommand
  407.     {
  408.         public CCeilingFanOffCmd(CCeilingFan objCeilingFan)
  409.         {
  410.             this.m_objCeilingFan = objCeilingFan;
  411.         }
  412.         public object Execute()
  413.         {
  414.             return this.m_objCeilingFan.Off();
  415.         }
  416.         private CCeilingFan m_objCeilingFan;
  417.     }
  418. };
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值