今天学习命令模式,用ASP.NET做了个例子,基本上是沿袭了李建忠老师的视频上的例子,当然,比那个演示用的例子要丰富些 ^_^。
功能很简单,就是一个网页,向一个文本文件写入页面上输入的字符串,然后可以进行撤销和重做操作。
命令模式包括4个参与者:命令执行者(就是做实际操作的类),命令的请求者(就是封装了所有命令的类),命令(封装了某组操作的类),以及一个为命令定义操作的接口,分别对应结构图中的Receiver、Invoker、ConcreteCommand和Command。(这个图到处都有,我就不贴了,哈哈。),对应我的代码中的MyDocument、MyOperations、DocumentCommand、IOperation。
使用的APS.NET页面也很简单,一个文本框,3个按钮
功能很简单,就是一个网页,向一个文本文件写入页面上输入的字符串,然后可以进行撤销和重做操作。
命令模式包括4个参与者:命令执行者(就是做实际操作的类),命令的请求者(就是封装了所有命令的类),命令(封装了某组操作的类),以及一个为命令定义操作的接口,分别对应结构图中的Receiver、Invoker、ConcreteCommand和Command。(这个图到处都有,我就不贴了,哈哈。),对应我的代码中的MyDocument、MyOperations、DocumentCommand、IOperation。
MyDocument类#region MyDocument类
using System;
using System.IO;
using System.Text;
/**//// <summary>
/// MyDocument 的摘要说明
/// </summary>
[Serializable]
public class MyDocument
{
private FileInfo file;
public MyDocument(String filePath)
{
file = new FileInfo(filePath);
}
/**//// <summary>
/// 向文件中写入字符串
/// </summary>
/// <param name="content">字符串</param>
public void WriteString(String content)
{
StreamWriter writer = null;
try
{
writer = file.AppendText();
writer.WriteLine(content);
writer.Flush();
}
finally
{
if (writer != null)
{
writer.Close();
}
}
}
/**//// <summary>
/// 从文件中末尾删除指定的字符串
/// </summary>
/// <param name="content">指定的字符串</param>
public void DeleteString(String content)
{
FileStream stream = null;
try
{
stream = file.Open(FileMode.Open);
stream.SetLength(stream.Length - Encoding.Default.GetByteCount(content) - 2);
}
finally
{
if (stream != null)
{
stream.Close();
}
}
}
}
#endregion
using System;
using System.IO;
using System.Text;
/**//// <summary>
/// MyDocument 的摘要说明
/// </summary>
[Serializable]
public class MyDocument
{
private FileInfo file;
public MyDocument(String filePath)
{
file = new FileInfo(filePath);
}
/**//// <summary>
/// 向文件中写入字符串
/// </summary>
/// <param name="content">字符串</param>
public void WriteString(String content)
{
StreamWriter writer = null;
try
{
writer = file.AppendText();
writer.WriteLine(content);
writer.Flush();
}
finally
{
if (writer != null)
{
writer.Close();
}
}
}
/**//// <summary>
/// 从文件中末尾删除指定的字符串
/// </summary>
/// <param name="content">指定的字符串</param>
public void DeleteString(String content)
{
FileStream stream = null;
try
{
stream = file.Open(FileMode.Open);
stream.SetLength(stream.Length - Encoding.Default.GetByteCount(content) - 2);
}
finally
{
if (stream != null)
{
stream.Close();
}
}
}
}
#endregion
MyOperations类#region MyOperations类
using System;
using System.Collections.Generic;
/**//// <summary>
/// MyOparations 的摘要说明
/// </summary>
[Serializable]
public class MyOperations
{
/**//// <summary>
/// 操作列表
/// </summary>
private Stack<IOperation> operations;
/**//// <summary>
/// 被撤消的操作列表
/// </summary>
private Stack<IOperation> unOperations;
private Boolean canUnDo;
private Boolean canReDo;
/**//// <summary>
/// 能否撤消
/// </summary>
public Boolean CanUnDo
{
get { return canUnDo; }
set { canUnDo = value; }
}
/**//// <summary>
/// 能否重做
/// </summary>
public Boolean CanReDo
{
get { return canReDo; }
set { canReDo = value; }
}
/**//// <summary>
/// 构造函数
/// </summary>
public MyOperations()
{
canUnDo = false;
canReDo = false;
operations = new Stack<IOperation>();
unOperations = new Stack<IOperation>();
}
/**//// <summary>
/// 执行操作
/// </summary>
/// <param name="operation">一个操作</param>
public void Write(IOperation operation)
{
if (operation != null)
{
operation.Write();
operations.Push(operation);
canUnDo = true;
}
}
/**//// <summary>
/// 撤销最后一个操作
/// </summary>
public void UnDo()
{
if (canUnDo)
{
IOperation operation = operations.Pop();
operation.UnDo();
unOperations.Push(operation);
canReDo = true;
if (operations.Count < 1)
{
canUnDo = false;
}
}
}
/**//// <summary>
/// 恢复最近一次被撤销的操作
/// </summary>
public void ReDo()
{
if (canReDo)
{
IOperation operation = unOperations.Pop();
operation.Write();
operations.Push(operation);
canUnDo = true;
if (unOperations.Count < 1)
{
CanReDo = false;
}
}
}
}
#endregion
using System;
using System.Collections.Generic;
/**//// <summary>
/// MyOparations 的摘要说明
/// </summary>
[Serializable]
public class MyOperations
{
/**//// <summary>
/// 操作列表
/// </summary>
private Stack<IOperation> operations;
/**//// <summary>
/// 被撤消的操作列表
/// </summary>
private Stack<IOperation> unOperations;
private Boolean canUnDo;
private Boolean canReDo;
/**//// <summary>
/// 能否撤消
/// </summary>
public Boolean CanUnDo
{
get { return canUnDo; }
set { canUnDo = value; }
}
/**//// <summary>
/// 能否重做
/// </summary>
public Boolean CanReDo
{
get { return canReDo; }
set { canReDo = value; }
}
/**//// <summary>
/// 构造函数
/// </summary>
public MyOperations()
{
canUnDo = false;
canReDo = false;
operations = new Stack<IOperation>();
unOperations = new Stack<IOperation>();
}
/**//// <summary>
/// 执行操作
/// </summary>
/// <param name="operation">一个操作</param>
public void Write(IOperation operation)
{
if (operation != null)
{
operation.Write();
operations.Push(operation);
canUnDo = true;
}
}
/**//// <summary>
/// 撤销最后一个操作
/// </summary>
public void UnDo()
{
if (canUnDo)
{
IOperation operation = operations.Pop();
operation.UnDo();
unOperations.Push(operation);
canReDo = true;
if (operations.Count < 1)
{
canUnDo = false;
}
}
}
/**//// <summary>
/// 恢复最近一次被撤销的操作
/// </summary>
public void ReDo()
{
if (canReDo)
{
IOperation operation = unOperations.Pop();
operation.Write();
operations.Push(operation);
canUnDo = true;
if (unOperations.Count < 1)
{
CanReDo = false;
}
}
}
}
#endregion
DocumentCommand类#region DocumentCommand类
using System;
/**//// <summary>
/// DocumentCommand 的摘要说明
/// </summary>
[Serializable]
public class DocumentCommand : IOperation
{
private MyDocument document;
private String content;
public DocumentCommand(MyDocument document, String content)
{
this.document = document;
this.content = content;
}
IOperation 成员#region IOperation 成员
public void Write()
{
this.document.WriteString(this.content);
}
public void UnDo()
{
this.document.DeleteString(this.content);
}
public void ReDo()
{
this.document.WriteString(this.content);
}
#endregion IOperation 成员
}
#endregion
using System;
/**//// <summary>
/// DocumentCommand 的摘要说明
/// </summary>
[Serializable]
public class DocumentCommand : IOperation
{
private MyDocument document;
private String content;
public DocumentCommand(MyDocument document, String content)
{
this.document = document;
this.content = content;
}
IOperation 成员#region IOperation 成员
public void Write()
{
this.document.WriteString(this.content);
}
public void UnDo()
{
this.document.DeleteString(this.content);
}
public void ReDo()
{
this.document.WriteString(this.content);
}
#endregion IOperation 成员
}
#endregion
IOperation#region IOperation
using System;
/**//// <summary>
/// MyOperation 的摘要说明
/// </summary>
public interface IOperation
{
void Write();
void UnDo();
void ReDo();
}
#endregion
using System;
/**//// <summary>
/// MyOperation 的摘要说明
/// </summary>
public interface IOperation
{
void Write();
void UnDo();
void ReDo();
}
#endregion
使用的APS.NET页面也很简单,一个文本框,3个按钮
ASPX.cs文件#region ASPX.cs文件
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class CommandPattern_CommandSample : System.Web.UI.Page
{
MyDocument document = new MyDocument(@"D:\command.txt");
MyOperations operations = null;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
ViewState["operations"] = new MyOperations();
}
else
{
operations = ViewState["operations"] as MyOperations;
}
}
protected void btnWrite_Click(object sender, EventArgs e)
{
operations.Write(new DocumentCommand(document, this.TextBox1.Text));
}
protected void btnUndo_Click(object sender, EventArgs e)
{
if (operations.CanUnDo)
{
operations.UnDo();
}
else
{
this.ClientScript.RegisterClientScriptBlock(this.GetType(),"warn", "<script language='javascript'>alert(\"Cann't Uedo.\");</script>");
}
}
protected void btnRedo_Click(object sender, EventArgs e)
{
if (operations.CanReDo)
{
operations.ReDo();
}
else
{
this.ClientScript.RegisterClientScriptBlock(typeof(Page), "warn", "<script language='javascript'>alert(\"Cann't Redo.\");</script>");
}
}
}
#endregion
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class CommandPattern_CommandSample : System.Web.UI.Page
{
MyDocument document = new MyDocument(@"D:\command.txt");
MyOperations operations = null;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
ViewState["operations"] = new MyOperations();
}
else
{
operations = ViewState["operations"] as MyOperations;
}
}
protected void btnWrite_Click(object sender, EventArgs e)
{
operations.Write(new DocumentCommand(document, this.TextBox1.Text));
}
protected void btnUndo_Click(object sender, EventArgs e)
{
if (operations.CanUnDo)
{
operations.UnDo();
}
else
{
this.ClientScript.RegisterClientScriptBlock(this.GetType(),"warn", "<script language='javascript'>alert(\"Cann't Uedo.\");</script>");
}
}
protected void btnRedo_Click(object sender, EventArgs e)
{
if (operations.CanReDo)
{
operations.ReDo();
}
else
{
this.ClientScript.RegisterClientScriptBlock(typeof(Page), "warn", "<script language='javascript'>alert(\"Cann't Redo.\");</script>");
}
}
}
#endregion