关于C# 设计模式 Command 命令模式

关于C# 设计模式 Command 命令模式

using System;
using System.Collections.Generic;
using System.Text;

///Command(命令)模式可用于将一个请求封装为一个对象,以便使用不同的请求进行参数化。
///add by jzh 2007-04-15
namespace DesignPattern
{
    abstract class Command
    {
        abstract public void Execute();
        protected Receiver r;
        public Receiver R
        {
            set
            {
                r = value;
            }
        }
    }

    class ConcreteCommand : Command
    {
        override public void Execute()
        {
            Console.WriteLine("命令执行");
            r.InformAboutCommand();
        }
    }

    class Receiver
    {
        public void InformAboutCommand()
        {
            Console.WriteLine("命令接收");
        }

    }

    class Invoker
    {
        private Command command;
        public void StoreCommand(Command c)
        {
            command = c;
        }
        public void ExecuteCommand()
        {
            command.Execute();
        }
    }

    public class Client
    {
        public static void Main(string[] args)
        {
            Command c = new ConcreteCommand();
            Receiver r = new Receiver();
            c.R = r;

            Invoker i = new Invoker();
            i.StoreCommand(c);

            i.ExecuteCommand();

            Console.ReadLine();
        }
    }
}

 

发表回复

您的电子邮箱地址不会被公开。