C#設計形式編程中應用適配器形式構造實戰練習訓練。本站提示廣大學習愛好者:(C#設計形式編程中應用適配器形式構造實戰練習訓練)文章只能為提供參考,不一定能成為您想要的結果。以下是C#設計形式編程中應用適配器形式構造實戰練習訓練正文
在現實的軟件體系設計和開辟中,為了完成某項任務須要購置一個第三方的庫來加速開辟。這帶來一個成績,在運用法式中曾經設計好的功效接口,與這個第三方供給的接口紛歧致。為了使得這些接口不兼容的類可以在一路任務,適配器形式供給了一種接口的適配機制。
適配器形式的設計思惟在生涯中常常會運用到,如我們在給手機充電的時刻,弗成能直接在220V電源上直接充電,而是用手機充電器轉換成手機須要的電壓才可以正常充電,不然就弗成以完成充電,這個充電器就起到了適配的感化。
適配器形式構造完成
1.類適配器構造完成

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();
}
}
}
運轉輸入:
Called SpecificRequest() 請按隨意率性鍵持續. . .
2.對象適配器構造完成
Client須要挪用Request辦法,而Adaptee並沒有該辦法,為了使Client可以或許應用Adaptee類,須要供給一個類Adapter。這個類包括了一個Adaptee的實例,將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();
}
}
}
適配器形式理論運用
以手機充電的電源適配器為例,用適配器形式的處理計劃。

1.類適配器構造完成
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("獲得手機的充電電壓!");
}
}
} 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("手機:");
ITarget t = new Adapter();
t.GetPower();
}
}
}
運轉輸入:
手機: 從電源中獲得220V的電壓 獲得手機的充電電壓! 請按隨意率性鍵持續. . .
2.對象適配器構造完成
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("獲得手機的充電電壓!");
}
}
}
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("手機:");
ITarget t = new Adapter(new Power());
t.GetPower();
}
}
}
適配器形式的優缺陷
在引言部門曾經提出,適配器形式用來處理現有對象與客戶端等待接口紛歧致的成績,上面具體總結下適配器兩種情勢的優缺陷。
1.類的適配器形式:
長處:
可以在不修正原有代碼的基本下去復用現有類,很好地相符 “開閉准繩”
可以從新界說Adaptee(被適配的類)的部門行動,由於在類適配器形式中,Adapter是Adaptee的子類
僅僅引入一個對象,其實不須要額定的字段來援用Adaptee實例(這個等於長處也是缺陷)。
缺陷:
用一個詳細的Adapter類對Adaptee和Target停止婚配,當假如想要婚配一個類和一切它的子類時,類的適配器形式就不克不及勝任了。由於類的適配器形式中沒有引入Adaptee的實例,光挪用this.SpecificRequest辦法其實不能去挪用它對應子類的SpecificRequest辦法。
采取了 “多繼續”的完成方法,帶來了不良的高耦合。
2.對象的適配器形式
長處:
可以在不修正原有代碼的基本下去復用現有類,很好地相符 “開閉准繩”(這點是兩種完成方法都具有的)
采取 “對象組合”的方法,更相符松耦合。
缺陷:
使得重界說Adaptee的行動較艱苦,這就須要生成Adaptee的子類而且使得Adapter援用這個子類而不是援用Adaptee自己。
應用場景
在以下情形下可以斟酌應用適配器形式:
體系須要復用現有類,而該類的接口不相符體系的需求
想要樹立一個可反復應用的類,用於與一些彼此之間沒有太年夜聯系關系的一些類,包含一些能夠在未來引進的類一路任務。
關於對象適配器形式,在設計裡須要轉變多個已有子類的接口,假如應用類的適配器形式,就要針對每個子類做一個適配器,而這不太現實。
.NET中適配器形式的完成
1.適配器形式在.NET Framework中的一個最年夜的運用就是COM Interop。COM Interop就似乎是COM和.NET之間的一座橋梁(關於COM互操作更多內容可以參考我的互操作系列)。COM組件對象與.NET類對象是完整分歧的,但為了使.NET法式像應用.NET對象一樣應用COM組件,微軟在處置方法上采取了Adapter形式,對COM對象停止包裝,這個包裝類就是RCW(Runtime Callable Wrapper)。RCW現實上是runtime生成的一個.NET類,它包裝了COM組件的辦法,並外部完成對COM組件的挪用。以下圖所示:

2..NET中的別的一個適配器形式的運用就是DataAdapter。ADO.NET為同一的數據拜訪供給了多個接口和基類,個中最主要的接口之一是IdataAdapter。DataAdpter起到了數據庫到DataSet橋接器的感化,使運用法式的數據操作同一到DataSet上,而與詳細的數據庫類型有關。乃至可以針對特別的數據源編制本身的DataAdpter,從而使我們的運用法式與這些特別的數據源相兼容。