欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#命令模式用法实例

程序员文章站 2023-11-17 13:58:28
本文实例讲述了c#命令模式。分享给大家供大家参考。具体实现方法如下: using system; using system.collections.generi...

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

using system;
using system.collections.generic;
using system.linq;
using system.text;
namespace 命令模式
{
  class program
  {
    static void main(string[] args)
    {
      receiver r = new receiver();
      command c = new concretecommand(r);
      invoker i = new invoker();
      i.setcommand(c);
      i.exectuecommand();
    }
    public abstract class command
    {
      private receiver receiver;
      internal receiver receiver
      {
        get { return receiver; }
        set { receiver = value; }
      }
      public command(receiver receiver)
      {
        this.receiver = receiver;
      }
      public abstract void execute();
    }
    public class receiver
    {
      public void action()
      {
        console.writeline("取得receiver的action方法!");
      }
    }
    public class concretecommand : command
    {
      public concretecommand(receiver receiver) : base(receiver) { }
      public override void execute()
      {
        receiver.action();
      }
    }
    public class invoker
    {
      private command command;
 
      internal command command
      {
        get { return command; }
        set { command = value; }
      }
      public void setcommand(command command)
      {
        this.command = command;
      }
      public void exectuecommand()
      {
        command.execute();
      }
    }
  }
}

希望本文所述对大家的c#程序设计有所帮助。