簡單了解C#設(shè)計(jì)模式編程中的橋接模式
橋接模式的概念
定義:將抽象部分與實(shí)現(xiàn)部分分離,使它們都可以獨(dú)立的變化。
理解:為啦解決一個對象變化而影響多個對象跟著變化,需要把具體實(shí)現(xiàn)對象抽象化,使降低對象和變化因素的耦合度,提高系統(tǒng)的可維護(hù)性和擴(kuò)展性。
舉例:
手機(jī)系統(tǒng)的生態(tài)圈問題:
啰嗦點(diǎn):眾所周知wp的生態(tài)圈相對與有些系統(tǒng)較差,各位需努力,諾基亞走下神壇,wp要走上神壇,頂一下哈。
wp/ios系統(tǒng)類:運(yùn)行軟件,可承載本運(yùn)行環(huán)境下的任何軟件,如果新增一個系統(tǒng),軟件就要多做一個系統(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ò)展,不會影響到對方。
- 實(shí)現(xiàn)細(xì)節(jié)對客戶透明,對用于隱藏了具體實(shí)現(xiàn)細(xì)節(jié)。
缺點(diǎn): 增加了系統(tǒng)的復(fù)雜度
使用場景
我們再來看看橋接模式的使用場景,在以下情況下應(yīng)當(dāng)使用橋接模式:
如果一個系統(tǒng)需要在構(gòu)件的抽象化角色和具體化角色之間添加更多的靈活性,避免在兩個層次之間建立靜態(tài)的聯(lián)系。
設(shè)計(jì)要求實(shí)現(xiàn)化角色的任何改變不應(yīng)當(dāng)影響客戶端,或者實(shí)現(xiàn)化角色的改變對客戶端是完全透明的。
需要跨越多個平臺的圖形和窗口系統(tǒng)上。
一個類存在兩個獨(dú)立變化的維度,且兩個維度都需要進(jìn)行擴(kuò)展。
一個實(shí)際應(yīng)用橋接模式的例子
橋接模式也經(jīng)常用于具體的系統(tǒng)開發(fā)中,對于三層架構(gòu)中就應(yīng)用了橋接模式,三層架構(gòu)中的業(yè)務(wù)邏輯層BLL中通過橋接模式與數(shù)據(jù)操作層解耦(DAL),其實(shí)現(xiàn)方式就是在BLL層中引用了DAL層中一個引用。這樣數(shù)據(jù)操作的實(shí)現(xiàn)可以在不改變客戶端代碼的情況下動態(tài)進(jì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
{
// 對記錄的增刪改查操作
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ù)庫中讀取數(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);
}
}
}
相關(guān)文章
C#使用FtpWebRequest與FtpWebResponse完成FTP操作
這篇文章介紹了C#使用FtpWebRequest與FtpWebResponse完成FTP操作的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05
C#實(shí)現(xiàn)簡易灰度圖和酷炫HeatMap熱力圖winform(附DEMO)
本文主要介紹了C#實(shí)現(xiàn)簡易灰度圖和酷炫HeatMap熱力圖winform(附DEMO),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12
C#使用WinRar命令進(jìn)行壓縮和解壓縮操作的實(shí)現(xiàn)方法
這篇文章主要介紹了C#使用WinRar命令進(jìn)行壓縮和解壓縮操作的實(shí)現(xiàn)方法,涉及C#基于Process類操作WinRar命令的相關(guān)實(shí)現(xiàn)技巧,代碼簡潔實(shí)用,需要的朋友可以參考下2016-06-06
時間字符串轉(zhuǎn)換成日期對象datetime的方法
在遇到形如"2012-12-19T17:00:00Z"這樣的時間字符串時,怎樣轉(zhuǎn)換到DateTime類型呢,下面的方法可以解決2013-12-12
C#運(yùn)算符之與,或,異或及移位運(yùn)算小結(jié)
本文是對C#中的與,或,異或及移位運(yùn)算進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來參考下,希望對大家有所幫助2013-10-10
c#使用win32api實(shí)現(xiàn)獲取光標(biāo)位置
本文給大家匯總了2個使用C#實(shí)現(xiàn)獲取光標(biāo)位置的代碼,非常的簡單實(shí)用,第二種方法更為全面,推薦給大家。2016-02-02

