亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C#設(shè)計模式編程中運用適配器模式結(jié)構(gòu)實戰(zhàn)演練

 更新時間:2016年02月17日 16:05:30   作者:Libing  
這篇文章主要介紹了C#設(shè)計模式編程中運用適配器模式結(jié)構(gòu)實戰(zhàn)演練,并總結(jié)了適配器模式的優(yōu)缺點和適用場景以及.NET框架中的應(yīng)用,需要的朋友可以參考下

 在實際的軟件系統(tǒng)設(shè)計和開發(fā)中,為了完成某項工作需要購買一個第三方的庫來加快開發(fā)。這帶來一個問題,在應(yīng)用程序中已經(jīng)設(shè)計好的功能接口,與這個第三方提供的接口不一致。為了使得這些接口不兼容的類可以在一起工作,適配器模式提供了一種接口的適配機(jī)制。


  適配器模式的設(shè)計思想在生活中經(jīng)常會應(yīng)用到,如我們在給手機(jī)充電的時候,不可能直接在220V電源上直接充電,而是用手機(jī)充電器轉(zhuǎn)換成手機(jī)需要的電壓才可以正常充電,否則就不可以完成充電,這個充電器就起到了適配的作用。

適配器模式結(jié)構(gòu)實現(xiàn)

1.類適配器結(jié)構(gòu)實現(xiàn)

2016217155956456.png (367×596)

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()
請按任意鍵繼續(xù). . .

2.對象適配器結(jié)構(gòu)實現(xiàn)

  Client需要調(diào)用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();
  }
 }
}


適配器模式實踐應(yīng)用

以手機(jī)充電的電源適配器為例,用適配器模式的解決方案。

2016217160138027.png (367×596)

1.類適配器結(jié)構(gòu)實現(xiàn)
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("得到手機(jī)的充電電壓!");
  }
 }
}  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("手機(jī):");
   ITarget t = new Adapter();
   t.GetPower();
  }
 }
}  

運行輸出:

手機(jī):
從電源中得到220V的電壓
得到手機(jī)的充電電壓!
請按任意鍵繼續(xù). . .

2.對象適配器結(jié)構(gòu)實現(xiàn)
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("得到手機(jī)的充電電壓!");
  }
 }
} 

 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("手機(jī):");
   ITarget t = new Adapter(new Power());
   t.GetPower();
  }
 }
}

適配器模式的優(yōu)缺點
在引言部分已經(jīng)提出,適配器模式用來解決現(xiàn)有對象與客戶端期待接口不一致的問題,下面詳細(xì)總結(jié)下適配器兩種形式的優(yōu)缺點。
1.類的適配器模式:
優(yōu)點:
可以在不修改原有代碼的基礎(chǔ)上來復(fù)用現(xiàn)有類,很好地符合 “開閉原則”
可以重新定義Adaptee(被適配的類)的部分行為,因為在類適配器模式中,Adapter是Adaptee的子類
僅僅引入一個對象,并不需要額外的字段來引用Adaptee實例(這個即是優(yōu)點也是缺點)。
缺點:
用一個具體的Adapter類對Adaptee和Target進(jìn)行匹配,當(dāng)如果想要匹配一個類以及所有它的子類時,類的適配器模式就不能勝任了。因為類的適配器模式中沒有引入Adaptee的實例,光調(diào)用this.SpecificRequest方法并不能去調(diào)用它對應(yīng)子類的SpecificRequest方法。
采用了 “多繼承”的實現(xiàn)方式,帶來了不良的高耦合。
2.對象的適配器模式
優(yōu)點:
可以在不修改原有代碼的基礎(chǔ)上來復(fù)用現(xiàn)有類,很好地符合 “開閉原則”(這點是兩種實現(xiàn)方式都具有的)
采用 “對象組合”的方式,更符合松耦合。
缺點:
使得重定義Adaptee的行為較困難,這就需要生成Adaptee的子類并且使得Adapter引用這個子類而不是引用Adaptee本身。

使用場景
在以下情況下可以考慮使用適配器模式:
系統(tǒng)需要復(fù)用現(xiàn)有類,而該類的接口不符合系統(tǒng)的需求
想要建立一個可重復(fù)使用的類,用于與一些彼此之間沒有太大關(guān)聯(lián)的一些類,包括一些可能在將來引進(jìn)的類一起工作。
對于對象適配器模式,在設(shè)計里需要改變多個已有子類的接口,如果使用類的適配器模式,就要針對每一個子類做一個適配器,而這不太實際。

