定義:將一個類的接口轉化成客戶希望的另一個接口,適配器模式讓那些接口不兼容的類可以一起工作。別名(包裝器[Wrapper]模式) 它屬于創(chuàng)建型模式的成員,何為創(chuàng)建型模式:就是關注如何將現有類或對象組織在一起形成更大的結構。由于系統(tǒng)中存在類和對象,所以存在兩種結構型模式:類結構型模式(關注類的組合,由多個類組成一個更大的系統(tǒng),一般只有繼承關系和實現關系)、對象結構型模式(關注類和對象的組合,通過關聯關系,在一個類中定義另一個類的對象,然后通過該對象調用相關方法)。 適配器,適配器,筆記本要用的就是電源適配器(AC Adapter),沒錯,有了電源適配器,我們筆記本就能使用本來不能使用的220V的電壓了,這也就是電源適配器的功勞。我們這里也一樣,引入適配器模式以兼容某些結構。
結構:
簡單分析:
上圖是課上老師給的例子,這樣理解起來就很容易了。偽裝者即適配器類,由于要用到鴨子的方法,所以用火雞偽裝下,它關聯鴨子,就可以在內部生成鴨子的對象。即披著鴨子皮的火雞,讓小朋友們誤以為是鴨子在表演。
定義:缺當不需要實現一個接口所提供的所有方法時,可先設計一個抽象類實現該接口,并為接口中每個方法提供一個默認實現(空方法),那么該抽象類的子類可以選擇性地覆蓋父類的某些方法來實現需求,它適用于不想使用一個接口中的所有方法的情況,又稱為單接口適配器模式。 結構:
適配器中同時包含對目標類和適配器類的引用,適配者可以通過調用目標類中的方法,目標類也可以通過它調用適配者類中的方法,該適配器即雙向適配器 結構: 1 public class Adapter : Target, Adaptee 2 { 3 //同時維持對抽象目標類和適配者的引用 4 private Target target; 5 private Adaptee adaptee; 6 7 public Adapter(Target target) 8 { 9 this.target = target; 10 } 11 12 public Adapter(Adaptee adaptee) 13 { 14 this.adaptee = adaptee; 15 } 16 17 public void Request() 18 { 19 adaptee.SpecificRequest(); 20 } 21 22 public void SpecificRequest() 23 { 24 target.Request(); 25 } 26 } 應用:實現一個雙向適配器實例,使得貓(Cat)可以學狗(Dog)叫(Cry()),狗可以學貓捉老鼠(CatchMouse())
實現:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace 適配器模式 7 { 8 public interface IDog 9 { 10 void Action(); 11 void Wang(); 12 } 13 14 public interface ICat 15 { 16 void Cry(); 17 void CatchMouse(); 18 } 19 20 public class Snoopy : IDog 21 { 22 public void Action() 23 { 24 } 25 26 public void Wang() 27 { 28 Console.WriteLine("汪汪的叫......."); 29 } 30 } 31 32 public class Tom : ICat 33 { 34 public void Cry() 35 { 36 } 37 38 public void CatchMouse() 39 { 40 Console.WriteLine("捉老鼠....."); 41 } 42 } 43 44 public class Adapter : ICat, IDog 45 { 46 private IDog dog; 47 private ICat cat; 48 49 public Adapter(IDog d) 50 { 51 this.dog = d; 52 } 53 54 public Adapter(ICat c) 55 { 56 this.cat = c; 57 } 58 59 public void Cry() 60 { 61 dog.Wang(); 62 } 63 64 public void CatchMouse() 65 { 66 cat.CatchMouse(); 67 } 68 69 public void Action() 70 { 71 cat.CatchMouse(); 72 } 73 74 public void Wang() 75 { 76 dog.Wang(); 77 } 78 } 79 80 class Program 81 { 82 static void Main(string[] args) 83 { 84 //這里貓想學狗叫,實現Cry的方法,所以適配者首先是一只貓,它要實現貓的Cry的方法, 85 //但適配者是假的,它只能借助狗的方法來實現。 86 ICat cat = new Adapter(new Snoopy()); 87 Console.Write("Cat learn:"); 88 cat.Cry(); 89 Console.Read(); 90 } 91 } 92 93 }
|
|
|