<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Strategy Pattern.aspx.cs" Inherits="Pattern_Strategy_Pattern" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Strategy Pattern</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="StrategyPattern" />
<select id="strategy_select" runat="server">
<option value="A" selected="selected">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</div>
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Strategy Pattern</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="StrategyPattern" />
<select id="strategy_select" runat="server">
<option value="A" selected="selected">A</option>
<option value="B">B</option>
<option value="C">C</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.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// 策略模式,
/// 1.使用情況:1).讓一個對象動態的選擇多種行為.2)讓一個系統動態的選擇多種算法,而且算法使用數據和內容不讓客戶端知道.
/// 2.優點:1)恰當的使用了繼承,可以把行為類的很多公用代碼移到父類,這樣減少了代碼的重複性.
/// 3.缺點:1)各種行為需要共同的接口,因此,在用此模式前,要清楚所有的行為.
/// 4.比較:策略模式与很多其它的模式都有着广泛的联系。Strategy很容易和Bridge模式相混淆。虽然它们结构很相似,但它们却是为解决不同的问题而设计的。
/// Strategy模式注重于算法的封装,而Bridge模式注重于分离抽象和实现,为一个抽象体系提供不同的实现。Bridge模式与Strategy模式都很好的体现了"Favor composite over inheritance"的观点。
/// </summary>
public partial class Pattern_Strategy_Pattern : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private void StrategyFactory()
{
Context ttt = null;
switch (strategy_select.Items[strategy_select.SelectedIndex].Value.ToString())
{
case "A":
ttt = new Context(new ConcreteStrategyA());
tb_result.Text = ttt.ContextInterface();
break;
case "B":
ttt = new Context(new ConcreteStrategyB());
tb_result.Text = ttt.ContextInterface();
break;
case "C":
ttt = new Context(new ConcreteStrategyC());
tb_result.Text = ttt.ContextInterface();
break;
default:
ttt = new Context(new ConcreteStrategyC());
tb_result.Text = ttt.ContextInterface();
break;
}
}
protected void bt_submit_Click(object sender, EventArgs e)
{
StrategyFactory();
}
}
abstract class Strategy
{
abstract public string AlgorithmInterface();
}
class ConcreteStrategyA : Strategy
{
public override string AlgorithmInterface()
{
return "A";
}
}
class ConcreteStrategyB : Strategy
{
public override string AlgorithmInterface()
{
return "B";
}
}
class ConcreteStrategyC : Strategy
{
public override string AlgorithmInterface()
{
return "C";
}
}
class Context
{
Strategy strategy;
public Context(Strategy strategy)
{
this.strategy = strategy;
}
public string ContextInterface()
{
return strategy.AlgorithmInterface();
}
}