Title: Five. Action mode (command mode)
终极管理员 知识笔记 62阅读
其他模式:策略模式(策略模式)二。模板法模式(模板方法模式)三。桥接模式(桥接模式)四。访问者模式(观察者模式)与操作模式(命令模式)% @ page language=' c# ' autoeventrewiep=' true ' code=round-color : rgba(245,245,245,1)' '动作模式。aspx。cs ' inherits=' Pattern _ Action _ Pattern ' % html xmlns=' ' head runat=' server ' title无标题页面/标题
style="color: rgba(0, 0, 255, 1)"></head><body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="tb_result" runat="server" TextMode="MultiLine" Rows="5"></asp:TextBox>
<asp:Button ID="bt_submit" runat="server" OnClick="bt_submit_Click" Text="ActionPattern" />
<select id="strategy_select" runat="server">
<option value="A" selected="selected">Begin</option>
<option value="B">Stop</option>
</select>
</div>
</form>
</body>
</html>
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;
/// <summary>
/// 命名模式
/// 好處:1.把執行動作和命令分開,可以隨意新增動作皆點,然後統一一個命令類去調用.
/// 2.很容易的設計一個動作對列,一個一個執行.還很方便的Undo前一個動作產生的效果.
/// 缺點:當一個系統動作較多的時候,比如超過500以上的動作的時候,這種模式就會產生500個動作類,這樣實現起來有點不實際.
///
/// </summary>
public partial class Pattern_Action_Pattern : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void bt_submit_Click(object sender, EventArgs e)
{
Doing d = null;
switch (strategy_select.Items[strategy_select.SelectedIndex].Value.ToString())
{
case "A":
d = new Begin();
break;
case "B":
d = new Stop();
break;
default:
break;
}
Command c = new ConcreteCommand(d);
Invoker i = new Invoker();
i.SetCommand(c);
tb_result.Text = i.ExecuteCommand();
}
}
/// <summary>
/// 定義一個命令抽象類
/// </summary>
public abstract class Command
{
protected Doing doing;
public Command(Doing _doing)
{
this.doing = _doing;
}
public abstract string Execute();
}
/// <summary>
/// 定義一個動作抽象類
/// </summary>
public abstract class Doing
{
public abstract string DoAction();
}
/// <summary>
/// 具體的動作類
/// </summary>
public class Begin : Doing
{
public override string DoAction()
{
return "Begin Action";
}
}
public class Stop : Doing
{
public override string DoAction()
{
return "Stop Action";
}
}
/// <summary>
/// 具體的命令類
/// </summary>
class ConcreteCommand : Command
{
public ConcreteCommand(Doing doing) : base(doing) { }
public override string Execute()
{
return doing.DoAction();
}
}
/// <summary>
/// 調用命令.由命令啟動動作執行
/// </summary>
class Invoker
{
public Command command;
public void SetCommand(Command _command)
{
command = _command;
}
public string ExecuteCommand()
{
return command.Execute();
}
}
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;
/// <summary>
/// 命名模式
/// 好處:1.把執行動作和命令分開,可以隨意新增動作皆點,然後統一一個命令類去調用.
/// 2.很容易的設計一個動作對列,一個一個執行.還很方便的Undo前一個動作產生的效果.
/// 缺點:當一個系統動作較多的時候,比如超過500以上的動作的時候,這種模式就會產生500個動作類,這樣實現起來有點不實際.
///
/// </summary>
public partial class Pattern_Action_Pattern : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void bt_submit_Click(object sender, EventArgs e)
{
Doing d = null;
switch (strategy_select.Items[strategy_select.SelectedIndex].Value.ToString())
{
case "A":
d = new Begin();
break;
case "B":
d = new Stop();
break;
default:
break;
}
Command c = new ConcreteCommand(d);
Invoker i = new Invoker();
i.SetCommand(c);
tb_result.Text = i.ExecuteCommand();
}
}
/// <summary>
/// 定義一個命令抽象類
/// </summary>
public abstract class Command
{
protected Doing doing;
public Command(Doing _doing)
{
this.doing = _doing;
}
public abstract string Execute();
}
/// <summary>
/// 定義一個動作抽象類
/// </summary>
public abstract class Doing
{
public abstract string DoAction();
}
/// <summary>
/// 具體的動作類
/// </summary>
public class Begin : Doing
{
public override string DoAction()
{
return "Begin Action";
}
}
public class Stop : Doing
{
public override string DoAction()
{
return "Stop Action";
}
}
/// <summary>
/// 具體的命令類
/// </summary>
class ConcreteCommand : Command
{
public ConcreteCommand(Doing doing) : base(doing) { }
public override string Execute()
{
return doing.DoAction();
}
}
/// <summary>
/// 調用命令.由命令啟動動作執行
/// </summary>
class Invoker
{
public Command command;
public void SetCommand(Command _command)
{
command = _command;
}
public string ExecuteCommand()
{
return command.Execute();
}
}
标签: