CSharp: Command Pattern
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | /// <summary> /// defines Command interface /// Command Patterns命令模式 /// 20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public interface Command { /// <summary> /// /// </summary> void Execute(); /// <summary> /// /// </summary> void Undo(); /// <summary> /// /// </summary> /// <returns></returns> bool isUndo(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | /// <summary> /// Summary description for ColorCommand. /// Command Patterns命令模式 /// 20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class ColorCommand : Command { protected Color color; //line color protected PictureBox pbox; //box to draw in protected ArrayList drawList; //list of lines protected int x, y, dx, dy; //coordinates /// <summary> /// /// </summary> /// <param name="pict"></param> public ColorCommand(PictureBox pict) { pbox = pict; //copy in picture box drawList = new ArrayList(); //create list } /// <summary> /// /// </summary> public void Execute() { //create a new line to draw DrawData dl = new DrawData(x, y, dx, dy); drawList.Add(dl); //add it to the list x = x + dx; //increment the positions y = y + dy; pbox.Refresh(); } /// <summary> /// /// </summary> /// <returns></returns> public bool isUndo() { return false ; } /// <summary> /// /// </summary> public void Undo() { DrawData dl; int index = drawList.Count - 1; if (index >= 0) { dl = (DrawData)drawList[index]; drawList.RemoveAt(index); x = dl.getX(); y = dl.getY(); } pbox.Refresh(); } /// <summary> /// /// </summary> /// <param name="g"></param> public void draw(Graphics g) { Pen rpen = new Pen(color, 1); int h = pbox.Height; int w = pbox.Width; //draw all the lines in the list for ( int i = 0; i < drawList.Count; i++) { DrawData dl = (DrawData)drawList[i]; g.DrawLine(rpen, dl.getX(), dl.getY(), dl.getX() + dx, dl.getDy() + h); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | /// <summary> /// Summary description for DrawData. /// Command Patterns命令模式 /// 20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class DrawData { /// <summary> /// /// </summary> private int x, y, dx, dy; /// <summary> /// /// </summary> /// <param name="x_"></param> /// <param name="y_"></param> /// <param name="dx_"></param> /// <param name="dy_"></param> public DrawData( int x_, int y_, int dx_, int dy_) { x = x_; dx = dx_; y = y_; dy = dy_; } /// <summary> /// /// </summary> /// <returns></returns> public int getX() { return x; } /// <summary> /// /// </summary> /// <returns></returns> public int getY() { return y; } /// <summary> /// /// </summary> /// <returns></returns> public int getDx() { return dx; } /// <summary> /// /// </summary> /// <returns></returns> public int getDy() { return dy; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | /// <summary> /// Summary description for RedCommand. /// Command Patterns命令模式 /// 20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class RedCommand : ColorCommand { /// <summary> /// /// </summary> /// <param name="pict"></param> public RedCommand(PictureBox pict) : base (pict) { color = Color.Red; x = 0; dx = 20; y = 0; dy = 0; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | /// <summary> /// Summary description for UndoCommand. /// Command Patterns命令模式 /// 20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class UndoComd : Command { /// <summary> /// /// </summary> private ArrayList undoList; /// <summary> /// /// </summary> public UndoComd() { undoList = new ArrayList(); } /// <summary> /// /// </summary> /// <param name="comd"></param> public void add(Command comd) { if (!comd.isUndo()) { undoList.Add(comd); } } /// <summary> /// /// </summary> /// <returns></returns> public bool isUndo() { return true ; } /// <summary> /// /// </summary> public void Undo() { } /// <summary> /// /// </summary> public void Execute() { int index = undoList.Count - 1; if (index >= 0) { Command cmd = (Command)undoList[index]; cmd.Undo(); undoList.RemoveAt(index); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | /// <summary> /// Summary description for CommandHolder. /// Command Patterns命令模式 /// 20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public interface CommandHolder { /// <summary> /// /// </summary> /// <returns></returns> Command getCommand(); /// <summary> /// /// </summary> /// <param name="cmd"></param> void setCommand(Command cmd); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | /// <summary> /// draws bluelines and caches list for undo /// Command Patterns命令模式 /// 20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class BlueCommand : ColorCommand { /// <summary> /// /// </summary> /// <param name="pict"></param> public BlueCommand(PictureBox pict) : base (pict) { color = Color.Blue; x = pbox.Width; dx = -20; y = 0; dy = 0; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | /// <summary> /// Command Patterns命令模式 /// 20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public partial class CommandButton : Button, CommandHolder { private Command command; /// <summary> /// /// </summary> /// <param name="comd"></param> public void setCommand(Command comd) { command = comd; } /// <summary> /// /// </summary> /// <returns></returns> public Command getCommand() { return command; } public CommandButton() { InitializeComponent(); } public CommandButton(IContainer container) { container.Add( this ); InitializeComponent(); } } |
调用试试:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | /// <summary> /// Command Patterns /// Command Patterns命令模式 /// 20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public partial class CommandPatternsForm : Form { private BlueCommand blueC; private RedCommand redC; private UndoComd undoC; /// <summary> /// /// </summary> public CommandPatternsForm() { InitializeComponent(); init(); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void CommandPatternsForm_Load( object sender, EventArgs e) { } /// <summary> /// /// </summary> private void init() { btBlue.setCommand(blueC = new BlueCommand(pBox)); btRed.setCommand(redC = new RedCommand(pBox)); btUndo.setCommand(undoC = new UndoComd()); EventHandler evh = new EventHandler(commandClick); btBlue.Click += evh; btRed.Click += evh; btUndo.Click += evh; pBox.Paint += new PaintEventHandler(paintHandler); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void commandClick( object sender, EventArgs e) { //get the command Command comd = ((CommandHolder)sender).getCommand(); undoC.add(comd); //add to undo list comd.Execute(); //and execute it } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void paintHandler( object sender, PaintEventArgs e) { Graphics g = e.Graphics; blueC.draw(g); redC.draw(g); } } |
输出:
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
CSharp code
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2018-09-28 MySQL5.7: Paging using Mysql Stored Proc