美文网首页
{C#}设计模式辨析.命令

{C#}设计模式辨析.命令

作者: 码农猫爸 | 来源:发表于2021-08-09 07:51 被阅读0次

背景

  • 通过命令解耦接收者和调用者。
  • 接收者,处理具体请求。
  • 命令,绑定接收者和请求。
  • 调用者=命令容器,可执行和撤销命令。

示例

using System.Collections.Generic;
using static System.Console;

namespace DesignPattern_Command
{
    // 接收者类,处理具体请求
    public class Calculator
    {
        private int result = 0;

        public Calculator() { }

        // @允许使用系统保留字
        // 请求= @operator & operand
        public void Evaluate(char @operator, int operand)
        {
            var fx = $"{result} {@operator} {operand} ";

            switch (@operator)
            {
                case '+':
                    result += operand;
                    break;

                case '-':
                    result -= operand;
                    break;

                case '*':
                    result *= operand;
                    break;

                case '/':
                    result /= operand;
                    break;
            }

            WriteLine($"{result}= {fx}");
        }
    }

    // 命令接口
    public interface ICommand
    {
        void Do();
        void Undo();
    }

    // 具体命令类,绑定接收者和请求
    public class Command : ICommand
    {
        private readonly Calculator calculator;
        private readonly char @operator;
        private readonly int operand;

        private readonly Dictionary<char, char> opposites = new Dictionary<char, char>
        {
            ['+'] = '-',
            ['-'] = '+',
            ['*'] = '/',
            ['/'] = '*',
        };

        public Command(Calculator c, char @operator, int operand)
        {
            calculator = c;
            this.@operator = @operator;
            this.operand = operand;
        }

        public void Do()
            => calculator.Evaluate(@operator, operand);

        public void Undo()
            => calculator.Evaluate(opposites[@operator], operand);
    }

    // 调用者=命令容器,执行或撤销命令
    public class User
    {
        private int current = -1;
        private List<ICommand> commands = new List<ICommand>();
        private readonly Calculator calculator;

        public User(Calculator c)
        {
            calculator = c;
        }

        public void Redo(int steps)
        {
            current++; // 从已实施的下一步开始

            for (int step = 1; step <= steps; step++)
            {
                if (current == commands.Count) break;

                commands[current++].Do();
            }
        }

        public void Undo(int steps)
        {
            // 从已实施开始
            for (int step = 1; step <= steps; step++)
            {
                if (current == -1) break;

                commands[current--].Undo();
            }
        }

        public void Compute(char @operator, int operand)
        {
            var command = new Command(calculator, @operator, operand);
            commands.Add(command);

            command.Do();
            current++;
        }
    }

    public class Client
    {
        public Client() { }

        public void Execute()
        {
            var calculator = new Calculator();
            var user = new User(calculator);
            user.Compute('+', 100);
            user.Compute('-', 50);
            user.Compute('*', 2);
            user.Compute('/', 100);

            WriteLine();
            user.Undo(2);

            WriteLine();
            user.Redo(4);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var client = new Client();
            client.Execute();

            ReadKey();
        }
    }
}

相关文章

  • {C#}设计模式辨析.命令

    背景 通过命令解耦接收者和调用者。 接收者,处理具体请求。 命令,绑定接收者和请求。 调用者=命令容器,可执行和撤...

  • 设计模式

    《C#设计模式》 《C#设计模式》-设计模式概述 《C#设计模式》-面向对象设计原则 《C#设计模式》-单例模式 ...

  • {C#}设计模式辨析.状态

    背景 前序状态执行后,切换到后续状态 与责任链异同责任链:遍历并下放到具体责任者处理要求状态:在不同状态中轮转处理...

  • {C#}设计模式辨析.策略

    背景 策略作为算法,可自由替换 示例

  • {C#}设计模式辨析.门面

    背景 复杂系统,可切割成多个相互独立的子系统并由子系统组合而成 与桥接的异同桥接的是维度,每个维度可由子类替换门面...

  • {C#}设计模式辨析.组合

    背景 处理枝(叶的集合)和叶结构 示例

  • {C#}设计模式辨析.代理

    背景 在原程序上加壳,ex. 壳=虚拟/保护/远程/日志记录 等 示例

  • {C#}设计模式辨析.原型

    背景 绕过构造器,快速创建已有原型的复制品。 复制品可与原型脱勾,独立修改属性或字段。 示例

  • {C#}设计模式辨析.抽象工厂

    背景 工厂模式也可应用到多类产品,单类产品请参考https://www.jianshu.com/p/09992d0...

  • {C#}设计模式辨析.桥接

    背景 桥接多个维度时 实现@抽象类:子类继承产生基础维度构造器传参其它维度(1-n)方法提炼或交互多个维度 示例

网友评论

      本文标题:{C#}设计模式辨析.命令

      本文链接:https://www.haomeiwen.com/subject/khyzvltx.html