无操作次数限制的 Undo/Redo 实现方案
2007-02-13 23:02
405 查看
一般的文档软件,图形设计工具,都会有Redo/Undo (即重做/撤消)功能,可是如何也在自己的应用程序当中实现这样的功能,而且是没有操作次数地Redo/Undo?
此时“软件设计模式”就显得很重要。
这里实现的Redo/Undo主要应用的“命令模式”与“备忘录模式”。
《C#设计模式》一书里讲到只用备忘录模式来实现,我看了之后觉得那种方法太烦琐了,我们可以利用
C#2.0的新特性来使之简化。
首先,如果要让我们的每一步操作都是可撤消、重做的,那么我们必须要将这些操作涉及的代码“封装
”到一个方法里去,以供以后调用。而且有撤消、重做,那么必须有两部分代码:
1.正向操作的代码 DoOperation
2.反向(撤消)的代码 UndoOperation.
然后,要实现无次数限制的撤消、重做,我们应该要用两个栈(Stack)来保存操作。
下面的UML 类图:
public interface ICommand
public delegate void ActionHandler();
3. Command.cs
public class Command:ICommand
public static class CommandManager
public partial class Form1 : Form
{
private int x=10;
private int y=10;
public Form1()
{
InitializeComponent();
}
private void btnNew_Click(object sender, EventArgs e)
{
btnRedo.Enabled = false;
Command command = new Command();
Label label = new Label();
Random r = new Random();
label.Text = ((char)r.Next(65, 99)).ToString();
label.Font = new Font("Arial", 16,FontStyle.Bold);
label.AutoSize = true;
label.Location = new Point(x, y);
/**//*
* 使用匿名委托,更加简单,而且匿名委托方法里还可以使用外部变量。
*/
command.DoOperation = delegate()
{
//Do或者Redo操作会执行到的代码
x += label.Width + 10;
y += label.Height + 10;
this.Controls.Add(label);
};
command.UndoOperation = delegate()
{
//Undo操作会执行到的代码
x -= label.Width + 10;
y -= label.Height + 10;
this.Controls.Remove(label);
};
command.Do();//执行DoOperation相应代码
CommandManager.AddNewCommand(command);
}
private void btnRedo_Click(object sender, EventArgs e)
{
btnUndo.Enabled = true;
CommandManager.Redo();
if (CommandManager.RedoStepsCount == 0)
{
btnRedo.Enabled = false;
}
}
private void btnUndo_Click(object sender, EventArgs e)
{
btnRedo.Enabled = true;
CommandManager.Undo();
if (CommandManager.UndoStepsCount == 0)
{
btnUndo.Enabled = false;
}
}
}
这是我第一次在Blog上写关于设计模式的心得体会,难免有写得不对之处,希望与大家多交流,多指正不当之处。。。。
此时“软件设计模式”就显得很重要。
这里实现的Redo/Undo主要应用的“命令模式”与“备忘录模式”。
《C#设计模式》一书里讲到只用备忘录模式来实现,我看了之后觉得那种方法太烦琐了,我们可以利用
C#2.0的新特性来使之简化。
首先,如果要让我们的每一步操作都是可撤消、重做的,那么我们必须要将这些操作涉及的代码“封装
”到一个方法里去,以供以后调用。而且有撤消、重做,那么必须有两部分代码:
1.正向操作的代码 DoOperation
2.反向(撤消)的代码 UndoOperation.
然后,要实现无次数限制的撤消、重做,我们应该要用两个栈(Stack)来保存操作。
下面的UML 类图:
public interface ICommand
public delegate void ActionHandler();
3. Command.cs
public class Command:ICommand
public static class CommandManager
public partial class Form1 : Form
{
private int x=10;
private int y=10;
public Form1()
{
InitializeComponent();
}
private void btnNew_Click(object sender, EventArgs e)
{
btnRedo.Enabled = false;
Command command = new Command();
Label label = new Label();
Random r = new Random();
label.Text = ((char)r.Next(65, 99)).ToString();
label.Font = new Font("Arial", 16,FontStyle.Bold);
label.AutoSize = true;
label.Location = new Point(x, y);
/**//*
* 使用匿名委托,更加简单,而且匿名委托方法里还可以使用外部变量。
*/
command.DoOperation = delegate()
{
//Do或者Redo操作会执行到的代码
x += label.Width + 10;
y += label.Height + 10;
this.Controls.Add(label);
};
command.UndoOperation = delegate()
{
//Undo操作会执行到的代码
x -= label.Width + 10;
y -= label.Height + 10;
this.Controls.Remove(label);
};
command.Do();//执行DoOperation相应代码
CommandManager.AddNewCommand(command);
}
private void btnRedo_Click(object sender, EventArgs e)
{
btnUndo.Enabled = true;
CommandManager.Redo();
if (CommandManager.RedoStepsCount == 0)
{
btnRedo.Enabled = false;
}
}
private void btnUndo_Click(object sender, EventArgs e)
{
btnRedo.Enabled = true;
CommandManager.Undo();
if (CommandManager.UndoStepsCount == 0)
{
btnUndo.Enabled = false;
}
}
}
这是我第一次在Blog上写关于设计模式的心得体会,难免有写得不对之处,希望与大家多交流,多指正不当之处。。。。
相关文章推荐
- 限制程序中某类操作的执行次数的算法设计及C代码实现
- 纯javascript对撤销和重写(undo、redo)的完美实现,适用于任何页面元素操作
- winform程序如何通过操作注册表来实现限制使用次数
- Undo/Redo几种实现方式介绍(三)——保存操作方式
- [原创]文本编辑中利用栈来实现undo和redo的无限次操作
- WinForm通过操作注册表实现限制软件使用次数的方法
- 利用Command模式实现无限次数的Undo/Redo功能
- 有限次数的Undo&Redo的C#实现
- 设计模式:利用Command模式实现无限次数的Undo/Redo功能
- Unity3d实现回滚操作(Undo和Redo)
- c#操作注册表实现对软件试用次数的限制
- Ajax+jQuery+bootstrap+Java实现异步点赞功能,并限制点击次数
- 用Command模式简单的实现Undo&Redo功能
- 表格批量编辑与Undo/Redo功能实现
- C++下利用Command设计模式实现undo和redo
- 实现一个函数,对一个正整数n,算得到1需要的最少操作次数?
- 基于操作+角色的授权方案(具体实现篇)
- 实现一个函数,对一个正整数n,算得到1需要的最少操作次数:
- iOS: 为画板App增加 Undo/Redo(撤销/重做)操作
- springboot整合shiro登录失败次数限制功能的实现代码