C#命令模式(Command Pattern)實例教程
本文以實例形式講述了C#命令模式的實現(xiàn)方法,分享給大家供大家參考。具體實現(xiàn)方法如下:
現(xiàn)假設(shè)想讓遙控器控制電燈的開關(guān)、電視機的開關(guān)和切換,該如何做?
所有的開、關(guān)、切換都是遙控器發(fā)出的指令,把這些指令統(tǒng)一抽象成一個接口。
public interface IControl
{
void Execute();
}
把電燈、電視機抽象成類。
public class Tv
{
public void TurnOn()
{
Console.WriteLine("電視機打開了");
}
public void TurnOff()
{
Console.WriteLine("電視機關(guān)閉了");
}
public void SwitchChannel()
{
Console.WriteLine("電視機切換頻道");
}
}
public class Light
{
public void TunrOn()
{
Console.WriteLine("電燈打開了");
}
public void TurnOff()
{
Console.WriteLine("電燈關(guān)閉了");
}
}
Tv類的TurnOn(),TurnOff(),SwitchChannel(),Light類的TurnOn(),TurnOff(),這些方法都會通過執(zhí)行IController的Execute方法被觸發(fā)。把每一種動作抽象成類,并實現(xiàn)IControl接口。
public class LighOn : IControl
{
private Light _light;
public LighOn(Light light)
{
_light = light;
}
public void Execute()
{
_light.TunrOn();
}
}
public class LightOff : IControl
{
private Light _light;
public LightOff(Light light)
{
_light = light;
}
public void Execute()
{
_light.TurnOff();
}
}
public class TvOn : IControl
{
private Tv _tv;
public TvOn(Tv tv)
{
_tv = tv;
}
public void Execute()
{
_tv.TurnOn();
}
}
public class TvOff : IControl
{
private Tv _tv;
public TvOff(Tv tv)
{
_tv = tv;
}
public void Execute()
{
_tv.TurnOff();
}
}
public class TvSwitch : IControl
{
private Tv _tv;
public TvSwitch(Tv tv)
{
_tv = tv;
}
public void Execute()
{
_tv.SwitchChannel();
}
}
現(xiàn)在,電視機和電燈有了,觸發(fā)各種動作的類有了,遙控器該裝下這些指令(提供裝下指令的方法)并提供方法供客戶端調(diào)用。
public class RemoteControl
{
private IList<IControl> onCommands = new List<IControl>();
private IList<IControl> offCommands = new List<IControl>();
private IList<IControl> switchCommands = new List<IControl>();
public void AddOnCommand(IControl control)
{
onCommands.Add(control);
}
public void AddOffCommand(IControl control)
{
offCommands.Add(control);
}
public void AddSwitchCommand(IControl control)
{
switchCommands.Add(control);
}
public void PressOnButton(int number)
{
onCommands[number].Execute();
}
public void PressOffButton(int number)
{
offCommands[number].Execute();
}
public void PressSwitchButton(int number)
{
switchCommands[number].Execute();
}
}
客戶端的執(zhí)行邏輯大致是:把電視機、電燈準(zhǔn)備好,把各種指令準(zhǔn)備好,拿出遙控器把各種指令收納其中,最后調(diào)用遙控器的方法執(zhí)行各種指令。
class Program
{
static void Main(string[] args)
{
//命令的接收方
Light light = new Light();
Tv tv = new Tv();
//各種命令
LighOn turnLightOn = new LighOn(light);
LightOff turnLightOff = new LightOff(light);
TvOn turnTvOn = new TvOn(tv);
TvOff turnTvOff = new TvOff(tv);
TvSwitch switchTv = new TvSwitch(tv);
//RemoteConroller組裝命令
RemoteControl control = new RemoteControl();
control.AddOnCommand(turnLightOn);
control.AddOnCommand(turnTvOn);
control.AddOffCommand(turnLightOff);
control.AddOffCommand(turnTvOff);
control.AddSwitchCommand(switchTv);
control.PressOnButton(0);
Console.ReadKey();
}
}
總結(jié):命令模式的需求源自想通過一個指令(比如這里IControl的Execute方法)來控制多個類的多個方法,包含了幾個要素:
1、命令:讓類的各種方法抽象成類實現(xiàn)一個接口
2、裝配命令:把各種命令放到一個集合中
3、觸發(fā)命令:提供方法調(diào)用命令集合中的某條命令,讓其執(zhí)行指令
相信本文所述對大家C#程序設(shè)計的學(xué)習(xí)有一定的幫助借鑒價值。
相關(guān)文章
總結(jié)C#動態(tài)調(diào)用WCF接口的兩種方法
這篇文章給大家總結(jié)了C#動態(tài)調(diào)用WCF接口的兩種方法,大家可以根據(jù)自己的需求選擇對應(yīng)的方式,下面來一起看看。2016-09-09
winform c#中子窗體關(guān)閉刷新父窗體的實例
下面小編就為大家?guī)硪黄獁inform c#中子窗體關(guān)閉刷新父窗體的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02

