一个比较常见的改进用户体验的方案是用Redo/Undo来取代确认对话框,由于这个功能比较常用,本文简单的给了一个在C#中通过Command模式实现Redo/Undo方案的例子,以供后续查询。
代码
class
Program
{
static void Main( string [] args)
{
var cmds = new CommandManager();
while ( true )
{
var key = Console.ReadKey( true );
if (key.KeyChar >= ' 0 ' && key.KeyChar <= ' 9 ' )
{
cmds.DoNewCommand(key.KeyChar.ToString(), () => Console.WriteLine( " process " + key.KeyChar), () => Console.WriteLine( " redo " + key.KeyChar));
}
else
{
if (key.Modifiers.HasFlag(ConsoleModifiers.Control) && (key.Key == ConsoleKey.Z))
cmds.UnDo();
else if (key.Modifiers.HasFlag(ConsoleModifiers.Control) && (key.Key == ConsoleKey.Y))
cmds.ReDo();
}
}
}
}
class CommandManager
{
#region Command定义
public class Command
{
string name;
Action action;
Action unDoAction;
internal Command( string name, Action action, Action unDoAction)
{
this .name = name;
this .action = action;
this .unDoAction = unDoAction;
}
internal void Do() { action(); }
internal void UnDo() { unDoAction(); }
public override string ToString()
{
return name.ToString();
}
}
#endregion
public Stack < Command > ReDoActionStack { get ; private set ; }
public Stack < Command > UnDoActionStack { get ; private set ; }
public CommandManager()
{
ReDoActionStack = new Stack < Command > ();
UnDoActionStack = new Stack < Command > ();
}
public void DoNewCommand( string name, Action action, Action unDoAction)
{
var cmd = new Command(name, action, unDoAction);
UnDoActionStack.Push(cmd);
ReDoActionStack.Clear();
cmd.Do();
}
public void UnDo()
{
if ( ! CanUnDo)
return ;
var cmd = UnDoActionStack.Pop();
ReDoActionStack.Push(cmd);
cmd.UnDo();
}
public void ReDo()
{
if ( ! CanReDo)
return ;
var cmd = ReDoActionStack.Pop();
UnDoActionStack.Push(cmd);
cmd.Do();
}
public bool CanUnDo { get { return UnDoActionStack.Count != 0 ; } }
public bool CanReDo { get { return ReDoActionStack.Count != 0 ; } }
// public IEnumerable<Command> Actions { get { return ReDoActionStack.Reverse().Concat(UnDoActionStack); } }
}
{
static void Main( string [] args)
{
var cmds = new CommandManager();
while ( true )
{
var key = Console.ReadKey( true );
if (key.KeyChar >= ' 0 ' && key.KeyChar <= ' 9 ' )
{
cmds.DoNewCommand(key.KeyChar.ToString(), () => Console.WriteLine( " process " + key.KeyChar), () => Console.WriteLine( " redo " + key.KeyChar));
}
else
{
if (key.Modifiers.HasFlag(ConsoleModifiers.Control) && (key.Key == ConsoleKey.Z))
cmds.UnDo();
else if (key.Modifiers.HasFlag(ConsoleModifiers.Control) && (key.Key == ConsoleKey.Y))
cmds.ReDo();
}
}
}
}
class CommandManager
{
#region Command定义
public class Command
{
string name;
Action action;
Action unDoAction;
internal Command( string name, Action action, Action unDoAction)
{
this .name = name;
this .action = action;
this .unDoAction = unDoAction;
}
internal void Do() { action(); }
internal void UnDo() { unDoAction(); }
public override string ToString()
{
return name.ToString();
}
}
#endregion
public Stack < Command > ReDoActionStack { get ; private set ; }
public Stack < Command > UnDoActionStack { get ; private set ; }
public CommandManager()
{
ReDoActionStack = new Stack < Command > ();
UnDoActionStack = new Stack < Command > ();
}
public void DoNewCommand( string name, Action action, Action unDoAction)
{
var cmd = new Command(name, action, unDoAction);
UnDoActionStack.Push(cmd);
ReDoActionStack.Clear();
cmd.Do();
}
public void UnDo()
{
if ( ! CanUnDo)
return ;
var cmd = UnDoActionStack.Pop();
ReDoActionStack.Push(cmd);
cmd.UnDo();
}
public void ReDo()
{
if ( ! CanReDo)
return ;
var cmd = ReDoActionStack.Pop();
UnDoActionStack.Push(cmd);
cmd.Do();
}
public bool CanUnDo { get { return UnDoActionStack.Count != 0 ; } }
public bool CanReDo { get { return ReDoActionStack.Count != 0 ; } }
// public IEnumerable<Command> Actions { get { return ReDoActionStack.Reverse().Concat(UnDoActionStack); } }
}
原理很简单,通过Command模式把每一步操作封装成一个可undo的命令(包含do和redo两个操作)。并将每一步操作执行后用栈保存起来,undo的时候就以此将Command依次出栈,并执行undo操作。(从某种意义上来说,redo就是undo操作的undo)
上面的代码已经实现了基本的Undo/Redo功能,但实际使用的时候还是有一些细节需要考虑的:如undo或redo时失败(抛异常)的处理等。由于这些细节方面的处理方式不尽相同,本文只是实现一个基本框架,以备后续使用时参考,并不想把它弄的过于复杂。
这种方式比较简单,几乎每种语言都可以轻易的写出这种方式下的实现。但通过这种Command封装的方式实现的也有一些限制,使用的时候需要注意:
- 每一步操作都需要封装成command命令
- 每一步操作都是可逆的
- 当命令过多的时候需要考虑commandlist的内存占用和命令查询时的性能问题