設計模式-適配器模式(Adapter)

簡介:函數

適配器模式在我看來是最無聊的一種模式,由於他根本不是一種新的創意模式,而是一種不得已而爲之的模式。就算不學適配器模式,在具體應用場景中也會天然而然的想到這種解決方案。測試

張三在英國留學時買了個筆記本,使用的外接電源爲歐式標準。如今回國了(中國電源標準不一樣意歐洲標準),要想讓筆記本繼續正常工做,有兩種方案,1爲從新改造筆記本,2爲買個電源轉換器,將歐式電源轉換爲中式電源。this

上面中的1,從新改造筆記本,至關於咱們軟件工程中的代碼重構,難度大,風險高。spa

2即爲本文中所描述的適配器模式,只須要稍微修改外部調用接口,內部實現邏輯不用改變。code

適配器模式的定義:對象

將一個類的接口轉換爲客戶但願的另一個接口。適配器模式使得本來因爲接口不兼容而不能一塊兒工做的那些類能夠一塊兒工做。繼承

轉換匹配,複用功能接口

適配器模式的組成:get

1.Target 定義客戶端須要的接口。即上文的中式電源接口string

2.Adaptee,已經存在的接口,一般已經可以知足客戶的需求,可是接口跟客戶端要求的不大一致。即上文的英式電源接口

3.Adapter:適配器,將Adaptee轉換爲Target,即適配器模式的核心。

繼承Target類或者實現Target接口

須要傳入Adaptee對象參數,對外展示爲Target的接口方法,內部實現實爲Adaptee接口方法。

代碼以下:

電腦原始的英式電源(以及其接口類,歐式電源):

/// <summary>
/// 歐式電源供電API
/// </summary>
public interface EurPowerSupplyApi
{
    /// <summary>
    /// 充電
    /// </summary>
    /// <returns></returns>
    void Charge();

    /// <summary>
    /// 供電
    /// </summary>
    /// <returns></returns>
    void Supply();
}

/// <summary>
/// 英式電源供電,對應適配器模式中的Adaptee
/// </summary>
public class UKPowerSupply : EurPowerSupplyApi
{
    public Power MyPower
    {
        get { return _myPower; }
        set { _myPower = value; }
    }
    private Power _myPower;

    public void Charge()
    {
        Console.WriteLine("I am using UK PowerSupply.");
    }

    public void Supply()
    {
        Console.WriteLine("The UK PowerSupply is supplying my PC");
    }
}

原始調用代碼爲:

/// <summary>
/// 原始英式電源供電測試類
/// </summary>
public class AddpterTest
{
    public static void Main(string[] args)
    {
        EurPowerSupplyApi powerSupply = new UKPowerSupply();

        //英式電源充電中
        powerSupply.Charge();
        //英式電源供電中
        powerSupply.Supply();

        Console.ReadLine();
    }
}

中式電源接口(方法名、方法參數都不一樣於原來的歐式電源接口)

/// <summary>
/// 中式電源供電API
/// </summary>
public interface ChinesePowerSupplyAPI
{
    void ChongDian(Power aPower);

    void GongDian(Power aPower);
}

適配器代碼爲:

/// <summary>
/// 電源轉換器,對應適配器模式中的Adapter,須要實現客戶端將於調用的接口(ChinesePowerSupplyAPI)的方法
/// </summary>
public class PowerSupplyAdapter : ChinesePowerSupplyAPI
{
    /// <summary>
    /// 將要被適配、被轉換的接口對象,對應適配器模式中的Adaptee
    /// </summary>
    private EurPowerSupplyApi adaptee;

    /// <summary>
    /// 構造函數,傳入Adaptee
    /// </summary>
    /// <param name="aAdaptee"></param>
    public PowerSupplyAdapter(EurPowerSupplyApi aAdaptee)
    {
        this.adaptee = aAdaptee;
    }

    /// <summary>
    /// 實現Target的ChongDian方法
    /// </summary>
    /// <param name="aPower"></param>
    public void ChongDian(Power aPower) 
    {
        adaptee.Charge();
    }

    /// <summary>
    /// 實現Target的GongDian方法
    /// </summary>
    /// <param name="aPower"></param>
    public void GongDian(Power aPower) 
    {
        adaptee.Supply();
    }
}

適配器測試代碼爲:

/// <summary>
/// Apapter測試類
/// </summary>
public class AddpterTest 
{
    public static void Main(string[] args) 
    {
        //現有英國電源充電器
        UKPowerSupply ukSupply = new UKPowerSupply();
        ukSupply.MyPower = new Power(80);

        //電源適配器,傳入英國電源,能夠轉換爲中式電源
        PowerSupplyAdapter adapter = new PowerSupplyAdapter(ukSupply);
        adapter.ChongDian(ukSupply.MyPower);
        adapter.GongDian(ukSupply.MyPower);

        Console.ReadLine();
    }
}

補充描述:

1.適配器模式的主要功能時進行轉換匹配,目的是複用已有的功能,而不是來實現新的接口。主要負責將不兼容的接口轉換客戶端指望的樣子便可。

2.Adaptee和Target通常爲同類對象(如上文中歐式電源接口和中式電源接口),也能夠爲先後不一樣的兩個版本接口。自己是沒有關聯的,內部方法簽名能夠相同,也能夠不一樣(如本文方法名稱和參數都不相同)。

3.雙向適配器。

上面的例子是適配器的功能是把歐式電源轉換爲中式電源,一樣的,也能夠把中式電源轉換爲歐式電源,還能夠把這兩個接口放在同一個類中。

相關文章
相關標籤/搜索