初涉c#设计模式-Command Pattern

命令模式:

    命令模式又称为行动(Action)模式或交易(Transaction)模式。命令模式把一个请求或者操作封装到一个对象中。命令模式允许系统使用不同的请求把客户端参数化,对请求排队或者记录请求日志,可以提供命令的撤销和恢复功能。

    命令模式是对命令的封装。命令模式把发出命令的责任和执行命令的责任分割开,委派给不同的对象。

    每一个命令都是一个操作:请求的一方发出请求要求执行一个操作;接收的一方收到请求,并执行操作。命令模式允许请求的一方和接收的一方独立开来,使得请求的一方不必知道接收请求的一方的接口,更不必知道请求是怎么被接收,以及操作是否被执行、何时被执行,以及是怎么被执行的。

UML:

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CommandPattern
{
    class Message
    {
        private int id;
        private string name;
        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        public string Name
        {
            set { name = value; }
            get { return name; }
        }
        public Message(int id,string name)
        {
            this.id = id;
            this.name = name;
        }
    }
    public enum Action
    {
        Delete,
        Insert
    }
    public interface ICommand
    {
        void Excute();
        void UnExcute();
    }
    class ConcreateCommand:ICommand
    {
        Reciever iReciever;
        Message iMessage;
        Action iAction;
        public ConcreateCommand(Action aAction,Message aMesage)
        {
            iReciever = new Reciever();
            iAction = aAction;
            iMessage = aMesage;
        }
        public void Excute()
        {
            iReciever.Operation(iAction,iMessage);
        }
        public void UnExcute()
        {
            iAction = GetUnDo();
            iReciever.Operation(iAction, iMessage);
        }
        Action GetUnDo()
        {
            switch (iAction)
            {
                case Action.Delete:
                    return Action.Insert;
                case Action.Insert:
                    return Action.Delete;
                default:
                    return 0;
            }
        }
    }
    class Invoker
    {
        int current;
        List<ICommand> commandList = new List<ICommand>();
        public void Do(ICommand aCommand)
        {
            aCommand.Excute();
            commandList.Add(aCommand);
            current++;
        }
        public void UnDo(int index)
        {
            for (int i = 0; i < index; i++)
            {
                commandList[--current].UnExcute();
                commandList.RemoveAt(current);
            }
        }
    }
    class Reciever
    {
        public void Operation(Action aAction,Message aMessage)
        {
            switch (aAction)
            {
                case Action.Delete:
                    Delete(aAction, aMessage);
                    break;
                case Action.Insert:
                    Insert(aAction, aMessage);
                    break;
                default:
                    break;
            }
        }
        void Delete(Action aAction,Message aMessage)
        {
            Console.WriteLine("删除这些东西"+aAction.ToString()+","+aMessage.Id+","+aMessage.Name);
        }
        void Insert(Action aAction, Message aMessage)
        {
            Console.WriteLine("增加这些东西" + aAction.ToString() + "," + aMessage.Id + "," + aMessage.Name);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Message aMessage = new Message(1, "wang");
            Message bMessage = new Message(2, "shuai");
            Message cMessage = new Message(3, "ni");
            Message dMessage = new Message(4, "hao");
            ICommand aCommand = new ConcreateCommand(Action.Delete, aMessage);
            ICommand bCommand = new ConcreateCommand(Action.Insert, bMessage);
            ICommand cCommand = new ConcreateCommand(Action.Delete, cMessage);
            ICommand dCommand = new ConcreateCommand(Action.Insert, dMessage);
            Invoker aInvoker = new Invoker();
            aInvoker.Do(aCommand);
            aInvoker.Do(bCommand);
            aInvoker.Do(cCommand);
            aInvoker.Do(dCommand);
            aInvoker.UnDo(1);
            aInvoker.UnDo(1);
        }
    }
}

 

使用命令模式的优点和缺点:

命令允许请求的一方和接收请求的一方能够独立演化,从而且有以下的优点:

  • 命令模式使新的命令很容易地被加入到系统里。
  • 允许接收请求的一方决定是否要否决(Veto)请求。
  • 能较容易地设计-个命令队列。
  • 可以容易地实现对请求的Undo和Redo。
  • 在需要的情况下,可以较容易地将命令记入日志。
  • 命令模式把请求一个操作的对象与知道怎么执行一个操作的对象分割开。
  • 命令类与其他任何别的类一样,可以修改和推广。
  • 你可以把命令对象聚合在一起,合成为合成命令。比如宏命令便是合成命令的例子。合成命令是合成模式的应用。
  • 由于加进新的具体命令类不影响其他的类,因此增加新的具体命令类很容易。

命令模式的缺点如下:

  • 使用命令模式会导致某些系统有过多的具体命令类。某些系统可能需要几十个,几百个甚至几千个具体命令类,这会使命令模式在这样的系统里变得不实际。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值