请选择 进入手机版 | 继续访问电脑版

马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

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

  [复制链接]
查看4459 | 回复17 | 2019-12-26 09:05:39 | 显示全部楼层 |阅读模式

一、概述
通常来说,“活动哀求者”与“活动实现者”是紧耦合的。但在某些场合,比如要对活动举行“记载、取消/重做、事务”等处置惩罚,这种无法反抗厘革的紧耦合是不符合的。在这些环境下,将“活动哀求者”与“活动实现者”解耦,实现二者之间的松耦合就至关重要。下令模式是管理这类标题的一个比力好的方法。
二、下令模式
下令模式将一个哀求封装为一个对象,从而使你可用差别的哀求对客户举行参数化;对哀求列队或记载哀求日志,以及支持可取消的操作。
下令模式的结构图如下

090540i8e8zwqf7t58k55o.jpg

Command界说了下令的接口
ConcreteCommand实现Command接口,界说了具体的下令
Client用于创建具体的下令并设定吸收者
Invoker要求Command实行相应的哀求
Receiver实行与实行一个哀求,任何一个类都大概作为Receiver。
三、示例
假定要实现一个画图体系,要求支持取消功能,下面就用下令模式来实现这一需求。
起首界说一个抽象的下令接口

  1. public interface IGraphCommand
  2. {
  3. void Draw();
  4. void Undo();
  5. }
复制代码

接着实现具体的画图下令

  1. public class Line : IGraphCommand
  2. {
  3. private Point startPoint;
  4. private Point endPoint;
  5. public Line(Point start, Point end)
  6. {
  7. startPoint = start;
  8. endPoint = end;
  9. }
  10. public void Draw()
  11. {
  12. Console.WriteLine("Draw Line:{0} To {1}", startPoint.ToString(), endPoint.ToString());
  13. }
  14. public void Undo()
  15. {
  16. Console.WriteLine("Erase Line:{0} To {1}", startPoint.ToString(), endPoint.ToString());
  17. }
  18. }
  19. public class Rectangle : IGraphCommand
  20. {
  21. private Point topLeftPoint;
  22. private Point bottomRightPoint;
  23. public Rectangle(Point topLeft, Point bottomRight)
  24. {
  25. topLeftPoint = topLeft;
  26. bottomRightPoint = bottomRight;
  27. }
  28. public void Draw()
  29. {
  30. Console.WriteLine("Draw Rectangle: Top Left Point {0}, Bottom Right Point {1}", topLeftPoint.ToString(), bottomRightPoint.ToString());
  31. }
  32. public void Undo()
  33. {
  34. Console.WriteLine("Erase Rectangle: Top Left Point {0}, Bottom Right Point {1}", topLeftPoint.ToString(), bottomRightPoint.ToString());
  35. }
  36. }
  37. public class Circle : IGraphCommand
  38. {
  39. private Point centerPoint;
  40. private int radius;
  41. public Circle(Point center, int radius)
  42. {
  43. centerPoint = center;
  44. this.radius = radius;
  45. }
  46. public void Draw()
  47. {
  48. Console.WriteLine("Draw Circle: Center Point {0}, Radius {1}", centerPoint.ToString(), radius.ToString());
  49. }
  50. publi cvoid Undo()
  51. {
  52. Console.WriteLine("Erase Circle: Center Point {0}, Radius {1}", centerPoint.ToString(), radius.ToString());
  53. }
  54. }
复制代码

然后再界说画图类作为下令吸收者

  1. public class Graphics
  2. {
  3. Stack<IGraphCommand> commands =new Stack<IGraphCommand>();
  4. public void Draw(IGraphCommand command)
  5. {
  6. command.Draw();
  7. commands.Push(command);
  8. }
  9. public void Undo()
  10. {
  11. IGraphCommand command = commands.Pop();
  12. command.Undo();
  13. }
  14. }
复制代码

末了看一下怎样调用

  1. static void Main(string[] args)
  2. {
  3. Line line =new Line(new Point(10, 10), new Point(100, 10));
  4. Rectangle rectangle =new Rectangle(new Point(20, 20), new Point(50, 30));
  5. Circle circle =new Circle(new Point(500, 500), 200);
  6. Graphics graphics =new Graphics();
  7. graphics.Draw(line);
  8. graphics.Draw(rectangle);
  9. graphics.Undo();
  10. graphics.Draw(circle);
  11. Console.ReadLine();
  12. }
复制代码

C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则