假想案例說明
需要的知識
結構圖
橋接模式的定義
橋梁模式所涉及的角色有
abstract public class book
{
abstract public double promotionPrice();
public proxy _proxy;
/// <summary>
/// 圖書名稱
/// </summary>
public string bookName
{
get;
set;
}
}
public class computerBook:book
{
/// <summary>
/// 計算機圖書打八折
/// </summary>
/// <param name=
/// <returns>經過圖書正常打折以及代理商後的價格</returns>
public override double promotionPrice()
{
//throw new NotImplementedException();
bookPrice = bookPrice *
bookPrice = base
return bookPrice;
}
/// <summary>
/// 圖書價格
/// </summary>
double bookPrice;
/// <summary>
/// 構造函數
/// </summary>
/// <param name=
public computerBook(double _bookPrice)
{
this
base
}
}
abstract public class proxy
{
abstract public double proxyPrice(double promotionPrice);
/// <summary>
/// 代理商名稱
/// </summary>
public string proxyName
{
get;
set;
}
/// <summary>
/// 代理商級別
/// </summary>
public string proxyTye
{
get;
set;
}
}
public class diamondProxy:proxy
{
/// <summary>
/// 鑽石級代理商的價格為圖書正常打完折後的價格的八折
/// </summary>
/// <param name=
/// <returns></returns>
public override double proxyPrice(double promotionPrice)
{
return promotionPrice *
}
//decimal promotionPrice;
public diamondProxy()
{
base
base
}
}
代碼以及結構圖分析
使用繼承的結構圖如下
使用繼承的代碼如下
interface IBook
{
/// <summary>
/// 圖書的價格接口
/// </summary>
/// <returns></returns>
double price();
}
interface IPromotion:IBook
{
/// <summary>
/// 圖書正常打折後的價格接口
/// </summary>
/// <returns></returns>
double promotionPrice();
}
interface IProxy:IBook
{
/// <summary>
/// 代理商打折後的價格接口
/// </summary>
/// <returns></returns>
double proxyPrice();
}
public class computerBook:IPromotion
{
/// <summary>
/// 計算機圖書打八折後的價格
/// </summary>
/// <returns></returns>
public double promotionPrice()
{
return bookprice *
}
double bookprice;
public computerBook(double _bookprice)
{
this
}
/// <summary>
/// 返回打折後的圖片價格
/// </summary>
/// <returns></returns>
public double price()
{
return this
}
}
public class diamondProxy:IProxy
{
/// <summary>
/// 鑽石代理商打八折後的價格
/// </summary>
/// <returns></returns>
public double proxyPrice()
{
return bookprice *
}
double bookprice;
public diamondProxy(double _bookprice)
{
this
}
/// <summary>
/// 返回打折後的圖片價格
/// </summary>
/// <returns></returns>
public double price()
{
return this
}
}
橋接與使用繼承的比較
//圖書類型:計算機
//原始價格:
//代理商級別
//計算最後實際等到的金額
book _Book = new computerBook(
_Book
double proxyPrice = _Book
//實例化一本計算機圖書
IBook _Book = new computerBook(
//得到計算機打完折後的價格
double price = _Book
//實例化一位鑽石代理商
IBook _BookProxy = new diamondProxy(price);
//把正常打折後的圖片價格做為基數來計算最終價格
price = _BookProxy
橋梁模式適用場合
總結
From:http://tw.wingwit.com/Article/program/net/201311/13377.html