首页 > 编程语言 >C#设计模式——命令模式(Command Pattern)

C#设计模式——命令模式(Command Pattern)

时间:2024-03-24 13:22:37浏览次数:27  
标签:Draw Point C# Pattern void ToString new 设计模式 public

命令模式
命令模式将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤销的操作。

示例
假定要实现一个绘图系统,要求支持撤销功能,下面就用命令模式来实现这一需求。
首先定义一个抽象的命令接口

 public interface IGraphCommand
 {
     void Draw();
     void Undo();
 }
public class Line : IGraphCommand
    {
        private Point startPoint;
        private Point endPoint;
        public Line(Point start, Point end)
        {
            startPoint = start;
            endPoint = end;
        }

        public void Draw()
        {
            Console.WriteLine("Draw Line:{0} To {1}", startPoint.ToString(), endPoint.ToString());
        }

        public void Undo()
        {
            Console.WriteLine("Erase Line:{0} To {1}", startPoint.ToString(), endPoint.ToString());
        }
    }
 public class Rectangle : IGraphCommand
    {
        private Point topLeftPoint;
        private Point bottomRightPoint;
        public Rectangle(Point topLeft, Point bottomRight)
        {
            topLeftPoint = topLeft;
            bottomRightPoint = bottomRight;
        }

        public void Draw()
        {
            Console.WriteLine("Draw Rectangle: Top Left Point {0},  Bottom Right Point {1}", topLeftPoint.ToString(), bottomRightPoint.ToString());
        }

        public void Undo()
        {
            Console.WriteLine("Erase Rectangle: Top Left Point {0},  Bottom Right Point {1}", topLeftPoint.ToString(), bottomRightPoint.ToString());
        }
    }
 public class Circle : IGraphCommand
    {
        private Point centerPoint;
        private int radius;
        public Circle(Point center, int radius)
        {
            centerPoint = center;
            this.radius = radius;
        }

        public void Draw()
        {
            Console.WriteLine("Draw Circle: Center Point {0},  Radius {1}", centerPoint.ToString(), radius.ToString());
        }

        publi cvoid Undo()
        {
            Console.WriteLine("Erase Circle: Center Point {0},  Radius {1}", centerPoint.ToString(), radius.ToString());
        }
    }

然后再定义绘图类作为命令接收者

public class Graphics
    {
        Stack<IGraphCommand> commands =new Stack<IGraphCommand>();

public void Draw(IGraphCommand command)
        {
            command.Draw();
            commands.Push(command);
        }

public void Undo()
        {
            IGraphCommand command = commands.Pop();
            command.Undo();
        }
    }

最后看一下如何调用

static void Main(string[] args)
    {
        Line line =new Line(new Point(10, 10), new Point(100, 10));
        Rectangle rectangle =new Rectangle(new Point(20, 20), new Point(50, 30));
        Circle circle =new Circle(new Point(500, 500), 200);

        Graphics graphics =new Graphics();
        graphics.Draw(line);
        graphics.Draw(rectangle);
        graphics.Undo();
        graphics.Draw(circle);

        Console.ReadLine();
    }

 

标签:Draw,Point,C#,Pattern,void,ToString,new,设计模式,public
From: https://www.cnblogs.com/itsone/p/18092310

相关文章

  • P10234 [yLCPC2024] B. 找机厅 题解
    题目简述给定一个$n$行$m$列的$01$矩阵,每次可以花费$1$的时间移动到邻近的上下左右的四个格子,求从$(1,1)$点到$(n,m)$的最少时间,并给出具体路径。题目分析第一问易发现是BFS模板题,在这里不多说。第二问我们首先考虑正着记录,即记录每一个点转移到了哪一个点,但......
  • raft算法和etcd代码解析-3.网络分区问题及其它
    网络分区问题网络分区导致选举永远无法达成共识,选举不断超时,任期号将不断增加为避免这个问题,candidate会探测网络环境以免发起无意义的竞选集群变更leader收到配置变更要求,会广播配置变更日志,日志包括新结点和老节点,在收到老节点的多数派认可后,leader后提交该请求在处理配置......
  • # c语言程序设计——实验报告一
    c语言程序设计——实验报告一实验项目名称:实验一熟悉C语言运行环境实验项目类型:验证性实验日期:2023年3月14日一、实验目的下载安装Devc6.0程序。了解在该系统上如何进行编辑、编译、连接和运行一个C程序。通过运行简单的C程序了解C程序的特点二、实验硬、软件环境Win......
  • # c语言程序设计——实验报告二
    c语言程序设计——实验报告二实验项目名称:实验报告2数据描述实验项目类型:验证性实验日期:2024年3月21日一、实验目的1、掌握C语言数据类型,熟悉如何定义一个整型、字符型和实型的变量,以及对它们赋值的方法。2、掌握不同数据类型之间赋值的规律。3、学会使用C的有关算......
  • 【WPF应用10】基本控件-StackPanel:布局原理与实际应用
    在WindowsPresentationFoundation(WPF)中,布局是用户界面设计的核心部分,它决定了控件如何排列和空间如何分配。WPF提供了一系列布局面板(Panel),以便开发者可以根据需要灵活地组织控件。在这些面板中,StackPanel是一个常用的布局控件,它按照子元素的顺序将它们堆叠起来。本文将深......
  • 利用GRACE球谐数据计算地表位移的基本原理与实现
    1.基本理论由于地表质量发生变化导致地表的负荷变形,利用GRACE数据计算地表的垂直(2)、水平东向(3)和水平北向位移(4)方法如下:我们之前已经利用冯伟老师提供的MATLAB工具包计算了EWH,下面我们具体看看两类计算公式的异同点:我们可以发现,有一些部分是一致的,即唯一不同的就是前面乘......
  • Docker的安装
    1.安装gcc以及所需要的软件包#确定你是CentOS7及以上版本cat/etc/redhat-releaseyum-yinstallgccyum-yinstallgcc-c++#安装需要的软件包yuminstall-yyum-utilsdevice-mapper-persistent-datalvm22.设置镜像仓库下载其余资源#使用阿里云(以下为一......
  • 一触即发,全栈联动:使用Docker Compose部署Spring Boot应用+MySQL+Redis实战指南
    在云原生时代的快车道上,DockerCompose无疑是那辆助您疾驰的豪华跑车,它凭借其简洁高效的YAML配置文件,让您能够轻松部署和管理包含SpringBoot应用、MySQL数据库以及Redis缓存服务在内的完整堆栈。本文将深入浅出地引导您通过一个docker-compose.yml文件来定义和配置这些服务,实......
  • Centos7 安装MySQL8后 加装 MySQL基准测试套件 (MySQL Benchmark Suite)
    CentOS7中安装MySQL8.3.0没有/usr/share/mysql/sql-bench下的BenchmarkSuite工具1.yum安装epel源yum-yinstallepel-release2.安装perl平台yum-yinstallperl*3.获得/usr/share/mysql/sql-benchwgethttps://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.......
  • C# wpf 实现自定义撤销重做功能
    新建wpf项目,新建Undoable.cs(操作记录),main.xaml(页面)usingSystem;usingSystem.Collections.Generic;namespaceWpfApp1{///<summary>///撤销重做对象///ceatebyxin2022.6.26///2023.9.13去除Step的定义替换为KeyValuePair简化实现///</summ......