<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Bridge Pattern.aspx.cs" Inherits="Pattern_Bridge_Pattern" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Bridge 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="BridgePattern" />
<select id="strategy_select" runat="server">
<option value="A" selected="selected">A</option>
<option value="B">B</option>
</select>
</div>
</form>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Bridge 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="BridgePattern" />
<select id="strategy_select" runat="server">
<option value="A" selected="selected">A</option>
<option value="B">B</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>
/// 優點: 將動作抽象和動作實現層分離,是兩者可以獨立的變化,体現靈活性.
/// </summary>
public partial class Pattern_Bridge_Pattern : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void bt_submit_Click(object sender, EventArgs e)
{
BridgeResult();
}
public void BridgeResult()
{
AbstractClass abclass = new RefinedAbstractClass();
switch (strategy_select.Items[strategy_select.SelectedIndex].Value.ToString())
{
case "A":
abclass.MyFields = new ConcrateA();
break;
case "B":
abclass.MyFields = new ConcrateB();
break;
default:
abclass.MyFields = new ConcrateB();
break;
}
tb_result.Text = abclass.Action();
}
}
/// <summary>
/// 定義抽象動作
/// </summary>
abstract class Fields
{
abstract public string Action();
}
class AbstractClass
{
protected Fields fields;
public Fields MyFields
{
set { fields = value; }
}
virtual public string Action()
{
return fields.Action();
}
}
class RefinedAbstractClass : AbstractClass
{
public override string Action()
{
return fields.Action();
}
}
/// <summary>
/// 具體動作類
/// </summary>
class ConcrateA : Fields
{
public override string Action()
{
return "A";
}
}
class ConcrateB : Fields
{
public override string Action()
{
return "B";
}
}
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>
/// 優點: 將動作抽象和動作實現層分離,是兩者可以獨立的變化,体現靈活性.
/// </summary>
public partial class Pattern_Bridge_Pattern : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void bt_submit_Click(object sender, EventArgs e)
{
BridgeResult();
}
public void BridgeResult()
{
AbstractClass abclass = new RefinedAbstractClass();
switch (strategy_select.Items[strategy_select.SelectedIndex].Value.ToString())
{
case "A":
abclass.MyFields = new ConcrateA();
break;
case "B":
abclass.MyFields = new ConcrateB();
break;
default:
abclass.MyFields = new ConcrateB();
break;
}
tb_result.Text = abclass.Action();
}
}
/// <summary>
/// 定義抽象動作
/// </summary>
abstract class Fields
{
abstract public string Action();
}
class AbstractClass
{
protected Fields fields;
public Fields MyFields
{
set { fields = value; }
}
virtual public string Action()
{
return fields.Action();
}
}
class RefinedAbstractClass : AbstractClass
{
public override string Action()
{
return fields.Action();
}
}
/// <summary>
/// 具體動作類
/// </summary>
class ConcrateA : Fields
{
public override string Action()
{
return "A";
}
}
class ConcrateB : Fields
{
public override string Action()
{
return "B";
}
}