第二十五章 命令模式(Command Pattern)
意图
将一个请求封装为一个对象,从而可以用不同的请求对客户进行参数化。并对请求进行排队或记录请求日志。
使用场合
行为需要扩充:将命令作为对象处理,新的行为可以扩充子类完成。
命令需要重做:需要UnDo“撤销”和ReDo“重做”。
......
意图
将一个请求封装为一个对象,从而可以用不同的请求对客户进行参数化。并对请求进行排队或记录请求日志。
使用场合
行为需要扩充:将命令作为对象处理,新的行为可以扩充子类完成。
命令需要重做:需要UnDo“撤销”和ReDo“重做”。
日志记录:命令需要日志记录。
命令排队:需要命令支持队列执行。
结构
Command:声明执行操作的接口。
ConcreteCommand:帮定一个接收者对象到接收者的相应操作以实现操作的执行。
Client:创建一个具体命令对象并设定它的接收者。
Invoker:要求该命令执行这个请求。
Receiver:知道如何实施与执行一个请求相关的操作,任何类都可能作为一个接收者。
![](https://www.cnblogs.com/images/cnblogs_com/williambirkin/DesignPatterns/Command01.gif)
效果
1.Command模式将调用操作的对象与知道如何实现该操作的对象解耦。
2.Command对象可以向其他对象一样被操纵和扩展。
3.将多个命令装配为一个复合命令,这是命令模式和组合模式的组合使用。
4.增加新的命令很容易,因为无须改变已有的类。
缺点
1.增加了类的数目,原来可能存在于窗体菜单调用方法中的代码将被转移到一个命令中,这样可能产生和多小类。
2.运行时命令对象增多,客户必须创建这些对象并将其与命令的执行者相关联。
采用命令模式支持“取消”与“重做”功能
Command接口:
每个命令被封装成一个实现这个接口的类。Execute方法是执行命令的方法。UndoCommand方法是撤销命令的方法。RedoCommand是重做命令的方法。
如果需要操作是不可逆的则需要使用备忘录模式记录变化过程的状态来实现操作的撤销。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
ICommand.cs
using System;
using System.Collections.Generic;
using System.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace CommandPattern.ex25_3
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public interface ICommand
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 操作
/// </summary>
int Execute();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 撤销操作
/// </summary>
int UndoCommand();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 恢复操作
/// </summary>
int RedoCommand();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Command类:
该类实现ICommand接口。功能是将数值加1,并提供“撤销”与“重做”功能。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
AddCmd.cs
using System;
using System.Collections.Generic;
using System.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace CommandPattern.ex25_3
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public class AddCmd:ICommand
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private bool _isexec = false;
private int _value=0;
private int _addvalue=0;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public bool IsExec
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _isexec; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public int Value
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public AddCmd(int v,int addv)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_value=v;
_addvalue = addv;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
ICommand メンバ#region ICommand メンバ
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public int Execute()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (_isexec)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new Exception ("is done");
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_value += _addvalue;
_isexec = true;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return _value;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public int UndoCommand()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!_isexec)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new Exception("not done");
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_value -= _addvalue;
_isexec = false;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return _value;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public int RedoCommand()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (_isexec)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new Exception("have done");
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_value += _addvalue;
_isexec = true;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return _value;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
堆栈类:
用来存储命令的集合类。模拟堆栈的功能。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Commands.cs
using System;
using System.Collections.Generic;
using System.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace CommandPattern.ex25_3
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
class Commands:ICollection<CommandPattern.ex25_3.ICommand>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private List<ICommand> cmds=new List<ICommand>();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
ICollection メンバ#region ICollection<ICommand> メンバ
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void Add(ICommand item)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cmds.Add(item);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void Clear()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cmds.Clear();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public bool Contains(ICommand item)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return cmds.Contains(item);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void CopyTo(ICommand[] array, int arrayIndex)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cmds.CopyTo(array, arrayIndex);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public int Count
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return cmds.Count; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public bool IsReadOnly
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return false; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public bool Remove(ICommand item)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return cmds.Remove(item);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
IEnumerable メンバ#region IEnumerable<ICommand> メンバ
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public IEnumerator<ICommand> GetEnumerator()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return cmds.GetEnumerator();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
IEnumerable メンバ#region IEnumerable メンバ
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return cmds.GetEnumerator();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public CommandPattern.ex25_3.ICommand PopCommand()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (cmds.Count == 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return null;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
CommandPattern.ex25_3.ICommand icmd=cmds[cmds.Count-1];
cmds.RemoveAt(cmds.Count - 1);
return icmd;
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
测试类:
这个类的代码也可以直接写在Main方法中,这里是为了清晰才作了封装。
定义了两个堆栈,一个是记录可执行撤销操作的命令的堆栈(ReDoCommand),一个是记录可执行重做操作的命令的堆栈(UnDoCommand)。
用户循环输入命令字符,执行命令操作。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Tester.cs
using System;
using System.Collections.Generic;
using System.Text;
using CommonFrameworkWBs.ValidateTool;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace CommandPattern.ex25_3
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
class Tester
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private Commands ReDoCommand;
private Commands UnDoCommand;
private int _value = 0;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public int Value
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Tester()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ReDoCommand = new Commands();
UnDoCommand = new Commands();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void Test()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string cmd="";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.WriteLine("Init Value:{0}", this._value);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
while (cmd!="x")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("ReDoCommand:{0},UnDoCommand:{1}",
ReDoCommand.Count, UnDoCommand.Count);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.WriteLine("[e] = Execute ,[u] = UnDo ,[r] = ReDo ,[x] = Exit ,[c] = ClearCommand");
cmd=Console.ReadLine();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
switch(cmd)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case "e":
AddCmd();
break;
case "u":
UnDo();
break;
case "r":
ReDo();
break;
case "c":
UnDoCommand.Clear();
ReDoCommand.Clear();
break;
case"x":
break;
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void AddCmd()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ICommand icmd;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ValidateNumber vn=new ValidateNumber();
Console.WriteLine("请输入整数");
string num=Console.ReadLine();
icmd = new AddCmd(this._value, int.Parse(num));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this._value=icmd.Execute();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.UnDoCommand.Add(icmd);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.WriteLine("Value:{0}", this._value);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void UnDo()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ICommand icmd = UnDoCommand.PopCommand();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (icmd == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("UnDoCommand is null");
return;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this._value = icmd.UndoCommand();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.ReDoCommand.Add(icmd);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.WriteLine("Value:{0}", this._value);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void ReDo()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ICommand icmd = ReDoCommand.PopCommand();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (icmd == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("ReDoCommand is null");
return;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this._value = icmd.RedoCommand();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.UnDoCommand.Add(icmd);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.WriteLine("Value:{0}", this._value);
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
测试类的使用
ex25_3.Tester t=new CommandPattern.ex25_3.Tester();
t.Test();
当然可以定义更多的命令对象,这里为了简单只定义了一个。另外命令对象可以由一个工厂来负责生产。
(转Terrylee的例子)
.NET中的Command模式
在ASP.NET的MVC模式中,有一种叫Front Controller的模式,它分为Handler和Command树两个部分,Handler处理所有公共的逻辑,接收HTTP Post或Get请求以及相关的参数并根据输入的参数选择正确的命令对象,然后将控制权传递到Command对象,由其完成后面的操作,这里面其实就是用到了Command模式。
![](https://www.cnblogs.com/images/cnblogs_com/williambirkin/DesignPatterns/Command02..jpg)
图2 Front Controller 的处理程序部分结构图
![](https://www.cnblogs.com/images/cnblogs_com/williambirkin/DesignPatterns/Command03.jpg)
Handler 类负责处理各个 Web 请求,并将确定正确的 Command 对象这一职责委派给 CommandFactory 类。当 CommandFactory 返回 Command 对象后,Handler 将调用 Command 上的 Execute 方法来执行请求。具体的实现如下
Handler类:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Handler.cs
public class Handler : IHttpHandler
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public void ProcessRequest(HttpContext context)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Command command = CommandFactory.Make(context.Request.Params);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
command.Execute(context);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public bool IsReusable
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return true;
}
}
}
Command接口:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Command.cs
public interface Command
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
void Execute(HttpContext context);
}
CommandFactory类:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
CommandFactory.cs
public class CommandFactory
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public static Command Make(NameValueCollection parms)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
string requestParm = parms["requestParm"];
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Command command = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//根据输入参数得到不同的Command对象
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
switch (requestParm)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case "1":
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
command = new FirstPortal();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
break;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
case "2":
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
command = new SecondPortal();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
break;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
default:
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
command = new FirstPortal();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
break;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return command;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
RedirectCommand类:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
RedirectCommand.cs
public abstract class RedirectCommand : Command
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
//获得Web.Config中定义的key和url键值对,UrlMap类详见下载包中的代码
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private UrlMap map = UrlMap.SoleInstance;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected abstract void OnExecute(HttpContext context);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void Execute(HttpContext context)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
OnExecute(context);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//根据key和url键值对提交到具体处理的页面
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
string url = String.Format("{0}?{1}", map.Map[context.Request.Url.AbsolutePath], context.Request.Url.Query);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
context.Server.Transfer(url);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
FirstPortal类:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
FirstPortal.cs
public class FirstPortal : RedirectCommand
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
protected override void OnExecute(HttpContext context)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//在输入参数中加入项portalId以便页面处理
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
context.Items["portalId"] = "1";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
SecondPortal类
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
SecondPortal.cs
public class SecondPortal : RedirectCommand
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
protected override void OnExecute(HttpContext context)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
context.Items["portalId"] = "2";
}
}
相关模式
1.组合模式:可以采用组合模式来实现宏命令。
2.备忘录模式:可以采用备忘录模式来保存命令的状态。
3.装饰模式: 可以用装饰模式为命令增加记录日志的功能。
4.代理模式:可以用代理来为命令增加权限控制。