设计模式之命令模式(Command Pattern)
源代码下载
1.概念
将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可取消的操作。
2.类图
3.调用代码
Use objUse = new Use();
this.listBox1.Items.Add(objUse.Do(new Model() { ID = "1", Name = "操作1" }, Action.Add));
this.listBox1.Items.Add(objUse.Do(new Model() { ID = "2", Name = "操作2" }, Action.Add));
this.listBox1.Items.Add(objUse.Do(new Model() { ID = "3", Name = "操作3" }, Action.Add));
this.listBox1.Items.Add(objUse.Do(new Model() { ID = "4", Name = "操作4" }, Action.Add));
this.listBox1.Items.Add(objUse.Do(new Model() { ID = "5", Name = "操作5" }, Action.Add));
this.listBox1.Items.Add(objUse.Do(new Model() { ID = "6", Name = "操作6" }, Action.Add));
this.listBox1.Items.Add(objUse.Do(new Model() { ID = "7", Name = "操作7" }, Action.Add));
this.listBox1.Items.Add(objUse.Do(new Model() { ID = "8", Name = "操作8" }, Action.Add));
this.listBox1.Items.Add(objUse.Do(new Model() { ID = "9", Name = "操作9" }, Action.Add));
this.listBox1.Items.Add(objUse.Do(new Model() { ID = "10", Name = "操作10" }, Action.Add)); this.listBox1.Items.Add(objUse.Undo(4));//撤销4次
this.listBox1.Items.Add(objUse.Undo(2));//撤销2次
this.listBox1.Items.Add(objUse.Undo(1));//撤销1次
4.输出结果
源代码下载