其它模式:
一.Strategy Pattern(策略模式)
二.Template Method Pattern(模版方法模式)
三.Bridge Pattern(橋接模式)
四.Visitor Pattern(觀察者模式)
五.Action Pattern(命令模式)
一.Strategy Pattern(策略模式)
二.Template Method Pattern(模版方法模式)
三.Bridge Pattern(橋接模式)
四.Visitor Pattern(觀察者模式)
五.Action Pattern(命令模式)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Action Pattern.aspx.cs" Inherits="Pattern_Action_Pattern" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</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>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</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();
}
}