簡(jiǎn)單了解C#設(shè)計(jì)模式編程中的橋接模式
橋接模式的概念
定義:將抽象部分與實(shí)現(xiàn)部分分離,使它們都可以獨(dú)立的變化。
理解:為啦解決一個(gè)對(duì)象變化而影響多個(gè)對(duì)象跟著變化,需要把具體實(shí)現(xiàn)對(duì)象抽象化,使降低對(duì)象和變化因素的耦合度,提高系統(tǒng)的可維護(hù)性和擴(kuò)展性。
舉例:
手機(jī)系統(tǒng)的生態(tài)圈問題:
啰嗦點(diǎn):眾所周知wp的生態(tài)圈相對(duì)與有些系統(tǒng)較差,各位需努力,諾基亞走下神壇,wp要走上神壇,頂一下哈。
wp/ios系統(tǒng)類:運(yùn)行軟件,可承載本運(yùn)行環(huán)境下的任何軟件,如果新增一個(gè)系統(tǒng),軟件就要多做一個(gè)系統(tǒng)的版本
weixin/kuwo軟件類:開始運(yùn)行軟件,如果新加一塊軟件,就要做眾多系統(tǒng)版本的。
實(shí)現(xiàn):在iso和wp系統(tǒng)中運(yùn)行,各種不同的軟件。
類圖:
下面我們來根據(jù)具體的代碼了解橋接模式。
軟件代碼:
//軟件
public interface ISoftWare
{
void start();
}
//Kuwo
public class Kuwo : ISoftWare
{
public void start()
{
Console.WriteLine("聽音樂,用酷我!");
}
}
//WeiXin
public class WeiXin : ISoftWare
{
public void start()
{
Console.WriteLine("讓你我的距離更近!");
}
}
操作系統(tǒng)代碼
//操作系統(tǒng),跑軟件
public abstract class System
{
public abstract void Run(ISoftWare software);
}
//Wp
public class WinPhone : System
{
public override void Run(ISoftWare software)
{
Console.WriteLine("Winphone系統(tǒng),給你想要的;");
software.start();
}
}
//Ios
public class Ios : System
{
public override void Run(ISoftWare software)
{
Console.WriteLine("Ios系統(tǒng),給你想不到的;");
software.start();
}
}
客戶端代碼
public static void Main()
{
ISoftWare weixin = new WeiXin();
ISoftWare kuwo = new Kuwo();
//Wp系統(tǒng)
System WpSys = new WinPhone();
WpSys.Run(weixin);
WpSys.Run(kuwo);
//IOS系統(tǒng)
System IosSys = new Ios();
IosSys.Run(weixin);
IosSys.Run(kuwo);
Console.Read();
}
橋接模式的優(yōu)缺點(diǎn)
介紹完橋接模式,讓我們看看橋接模式具體哪些優(yōu)缺點(diǎn)。
優(yōu)點(diǎn):
- 把抽象接口與其實(shí)現(xiàn)解耦。
- 抽象和實(shí)現(xiàn)可以獨(dú)立擴(kuò)展,不會(huì)影響到對(duì)方。
- 實(shí)現(xiàn)細(xì)節(jié)對(duì)客戶透明,對(duì)用于隱藏了具體實(shí)現(xiàn)細(xì)節(jié)。
缺點(diǎn): 增加了系統(tǒng)的復(fù)雜度
使用場(chǎng)景
我們?cè)賮砜纯礃蚪幽J降氖褂脠?chǎng)景,在以下情況下應(yīng)當(dāng)使用橋接模式:
如果一個(gè)系統(tǒng)需要在構(gòu)件的抽象化角色和具體化角色之間添加更多的靈活性,避免在兩個(gè)層次之間建立靜態(tài)的聯(lián)系。
設(shè)計(jì)要求實(shí)現(xiàn)化角色的任何改變不應(yīng)當(dāng)影響客戶端,或者實(shí)現(xiàn)化角色的改變對(duì)客戶端是完全透明的。
需要跨越多個(gè)平臺(tái)的圖形和窗口系統(tǒng)上。
一個(gè)類存在兩個(gè)獨(dú)立變化的維度,且兩個(gè)維度都需要進(jìn)行擴(kuò)展。
一個(gè)實(shí)際應(yīng)用橋接模式的例子
橋接模式也經(jīng)常用于具體的系統(tǒng)開發(fā)中,對(duì)于三層架構(gòu)中就應(yīng)用了橋接模式,三層架構(gòu)中的業(yè)務(wù)邏輯層BLL中通過橋接模式與數(shù)據(jù)操作層解耦(DAL),其實(shí)現(xiàn)方式就是在BLL層中引用了DAL層中一個(gè)引用。這樣數(shù)據(jù)操作的實(shí)現(xiàn)可以在不改變客戶端代碼的情況下動(dòng)態(tài)進(jìn)行更換,下面看一個(gè)簡(jiǎn)單的示例代碼:
// 客戶端調(diào)用
// 類似Web應(yīng)用程序
class Client
{
static void Main(string[] args)
{
BusinessObject customers = new CustomersBusinessObject("ShangHai");
customers.Dataacces = new CustomersDataAccess();
customers.Add("小六");
Console.WriteLine("增加了一位成員的結(jié)果:");
customers.ShowAll();
customers.Delete("王五");
Console.WriteLine("刪除了一位成員的結(jié)果:");
customers.ShowAll();
Console.WriteLine("更新了一位成員的結(jié)果:");
customers.Update("Learning_Hard");
customers.ShowAll();
Console.Read();
}
}
// BLL 層
public class BusinessObject
{
// 字段
private DataAccess dataacess;
private string city;
public BusinessObject(string city)
{
this.city = city;
}
// 屬性
public DataAccess Dataacces
{
get { return dataacess; }
set { dataacess = value; }
}
// 方法
public virtual void Add(string name)
{
Dataacces.AddRecord(name);
}
public virtual void Delete(string name)
{
Dataacces.DeleteRecord(name);
}
public virtual void Update(string name)
{
Dataacces.UpdateRecord(name);
}
public virtual string Get(int index)
{
return Dataacces.GetRecord(index);
}
public virtual void ShowAll()
{
Console.WriteLine();
Console.WriteLine("{0}的顧客有:", city);
Dataacces.ShowAllRecords();
}
}
public class CustomersBusinessObject : BusinessObject
{
public CustomersBusinessObject(string city)
: base(city) { }
// 重寫方法
public override void ShowAll()
{
Console.WriteLine("------------------------");
base.ShowAll();
Console.WriteLine("------------------------");
}
}
/// <summary>
/// 相當(dāng)于三層架構(gòu)中數(shù)據(jù)訪問層(DAL)
/// </summary>
public abstract class DataAccess
{
// 對(duì)記錄的增刪改查操作
public abstract void AddRecord(string name);
public abstract void DeleteRecord(string name);
public abstract void UpdateRecord(string name);
public abstract string GetRecord(int index);
public abstract void ShowAllRecords();
}
public class CustomersDataAccess:DataAccess
{
// 字段
private List<string> customers =new List<string>();
public CustomersDataAccess()
{
// 實(shí)際業(yè)務(wù)中從數(shù)據(jù)庫(kù)中讀取數(shù)據(jù)再填充列表
customers.Add("Learning Hard");
customers.Add("張三");
customers.Add("李四");
customers.Add("王五");
}
// 重寫方法
public override void AddRecord(string name)
{
customers.Add(name);
}
public override void DeleteRecord(string name)
{
customers.Remove(name);
}
public override void UpdateRecord(string updatename)
{
customers[0] = updatename;
}
public override string GetRecord(int index)
{
return customers[index];
}
public override void ShowAllRecords()
{
foreach (string name in customers)
{
Console.WriteLine(" " + name);
}
}
}
上一篇:C#實(shí)現(xiàn)PDF文件添加圖片背景
欄 目:C#教程
下一篇:深入解析C#編程中泛型委托的使用
本文標(biāo)題:簡(jiǎn)單了解C#設(shè)計(jì)模式編程中的橋接模式
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/6695.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)簡(jiǎn)單的Login窗口實(shí)例
- 01-10Winform消除button按下出現(xiàn)的虛線簡(jiǎn)單實(shí)現(xiàn)方法
- 01-10深入淺出23種設(shè)計(jì)模式
- 01-10winform簡(jiǎn)單緩存類實(shí)例
- 01-10C#一個(gè)簡(jiǎn)單的定時(shí)小程序?qū)崿F(xiàn)代碼
- 01-10C#圓角窗體簡(jiǎn)單實(shí)現(xiàn)方法
- 01-10C#使用Mutex簡(jiǎn)單實(shí)現(xiàn)程序單實(shí)例運(yùn)行的方法
- 01-10C#實(shí)現(xiàn)簡(jiǎn)單合并word文檔的方法
- 01-10C#簡(jiǎn)單實(shí)現(xiàn)子窗體向父窗體傳值的方法
- 01-10基于C#實(shí)現(xiàn)簡(jiǎn)單離線注冊(cè)碼生成與驗(yàn)證


閱讀排行
- 1C語言 while語句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 04-02jquery與jsp,用jquery


