C# 设计模式之命令模式

2021-03-05 10:27

阅读:762

标签:客户   pat   open   work   ace   write   space   code   logs   

模式设计的对象:

1、客户角色:发出一个具体的命令并确定其接受者。

2、命令角色:声明了一个给所有具体命令类实现的抽象接口

3、具体命令角色:定义了一个接受者和行为的弱耦合,负责调用接受者的相应方法。

4、请求者角色:负责调用命令对象执行命令。

5、接受者角色:负责具体行为的执行。

 

技术图片技术图片
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 
  7 namespace FactoryMode
  8 {
  9     /// 
 10     /// 命令调用者
 11     /// 
 12     class ComandInvoke
 13     {
 14         CommandMode commandModeRecall;
 15         public ComandInvoke(CommandMode obj)
 16         {
 17             commandModeRecall = obj;
 18         }
 19 
 20         public CommandMode ActionComand
 21         {
 22             get
 23             {
 24                 return commandModeRecall;
 25             }
 26             set
 27             {
 28                 commandModeRecall = value;
 29             }
 30         }
 31         public void Open()
 32         {
 33             commandModeRecall.Openlaser();
 34         }
 35         public void Close()
 36         {
 37             commandModeRecall.CloseLaser();
 38         }
 39         public void Accelerate()
 40         {
 41             commandModeRecall.Accelerate();
 42         }
 43         public void Stop()
 44         {
 45             commandModeRecall.StopLaser();
 46         }
 47     }
 48 
 49 
 50     /// 
 51     /// 抽象命令
 52     /// 
 53     abstract class CommandMode
 54     {
 55         public abstract void Openlaser();
 56         public virtual void Accelerate() { }
 57         public abstract void StopLaser();
 58         public abstract void CloseLaser(); 
 59     }
 60 
 61     /// 
 62     /// 具体命令1
 63     /// 
 64     class FactroyCommand:CommandMode
 65     {
 66         ComandAction _comandInvoke;//具体实施任务的对象
 67         public FactroyCommand(ComandAction comandInvoke)
 68         {
 69             _comandInvoke = comandInvoke;
 70         }
 71         public override void Openlaser() { _comandInvoke.Open(); }
 72         public override void Accelerate() { }
 73         public override void StopLaser() { }
 74         public override void CloseLaser() { }
 75     }
 76 
 77     /// 
 78     /// 具体命令1
 79     /// 
 80     class ClientCommand : CommandMode
 81     {
 82         ComandAction _comandInvoke;
 83         public ClientCommand(ComandAction comandInvoke)
 84         {
 85             _comandInvoke = comandInvoke;
 86         }
 87         public override void Openlaser() { _comandInvoke.Open(); }
 88 
 89         public override void StopLaser() { }
 90         public override void CloseLaser() { }
 91     }
 92 
 93 
 94 
 95     /// 
 96     /// 命令对象抽象类
 97     /// 
 98     abstract class ComandAction
 99     {
100         public virtual void Open() { }
101         public virtual void Close() { }
102         public virtual void Stop() { }
103         public virtual void TestP() { }
104         public virtual void SetPara(int iIndex) { }
105         public virtual void GetPara(int iIndex) { }
106         public virtual void SavePara() { }
107 
108     }
109 
110     class Engineer:ComandAction
111     {
112         string strCurName = "";
113         public Engineer(string strname)
114         {
115             strCurName = strname;
116         }
117         public override void Open() { Console.WriteLine(typeof(Engineer)+":"+ strCurName + ":Open"); }
118         public override void Close() { }
119         public override void Stop() { }
120         public override void TestP() { }
121         public override void SetPara(int iIndex) { }
122         public override void GetPara(int iIndex) { }
123         public override void SavePara() { }
124     }
125 
126     class Worker : ComandAction
127     {
128         string strCurName = "";
129         public Worker(string strname)
130         {
131             strCurName = strname;
132         }
133         public override void Open() { Console.WriteLine(typeof(Worker) + ":" + strCurName + ":Open"); }
134         public override void Close() { }
135         public override void Stop() { }
136 
137     }
138 }
139 
140 使用参考:
141             //=== 命令模式
142             ComandInvoke comandInvoke;//命令使用者
143             CommandMode commandMode;// 命令
144             ComandAction comandAction;//命令实现对象
145             Engineer engineer = new Engineer("Jbt");
146             Worker worker = new Worker("N124");
147 
148 
149             FactroyCommand factroyCommand = new FactroyCommand(engineer);
150             ClientCommand clientCommand = new ClientCommand(worker);
151 
152             comandInvoke = new ComandInvoke(factroyCommand);
153             comandInvoke.Open();
154             comandInvoke.ActionComand = clientCommand;
155             comandInvoke.Open();
View Code

 

引用参考博客:https://www.cnblogs.com/zhili/p/CommandPattern.html

C# 设计模式之命令模式

标签:客户   pat   open   work   ace   write   space   code   logs   

原文地址:https://www.cnblogs.com/SoftZoro20181229/p/12899605.html


评论


亲,登录后才可以留言!