(18).命令模式

news/2025/3/16 22:16:05/文章来源:https://www.cnblogs.com/kitthe/p/18775812

命令模式

命令模式的核心思想是将请求封装为个对象,将其作为命令发起者和接收者的中介,而抽象出来的命令对象又使得能够对一系列请求进行操作,如对请求进行排队,
记录请求日志以及支持可撤销的操作等。
命令模式参与者:
命令的执行者(接收者Receiver):它单纯的只具体实现了功能。(实例中对应的则是将军)
命令的下达者(调用者Invoker):就是起到遥控的作用,首先在类中声明抽象命令类的引用,并通过参数注入等方式进行实例化。然后通过调用命令对象来告诉执行者执行功能。起到一个传声筒的作用。(实例中对应的是太监)
抽象命令类Command:主要作用有两点,其一就是为了规范具体命令类,声明其执行方法。其二就是为了方便调用者Invoker调用,使其不需要知道具体命令类,只需要执行抽象命令类即可。(实例中对应的是圣旨
具体命令类OfficeCommand:继承自抽象类,在类中首先声明了执行者的引用,通过参数注入等方式进行创建执行者对象,将命令类和执行者进行绑发其次具体实现了抽象类中声明的执行方法,调用执行者对象中方法进行执行。(实例中对应的是上任与回京具体的两道圣旨)

◇客户端角色Client:主要是负责将执行者和命令类进行绑定,其次将命令类和调用者进行绑定。

使其调用者可以通过命令类给执行者传递消息进行执行。(实例中对应的是皇帝角色)

 以下是调用关系:

 

(1).命令抽像类:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 命令模式.Base
 8 {
 9    /// <summary>
10    /// 命令抽像类
11    /// </summary>
12     public abstract class CommandBase
13     {
14         public IReceiver Receiver;
15 
16         protected CommandBase(IReceiver receiver)
17         {
18             Receiver = receiver;
19         }
20         public abstract void Execute();
21     }
22 }

(2). 命令接收者  ---将军

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 命令模式.Base
 8 { 
 9     /// <summary>
10   /// 命令接收者  ---将军
11   /// </summary>
12     public interface IReceiver
13     {
14         /// <summary>
15         /// 上任 开始
16         /// </summary>
17         void Start();
18 
19 
20         /// <summary>
21         /// 回京
22         /// </summary>
23         void Return();
24     }
25 }

(3).命令的宣读者  --太监

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using 命令模式.Base;
 7 
 8 namespace 命令模式
 9 {
10     /// <summary>
11     ///命令的宣读者  --太监
12     /// </summary>
13     public class Ivoker
14     {
15         public CommandBase Command;
16 
17 
18         /// <summary>
19         /// 设置,因为命令是皇帝给的
20         /// </summary>
21         /// <param name="command"></param>
22         public void SetCommand(CommandBase command)
23         {
24             Command = command;
25         }
26 
27         /// <summary>
28         /// 宣读圣旨
29         /// </summary>
30         public void Reading()
31         { 
32              Command.Execute();
33         }
34 
35     }
36 }

(4)上任的命令

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using 命令模式.Base;
 7 
 8 namespace 命令模式
 9 {
10     /// <summary>
11     /// 上任的命令
12     /// </summary>
13     public class OfficeCommand : CommandBase
14     {
15         public OfficeCommand(IReceiver receiver) : base(receiver)
16         {
17 
18         }
19 
20         public override void Execute()
21         {
22             Receiver.Start();
23         }
24     }
25 }

(5).回京命令

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using 命令模式.Base;
 7 
 8 namespace 命令模式
 9 {
10     /// <summary>
11     /// 回京命令
12     /// </summary>
13     public class ReturnCommand : CommandBase
14     {
15         public ReturnCommand(IReceiver receiver) : base(receiver)
16         {
17         }
18 
19         public override void Execute()
20         {
21             Receiver.Return();
22         }
23     }
24 }

(6).命令的真实执行者---将军

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using 命令模式.Base;
 7 
 8 namespace 命令模式
 9 {
10     /// <summary>
11     /// 命令的真实执行者---将军
12     /// </summary>
13     public class Receiver : IReceiver
14     {
15         public void Return()
16         {
17             Console.WriteLine("回京。。。"); 
18         }
19 
20         public void Start()
21         {
22             Console.WriteLine("去安徽上任。。。");
23         }
24     }
25 }

(7).调用者

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading;
 6 using System.Threading.Tasks;
 7 using 命令模式.Base;
 8 
 9 namespace 命令模式
10 {
11 
12     /// <summary>
13     ///  皇帝
14     /// </summary>
15     public class Client
16     {
17         public void Start()
18         { 
19             ///一个太监
20             Ivoker ivoker = new Ivoker();
21 
22             ///一个将军
23             IReceiver receiver = new Receiver();
24 
25             ///上任的诏书
26             CommandBase officeCommand = new OfficeCommand(receiver);
27 
28             ///拿到诏书
29             ivoker.SetCommand(officeCommand);
30 
31             ///宣读
32             ivoker.Reading();
33 
34 
35             Console.WriteLine("4 years later...");
36 
37 
38             ///回京的诏书
39             CommandBase returnCommand = new ReturnCommand(receiver);
40 
41             ///拿到诏书
42             ivoker.SetCommand(returnCommand);
43 
44             ///宣读
45             ivoker.Reading();
46 
47 
48         }
49     }
50 }
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 命令模式
 8 {
 9     internal class Program
10     {
11         static void Main(string[] args)
12         {
13             new Client().Start();
14             Console.ReadKey();
15         }
16     }
17 }

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/900016.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

学嵌入式C语言,看这一篇就够了(5)

C语言的运算符 学习编程语言,应该遵循“字-->词-->句-->段--->章”,对于一条有意义的语句而言,是离不开标点符号的运算符指明要进行的运算和操作,操作数是指运算符的操作对象,根据运算符操作数的数目不同,C语言标准把运算符分为三种:单目运算符(一元运算符…

20242313 2024-2025-2 《Python程序设计》实验一报告

20242313 2024-2025-2 《Python程序设计》实验一报告 课程:《Python程序设计》 班级:2423 姓名:曾海鹏 学号:20242313 实验教师:王志强 实验日期:2025年3月16日 必修/选修:公选课 1.实验内容 1.熟悉Python开发环境; 2.练习Python运行、调试技能;(编写书中的程序,并…

nn.Embedding()函数详解

nn.Embedding()函数详解 nn.Embedding()函数:随机初始化词向量,词向量在正态分布N(0,1)中随机取值 输入: torch.nn.Embedding(num_embeddings, embedding_dim, padding_idx=None, max_norm=None, norm_type=2.0, scale_grad_by_freq=False, sparse=False, _weight=None) num…

htb Authority

端口扫描 nmap -sC -sV -p- -Pn -T4 10.10.11.222 Starting Nmap 7.92 ( https://nmap.org ) at 2024-10-04 19:42 CST Nmap scan report for 10.10.11.222 (10.10.11.222) Host is up (0.40s latency). Not shown: 65506 closed tcp ports (reset) PORT STATE SERVICE …

蓝桥杯14届省B

蓝桥杯14届省赛B组A:int a[105]; int day[]={0,31,28,31,30,31,30,31,31,30,31,30,31};//记录每个月有多少天 set<int> st;//记录不重复的日期void check(int mm,int dd){if (mm>12||mm<1||dd<1||dd>day[mm]) return;else st.insert(mm*100+dd);//st存日期 …

docker 安装 oracle database 问题记录

pre本地docker (WSL)安装运行 Oracle1. 镜像处理参考链接:https://www.cnblogs.com/wuchangsoft/p/18344847 oracle 镜像获取:https://container-registry.oracle.com/ords/f?p=113:10:::::: (Oracle官网,由于部分问题导致直接pull无法拉取) 阿里云,参考链接里有个个人19…

20242103 实验一《Python程序设计》实验报告

20242103 《Python程序设计》实验1报告 课程:《Python程序设计》 班级: 2421 姓名: 李雨虓 学号:20242103 实验教师:王志强 实验日期:2025年3月12日 必修/选修: 公选课 1.实验内容: 1.熟悉Python开发环境; 2.练习Python运行、调试技能;(编写书中的程序,并进行调试…

20241313 2024-2025-2 《Python程序设计》实验一报告

20241313 2024-2025-2 《Python程序设计》实验一报告 课程:《Python程序设计》 班级: 2413 姓名: 刘鸣宇 学号:20241313 实验教师:王志强 实验日期:2025年3月12日 必修/选修: 公选课 1.实验内容 1.熟悉Python开发环境; 2.练习Python运行、调试技能;(编写书中的程序…

mutatingwebhook的简单实例

一. k8s集群准备 这里不再赘述k8s集群搭建。主要注意参数:kubectl get po kube-apiserver-server -n kube-system -o yaml | grep plugin 预期结果为:- --enable-admission-plugins=NodeRestriction,MutatingAdmissionWebhook,ValidatingAdmissionWebhook 至少要拥有两个参数…

Tauri新手向 - 基于LSB隐写的shellcode加载器

此篇是记录自己初次学习tauri开发工具,包含遇到的一些问题以及基本的知识,也给想上手rust tauri的师傅们一些小小的参考。此项目为保持免杀性暂不开源,希望各位师傅多多支持,反响可以的话后续会放出代码大家一起交流学习。ShadowMeld - 基于图像隐写技术的载荷生成框架 通过…