/// <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(); }
/// <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); } } }
/// <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; } }
/// <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; } }
/// <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); } } }
/// <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); }
/// <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; } }
/// <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(); } }
调用试试:
/// <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); } }
输出:
标签:dl,int,Pattern,void,Command,dx,CSharp,public From: https://www.cnblogs.com/geovindu/p/16740015.html