後續內容將包括以下結構模式:
適配器模式(Adapter):Match interfaces of different classes合成模式(Composite):A tree structure of simple and composite objects裝飾模式(Decorator):Add responsibilities to objects dynamically代理模式(Proxy):An object representing another object享元模式(Flyweight):A fine-grained instance used for efficient sharing門面模式(Facade):A single class that represents an entire subsystem橋樑模式(Bridge):Separates an object interface from its implementation
一、 適配器(Adapter)模式
適配器模式把一個類的介面變換成用戶端所期待的另一種介面,從而使原本介面不匹配而無法在一起工作的兩個類能夠在一起工作。
名稱由來
這很像變壓器(Adapter),變壓器把一種電壓變換成另一種電壓。美國的生活用電電壓是110V,而中國的電壓是220V。如果要在中國使用美國電器,就必須有一個能把220V電壓轉換成110V電壓的變壓器。這個變壓器就是一個Adapter。
Adapter模式也很像貨物的封裝過程:被封裝的貨物的真實樣子被封裝所掩蓋和改變,因此有人把這種模式叫做封裝(Wrapper)模式。事實上,大家經常寫很多這樣的Wrapper類,把已有的一些類封裝起來,使之有能滿足需要的介面。
適配器模式的兩種形式
適配器模式有類的適配器模式和對象的適配器模式兩種。我們將分別討論這兩種Adapter模式。
二、 類的Adapter模式的結構:
由圖中可以看出,Adaptee類沒有Request方法,而客戶期待這個方法。為了使客戶能夠使用Adaptee類,提供一個中間環節,即類Adapter類,Adapter類實現了Target介面,並繼承自Adaptee,Adapter類的Request方法重新封裝了Adaptee的SpecificRequest方法,實現了適配的目的。
因為Adapter與Adaptee是繼承的關係,所以這決定了這個適配器模式是類的。
該適配器模式所涉及的角色包括:
目標(Target)角色:這是客戶所期待的介面。因為C#不支援多繼承,所以Target必須是介面,不可以是類。
源(Adaptee)角色:需要適配的類。
適配器(Adapter)角色:把源介面轉換成目標介面。這一角色必須是類。
三、 類的Adapter模式示意性實現:
下面的程式給出了一個類的Adapter模式的示意性的實現: 複製代碼 代碼如下:// Class Adapter pattern -- Structural example
using System;
// "ITarget"
interface ITarget
{
// Methods
void Request();
}
// "Adaptee"
class Adaptee
{
// Methods
public void SpecificRequest()
{
Console.WriteLine("Called SpecificRequest()" );
}
}
// "Adapter"
class Adapter : Adaptee, ITarget
{
// Implements ITarget interface
public void Request()
{
// Possibly do some data manipulation
// and then call SpecificRequest
this.SpecificRequest();
}
}
/**//// <summary>
/// Client test
/// </summary>
public class Client
{
public static void Main(string[] args)
{
// Create adapter and place a request
ITarget t = new Adapter();
t.Request();
}
}
四、 對象的Adapter模式的結構:
可以看出:用戶端需要調用Request方法,而Adaptee沒有該方法,為了使用戶端能夠使用Adaptee類,需要提供一個封裝(Wrapper)類Adapter。這個封裝類封裝了一個Adaptee的執行個體,從而將用戶端與Adaptee銜接起來。由於Adapter與Adaptee是委派關係,這決定了這個適配器模式是對象的。
該適配器模式所涉及的角色包括:
目標(Target)角色:這是客戶所期待的介面。目標可以是具體的或抽象的類,也可以是介面。
源(Adaptee)角色:需要適配的類。
適配器(Adapter)角色:通過在內部封裝(Wrap)一個Adaptee對象,把源介面轉換成目標介面。
五、 對象的Adapter模式示意性實現:
下面的程式給出了一個類的Adapter模式的示意性的實現: 複製代碼 代碼如下:// Adapter pattern -- Structural example
using System;
// "Target"
class Target
{
// Methods
virtual public void Request()
{
// Normal implementation goes here
}
}
// "Adapter"
class Adapter : Target
{
// Fields
private Adaptee adaptee = new Adaptee();
// Methods
override public void Request()
{
// Possibly do some data manipulation
// and then call SpecificRequest
adaptee.SpecificRequest();
}
}
// "Adaptee"
class Adaptee
{
// Methods
public void SpecificRequest()
{
Console.WriteLine("Called SpecificRequest()" );
}
}
/**//// <summary>
/// Client test
/// </summary>
public class Client
{
public static void Main(string[] args)
{
// Create adapter and place a request
Target t = new Adapter();
t.Request();
}
}
六、 在什麼情況下使用適配器模式
在以下各種情況下使用適配器模式:
1、 系統需要使用現有的類,而此類的介面不符合系統的需要。
2、 想要建立一個可以重複使用的類,用於與一些彼此之間沒有太大關聯的一些類,包括一些可能在將來引進的類一起工作。這些源類不一定有很複雜的介面。
3、 (對對象適配器而言)在設計裡,需要改變多個已有子類的介面,如果使用類的適配器模式,就要針對每一個子類做一個適配器,而這不太實際。
七、 一個實際應用Adapter模式的例子
下面的程式示範了Class Adapter與Object Adapter的應用。 複製代碼 代碼如下:// Example of implementing the Adapter pattern
using System;
// Target
public interface ICar
{
void Drive();
}
// Direct use without Adapter
public class CToyota : ICar
{
public void Drive()
{
Console.WriteLine("Vroom Vroom, we're off in our Toyota");
}
}
// Adaptee
public class CCessna
{
public void Fly()
{
Console.WriteLine("Static runup OK, we're off in our C172");
}
}
// Class Adapter
public class CDrivableCessna : CCessna, ICar
{
public void Drive() { base.Fly(); }
}
// Object Adapter
public class CDrivableCessna2 : ICar
{
private CCessna m_oContained;
public CDrivableCessna2()
{
m_oContained = new CCessna();
}
public void Drive() { m_oContained.Fly(); }
}
// Client
public class Client
{
public static void Main(string[] args)
{
ICar oCar = new CToyota();
Console.Write("Class Adapter: Driving an Automobile");
oCar.Drive();
oCar = new CDrivableCessna();
Console.Write("Driving a Cessna");
oCar.Drive();
oCar = new CDrivableCessna2();
Console.Write(" Object Adapter: Driving a Cessna");
oCar.Drive();
}
}
八、 關於Adapter模式的討論
Adapter模式在實現時有以下這些值得注意的地方:
1、 目標介面可以省略,模式發生退化。但這種做法看似平庸而並不平庸,它可以使Adaptee不必實現不需要的方法(可以參考Default Adapter模式)。其表現形式就是父類實現預設方法,而子類只需實現自己獨特的方法。這有些像模板(Template)模式。
2、 適配器類可以是抽象類別。
3、 帶參數的適配器模式。使用這種辦法,適配器類可以根據參數返還一個合適的執行個體給用戶端。