当前位置: 移动技术网 > IT编程>开发语言>c# > C#命令模式(Command Pattern)实例教程

C#命令模式(Command Pattern)实例教程

2019年07月18日  | 移动技术网IT编程  | 我要评论

本文以实例形式讲述了c#命令模式的实现方法,分享给大家供大家参考。具体实现方法如下:

现假设想让遥控器控制电灯的开关、电视机的开关和切换,该如何做?

所有的开、关、切换都是遥控器发出的指令,把这些指令统一抽象成一个接口。

public interface icontrol
{
    void execute();
}

把电灯、电视机抽象成类。

public class tv
{
    public void turnon()
    {
      console.writeline("电视机打开了");
    }
    public void turnoff()
    {
      console.writeline("电视机关闭了");
    }
    public void switchchannel()
    {
      console.writeline("电视机切换频道");
    }
}
public class light
{
    public void tunron()
    {
      console.writeline("电灯打开了");
    }
    public void turnoff()
    {
      console.writeline("电灯关闭了");
    }
}

tv类的turnon(),turnoff(),switchchannel(),light类的turnon(),turnoff(),这些方法都会通过执行icontroller的execute方法被触发。把每一种动作抽象成类,并实现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();
    }
}

现在,电视机和电灯有了,触发各种动作的类有了,遥控器该装下这些指令(提供装下指令的方法)并提供方法供客户端调用。

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

客户端的执行逻辑大致是:把电视机、电灯准备好,把各种指令准备好,拿出遥控器把各种指令收纳其中,最后调用遥控器的方法执行各种指令。

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

总结命令模式的需求源自想通过一个指令(比如这里icontrol的execute方法)来控制多个类的多个方法,包含了几个要素:

1、命令:让类的各种方法抽象成类实现一个接口
2、装配命令:把各种命令放到一个集合中
3、触发命令:提供方法调用命令集合中的某条命令,让其执行指令

相信本文所述对大家c#程序设计的学习有一定的帮助借鉴价值。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网