C#設(shè)計(jì)模式編程中運(yùn)用適配器模式結(jié)構(gòu)實(shí)戰(zhàn)演練
在實(shí)際的軟件系統(tǒng)設(shè)計(jì)和開發(fā)中,為了完成某項(xiàng)工作需要購買一個(gè)第三方的庫來加快開發(fā)。這帶來一個(gè)問題,在應(yīng)用程序中已經(jīng)設(shè)計(jì)好的功能接口,與這個(gè)第三方提供的接口不一致。為了使得這些接口不兼容的類可以在一起工作,適配器模式提供了一種接口的適配機(jī)制。
  適配器模式的設(shè)計(jì)思想在生活中經(jīng)常會(huì)應(yīng)用到,如我們?cè)诮o手機(jī)充電的時(shí)候,不可能直接在220V電源上直接充電,而是用手機(jī)充電器轉(zhuǎn)換成手機(jī)需要的電壓才可以正常充電,否則就不可以完成充電,這個(gè)充電器就起到了適配的作用。
適配器模式結(jié)構(gòu)實(shí)現(xiàn)
1.類適配器結(jié)構(gòu)實(shí)現(xiàn)
ITarget.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DesignPatterns.AdapterPattern.Structural.ClassAdapter
{
 public interface ITarget
 {
  void Request();
 }
}  Adaptee.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DesignPatterns.AdapterPattern.Structural.ClassAdapter
{
 public class Adaptee
 {
  public void SpecificRequest()
  {
   Console.WriteLine("Called SpecificRequest()");
  }
 }
}  Adapter.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DesignPatterns.AdapterPattern.Structural.ClassAdapter
{
 public class Adapter : Adaptee, ITarget
 {
  public void Request()
  {
   this.SpecificRequest();
  }
 }
}  Client.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DesignPatterns.AdapterPattern.Structural.ClassAdapter
{
 public class Client
 {
  static void Main(string[] args)
  {
   ITarget t = new Adapter();
   t.Request();
  }
 }
}  
運(yùn)行輸出:
Called SpecificRequest() 請(qǐng)按任意鍵繼續(xù). . .
2.對(duì)象適配器結(jié)構(gòu)實(shí)現(xiàn)
Client需要調(diào)用Request方法,而Adaptee并沒有該方法,為了使Client能夠使用Adaptee類,需要提供一個(gè)類Adapter。這個(gè)類包含了一個(gè)Adaptee的實(shí)例,將Client與Adaptee銜接起來。
ITarget.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DesignPatterns.AdapterPattern.Structural.ObjectAdapter
{
 public interface ITarget
 {
  void Request();
 }
}  
Target.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DesignPatterns.AdapterPattern.Structural.ObjectAdapter
{
 public class Target : ITarget
 {
  public virtual void Request()
  {
   Console.WriteLine("Called Target Request()");
  }
 }
}  
Adaptee.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DesignPatterns.AdapterPattern.Structural.ObjectAdapter
{
 public class Adaptee
 {
  public void SpecificRequest()
  {
   Console.WriteLine("Called SpecificRequest()");
  }
 }
} 
Adapter.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DesignPatterns.AdapterPattern.Structural.ObjectAdapter
{
 public class Adapter : Target
 {
  private Adaptee _adaptee = new Adaptee();
  public override void Request()
  {
   _adaptee.SpecificRequest();
  }
 }
} 
Client.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DesignPatterns.AdapterPattern.Structural.ObjectAdapter
{
 public class Client
 {
  static void Main(string[] args)
  {
   ITarget t = new Adapter();
   t.Request();
  }
 }
}
適配器模式實(shí)踐應(yīng)用
以手機(jī)充電的電源適配器為例,用適配器模式的解決方案。
1.類適配器結(jié)構(gòu)實(shí)現(xiàn)
ITarget.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DesignPatterns.AdapterPattern.Practical.ClassAdapter
{
 public interface ITarget
 {
  void GetPower();
 }
}  Power.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DesignPatterns.AdapterPattern.Practical.ClassAdapter
{
 public class Power
 {
  public void GetPower220V()
  {
   Console.WriteLine("從電源中得到220V的電壓");
  }
 }
}  Adapter.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DesignPatterns.AdapterPattern.Practical.ClassAdapter
{
 public class Adapter : Power, ITarget
 {
  public void GetPower()
  {
   this.GetPower220V();
   Console.WriteLine("得到手機(jī)的充電電壓!");
  }
 }
}  Client.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DesignPatterns.AdapterPattern.Practical.ClassAdapter
{
 public class Client
 {
  static void Main(string[] args)
  {
   Console.WriteLine("手機(jī):");
   ITarget t = new Adapter();
   t.GetPower();
  }
 }
}  
運(yùn)行輸出:
手機(jī): 從電源中得到220V的電壓 得到手機(jī)的充電電壓! 請(qǐng)按任意鍵繼續(xù). . .
2.對(duì)象適配器結(jié)構(gòu)實(shí)現(xiàn)
ITarget.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DesignPatterns.AdapterPattern.Practical.ObjectAdapter
{
 public interface ITarget
 {
  void GetPower();
 }
} 
Power.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DesignPatterns.AdapterPattern.Practical.ObjectAdapter
{
 public class Power
 {
  public void GetPower220V()
  {
   Console.WriteLine("從電源中得到220V的電壓");
  }
 }
} 
 Adapter.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DesignPatterns.AdapterPattern.Practical.ObjectAdapter
{
 public class Adapter : ITarget
 {
  public Power _power;
  public Adapter(Power power)
  {
   this._power = power;
  }
  /// <summary>
  /// 得到想要的電壓
  /// </summary>
  public void GetPower()
  {
   _power.GetPower220V();
   Console.WriteLine("得到手機(jī)的充電電壓!");
  }
 }
} 
 Client.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DesignPatterns.AdapterPattern.Practical.ObjectAdapter
{
 public class Client
 {
  static void Main(string[] args)
  {
   Console.WriteLine("手機(jī):");
   ITarget t = new Adapter(new Power());
   t.GetPower();
  }
 }
}
適配器模式的優(yōu)缺點(diǎn)
在引言部分已經(jīng)提出,適配器模式用來解決現(xiàn)有對(duì)象與客戶端期待接口不一致的問題,下面詳細(xì)總結(jié)下適配器兩種形式的優(yōu)缺點(diǎn)。
1.類的適配器模式:
優(yōu)點(diǎn):
可以在不修改原有代碼的基礎(chǔ)上來復(fù)用現(xiàn)有類,很好地符合 “開閉原則”
可以重新定義Adaptee(被適配的類)的部分行為,因?yàn)樵陬愡m配器模式中,Adapter是Adaptee的子類
僅僅引入一個(gè)對(duì)象,并不需要額外的字段來引用Adaptee實(shí)例(這個(gè)即是優(yōu)點(diǎn)也是缺點(diǎn))。
缺點(diǎn):
用一個(gè)具體的Adapter類對(duì)Adaptee和Target進(jìn)行匹配,當(dāng)如果想要匹配一個(gè)類以及所有它的子類時(shí),類的適配器模式就不能勝任了。因?yàn)轭惖倪m配器模式中沒有引入Adaptee的實(shí)例,光調(diào)用this.SpecificRequest方法并不能去調(diào)用它對(duì)應(yīng)子類的SpecificRequest方法。
采用了 “多繼承”的實(shí)現(xiàn)方式,帶來了不良的高耦合。
2.對(duì)象的適配器模式
優(yōu)點(diǎn):
可以在不修改原有代碼的基礎(chǔ)上來復(fù)用現(xiàn)有類,很好地符合 “開閉原則”(這點(diǎn)是兩種實(shí)現(xiàn)方式都具有的)
采用 “對(duì)象組合”的方式,更符合松耦合。
缺點(diǎn):
使得重定義Adaptee的行為較困難,這就需要生成Adaptee的子類并且使得Adapter引用這個(gè)子類而不是引用Adaptee本身。
使用場(chǎng)景
在以下情況下可以考慮使用適配器模式:
系統(tǒng)需要復(fù)用現(xiàn)有類,而該類的接口不符合系統(tǒng)的需求
想要建立一個(gè)可重復(fù)使用的類,用于與一些彼此之間沒有太大關(guān)聯(lián)的一些類,包括一些可能在將來引進(jìn)的類一起工作。
對(duì)于對(duì)象適配器模式,在設(shè)計(jì)里需要改變多個(gè)已有子類的接口,如果使用類的適配器模式,就要針對(duì)每一個(gè)子類做一個(gè)適配器,而這不太實(shí)際。
.NET中適配器模式的實(shí)現(xiàn)
1.適配器模式在.NET Framework中的一個(gè)最大的應(yīng)用就是COM Interop。COM Interop就好像是COM和.NET之間的一座橋梁(關(guān)于COM互操作更多內(nèi)容可以參考我的互操作系列)。COM組件對(duì)象與.NET類對(duì)象是完全不同的,但為了使.NET程序像使用.NET對(duì)象一樣使用COM組件,微軟在處理方式上采用了Adapter模式,對(duì)COM對(duì)象進(jìn)行包裝,這個(gè)包裝類就是RCW(Runtime Callable Wrapper)。RCW實(shí)際上是runtime生成的一個(gè).NET類,它包裝了COM組件的方法,并內(nèi)部實(shí)現(xiàn)對(duì)COM組件的調(diào)用。如下圖所示:
2..NET中的另外一個(gè)適配器模式的應(yīng)用就是DataAdapter。ADO.NET為統(tǒng)一的數(shù)據(jù)訪問提供了多個(gè)接口和基類,其中最重要的接口之一是IdataAdapter。DataAdpter起到了數(shù)據(jù)庫到DataSet橋接器的作用,使應(yīng)用程序的數(shù)據(jù)操作統(tǒng)一到DataSet上,而與具體的數(shù)據(jù)庫類型無關(guān)。甚至可以針對(duì)特殊的數(shù)據(jù)源編制自己的DataAdpter,從而使我們的應(yīng)用程序與這些特殊的數(shù)據(jù)源相兼容。
上一篇:深入解析C#設(shè)計(jì)模式編程中對(duì)建造者模式的運(yùn)用
欄 目:C#教程
下一篇:舉例講解C#編程中對(duì)設(shè)計(jì)模式中的單例模式的運(yùn)用
本文標(biāo)題:C#設(shè)計(jì)模式編程中運(yùn)用適配器模式結(jié)構(gòu)實(shí)戰(zhàn)演練
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/6680.html
您可能感興趣的文章
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并打開的方法
 - 01-10C#實(shí)現(xiàn)Winform中打開網(wǎng)頁頁面的方法
 - 01-10C#實(shí)現(xiàn)由四周向中心縮小的窗體退出特效
 - 01-10Extjs4如何處理后臺(tái)json數(shù)據(jù)中日期和時(shí)間
 - 01-10C#編程實(shí)現(xiàn)自定義熱鍵的方法
 - 01-10C#中DataGridView常用操作實(shí)例小結(jié)
 - 01-10C#編程獲取資源文件中圖片的方法
 - 01-10深入淺出23種設(shè)計(jì)模式
 - 01-10asp.net中XML如何做增刪改查操作
 - 01-10C#利用反射技術(shù)實(shí)現(xiàn)去掉按鈕選中時(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文件的方法
 - 04-02jquery與jsp,用jquery
 - 01-10C#中split用法實(shí)例總結(jié)
 - 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
 - 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
 - 01-11ajax實(shí)現(xiàn)頁面的局部加載
 - 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
 - 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
 - 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
 - 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
 


