設計模式之美:Adapter(適配器)


索引

別名

  • 包裝器(Wrapper)

意圖

將一個類的接口轉換成客戶希望的另外一個接口。

Adapter 模式使得原本由於接口不兼容而不能一起工作的那些類可以一起工作。

Convert the interface of a class into another interface clients expect.

Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

結構

類適配器使用多重繼承對一個接口與另一個接口進行匹配。

對象適配器依賴於對象組合。

參與者

Target

  • 定義 Client 使用的與特定領域相關的接口。

Client

  • 與符合 Target 接口的對象協同。

Adaptee

  • 定義一個已經存在的接口,這個接口需要適配。

Adapter

  • 對 Adaptee 的接口與 Target 接口進行適配。

適用性

在以下情況下可以使用 Adapter 模式:

  • 你想使用一個已經存在的類,而它的接口不符合你的需求。
  • 你想創建一個可以復用的類,該類可以與其他不相關的類或不可預見的類協同工作。
  • 你想使用一些已經存在的類,但是不可能對每一個都進行子類化匹配它們的接口。對象適配器可以適配它的父類的接口。

效果

  • 允許一個 Adapter 與多個 Adaptee (Adaptee 本身及它的子類)同時協同。Adapter 也可以一次給所有的 Adaptee 添加功能。
  • 使得重定義 Adaptee 的行為比較困難。這就需要生成 Adaptee 的子類並且使得 Adapter 引用這個子類而不是引用 Adaptee 自身。

相關模式

  • Bridge 模式的結構與對象 Adapter 模式類似,但是 Bridge 模式的出發點不同:Bridge 目的是將接口部分和實現部分分離,從而對它們可以較為容易也相對獨立的加以改變。而 Adapter 則意味着改變一個已有對象的接口。
  • Decorator 模式增強了其他對象的功能而同時又不改變它的接口。因此 Decorator 對應用程序的透明性比 Adapter 要好。結果是 Decorator 支持遞歸組合,而 Adapter 無法實現這一點。
  • Proxy 模式在不改變它的接口的條件下,為另一個對象定義了一個代理。

實現

實現方式(一):簡單直接的對象適配器。

 1 namespace AdapterPattern.Implementation1
 2 {
 3   public class ParentAdaptee
 4   {
 5     public virtual string SpecificRequest()
 6     {
 7       return "ParentAdaptee";
 8     }
 9   }
10 
11   public class ChildAdaptee : ParentAdaptee
12   {
13     public override string SpecificRequest()
14     {
15       return "ChildAdaptee";
16     }
17   }
18 
19   public class Target
20   {
21     public virtual string Request()
22     {
23       return "Target";
24     }
25   }
26 
27   public class Adapter : Target
28   {
29     private ParentAdaptee _adaptee;
30 
31     public Adapter(ParentAdaptee adaptee)
32     {
33       _adaptee = adaptee;
34     }
35 
36     public override string Request()
37     {
38       return _adaptee.SpecificRequest();
39     }
40   }
41 
42   public class Client
43   {
44     public void TestCase1()
45     {
46       ParentAdaptee adaptee = new ChildAdaptee();
47       Target target = new Adapter(adaptee);
48       var result = target.Request();
49     }
50   }
51 }

實現方式(二):實現雙向類適配器。

 1 namespace AdapterPattern.Implementation2
 2 {
 3   public class ParentAdaptee
 4   {
 5     public virtual string SpecificRequest()
 6     {
 7       return "ParentAdaptee";
 8     }
 9   }
10 
11   public class ChildAdaptee : ParentAdaptee
12   {
13     public override string SpecificRequest()
14     {
15       return "ChildAdaptee";
16     }
17   }
18 
19   public interface ITarget
20   {
21     string Request();
22   }
23 
24   public class Adapter : ChildAdaptee, ITarget
25   {
26     public Adapter()
27     {
28     }
29 
30     public string Request()
31     {
32       return base.SpecificRequest();
33     }
34   }
35 
36   public class Client
37   {
38     public void TestCase2()
39     {
40       ITarget target = new Adapter();
41       var result = target.Request();
42     }
43   }
44 }

設計模式之美》為 Dennis Gao 發布於博客園的系列文章,任何未經作者本人同意的人為或爬蟲轉載均為耍流氓。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM