Message
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Command
{
/**//// <summary>
/// 請求者(Invoker)角色
/// </summary>
public class Message
{
/**//// <summary>
/// 命令集合(保存每次操作)
/// </summary>
private List<ICommand> _listCommand = new List<ICommand>();
/**//// <summary>
/// 命令集合中當前要執行的命令的索引
/// </summary>
private int current = 0;
/**//// <summary>
/// 執行Sql
/// </summary>
/// <param name="action">操作的方法</param>
/// <param name="mm">Message實體對象</param>
/// <returns>操作的方法及操作的信息</returns>
public string Do(Action action, MessageModel mm)
{
string rtn = "";
ICommand cmd = new SqlMessageCommand(action, mm);
rtn = cmd.Execute();
_listCommand.Add(cmd);
current++;
return rtn;
}
/**//// <summary>
/// 撤銷
/// </summary>
/// <param name="levels">執行撤銷操作的次數</param>
/// <returns>操作的方法及操作的信息(用空格分開多條記錄)</returns>
public string Undo(int levels)
{
string rtn = "";
for (int i = 0; i < levels; i++)
{
if (current > 0)
{
ICommand cmd = _listCommand[--current];
rtn += cmd.UnExecute() + " ";
}
}
return rtn;
}
/**//// <summary>
/// 重復
/// </summary>
/// <param name="levels">執行重復操作的次數</param>
/// <returns>操作的方法及操作的信息(用空格分開多條記錄)</returns>
public string Redo(int levels)
{
string rtn = "";
for (int i = 0; i < levels; i++)
{
if (current < _listCommand.Count - 1)
{
ICommand cmd = _listCommand[current++];
rtn += cmd.UnExecute() + " ";
}
}
return rtn;
}
}
}