.NET中適配器模式的實現(xiàn)
1.適配器模式在.NET Framework中的一個最大的應(yīng)用就是COM Interop。COM Interop就好像是COM和.NET之間的一座橋梁(關(guān)于COM互操作更多內(nèi)容可以參考我的互操作系列)。COM組件對象與.NET類對象是完全不同的,但為了使.NET程序像使用.NET對象一樣使用COM組件,微軟在處理方式上采用了Adapter模式,對COM對象進(jìn)行包裝,這個包裝類就是RCW(Runtime Callable Wrapper)。RCW實際上是runtime生成的一個.NET類,它包裝了COM組件的方法,并內(nèi)部實現(xiàn)對COM組件的調(diào)用。如下圖所示:

2016217160319899.png (784×333)

2..NET中的另外一個適配器模式的應(yīng)用就是DataAdapter。ADO.NET為統(tǒng)一的數(shù)據(jù)訪問提供了多個接口和基類,其中最重要的接口之一是IdataAdapter。DataAdpter起到了數(shù)據(jù)庫到DataSet橋接器的作用,使應(yīng)用程序的數(shù)據(jù)操作統(tǒng)一到DataSet上,而與具體的數(shù)據(jù)庫類型無關(guān)。甚至可以針對特殊的數(shù)據(jù)源編制自己的DataAdpter,從而使我們的應(yīng)用程序與這些特殊的數(shù)據(jù)源相兼容。

相關(guān)文章

  • C#多線程之線程中止Abort()方法

    C#多線程之線程中止Abort()方法

    這篇文章介紹了C#多線程中的線程中止Abort()方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • 詳解c# 類的構(gòu)造方法

    詳解c# 類的構(gòu)造方法

    本文主要介紹了c#類的構(gòu)造方法。具有一定的參考價值,下面跟著小編一起來看下吧
    2017-01-01
  • C#實現(xiàn)將文件轉(zhuǎn)換為XML的方法

    C#實現(xiàn)將文件轉(zhuǎn)換為XML的方法

    這篇文章主要介紹了C#實現(xiàn)將文件轉(zhuǎn)換為XML的方法,實例分析了office文件與xml的相互轉(zhuǎn)換技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-12-12
  • c#中文gbk編碼查詢示例代碼

    c#中文gbk編碼查詢示例代碼

    c#中文gbk編碼查詢示例,大家參考使用吧
    2013-12-12
  • Unity使用LineRender實現(xiàn)簽名效果

    Unity使用LineRender實現(xiàn)簽名效果

    這篇文章主要為大家詳細(xì)介紹了Unity使用LineRender實現(xiàn)簽名效果,制作簽名功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • 利用unity代碼C#封裝為dll的步驟分享

    利用unity代碼C#封裝為dll的步驟分享

    這篇文章主要給大家介紹了關(guān)于利用unity代碼C#封裝為dll的相關(guān)資料,文中通過圖文將實現(xiàn)的方法介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-05-05
  • C#使用HttpClient的正確方式你了解嗎

    C#使用HttpClient的正確方式你了解嗎

    在微服務(wù)架構(gòu)體系中經(jīng)常需要向特定?URL?地址發(fā)送?Http?請求操作,在?.net?core?中?httpClient?使用不當(dāng)會造成災(zāi)難性的問題,這篇文章主要來分享?.net?core?中通過?IHttpClientFactory?工廠來使用?HttpClient?的正確打開方式
    2021-11-11
  • C#中Task.ContinueWith連續(xù)任務(wù)使用實例

    C#中Task.ContinueWith連續(xù)任務(wù)使用實例

    本文主要介紹了C#中Task.ContinueWith連續(xù)任務(wù)使用實例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • C#判斷程序是否是管理員權(quán)限運行的方法代碼示例

    C#判斷程序是否是管理員權(quán)限運行的方法代碼示例

    這篇文章主要介紹了C#判斷程序是否是管理員權(quán)限運行的方法代碼示例,本文直接給出實現(xiàn)代碼例子,需要的朋友可以參考下
    2015-03-03
  • C#規(guī)則引擎RulesEngine的具體使用

    C#規(guī)則引擎RulesEngine的具體使用

    這篇文章主要介紹了C#規(guī)則引擎RulesEngine的具體使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02

最新評論