定义: 将一个借口转换为客户想要的另外一个接口,适配器模式使接口不兼容的那些类可以在一起工作。类的关系: Target: 目标抽象类; 定义客户要用的特定领域接口. Adapter: 适配器(公接口) 调用另外一个接口,作为一个转换器. Adaptee: 适配器(目接口) 定义一个接口,Adapter需要接入. Client: 客户调用类 协同对象符合Adapter适配器(公接口). 优势和缺陷 适配器模式可以将一个类的接口和另外一个类的借口匹配起来,使用的前提是你不能或不想修改的原来的适配 器母接口(adaptee)。例如。你使用第三方控件,类时但没有原程序,这时,使用适配器模式你可以统一对象访问借口。 应用情景; 对象需要利用现存的并且接口不兼容的类. 你需要创建可重用的类以协作其它接口不一定兼容的类. 你需要使用若干个现存的子类但有不想派生这些之类的每个接口. 例题: using System;using System.Collections.Generic;using System.Text; namespace AbstractFactory { //目标抽象类 class ChemicalCompound { protected string name; protected float boilingPoint; protected float meltingPoint; protected double molecularWeight; protected string molecularFormuna; //构造函数 public ChemicalCompound( string name ) { this.name = name; } //属性 public float BoilingPoint { get { return boilingPoint; } } public float MeltingPoint { get { return meltingPoint; } } public double MolecularWeight { get { return molecularWeight; } } public string MolecularFormuna { get { return molecularFormuna; } } } //定义公接口 class Compound : ChemicalCompound { private ChemicalDatabank bank; public Compound( string name ) : base( name ) { //适配器(母接口)-化学数据银行 bank = new ChemicalDatabank(); //调用适配器(母接口)方法 boilingPoint = bank.GetCriticalPoint( name, "B" ); meltingPoint = bank.GetCriticalPoint( name, "M" ); molecularWeight = bank.GetMolecularWeight( name ); molecularFormuna = bank.GetMolecularStructure( name ); } public void Display() { Console.WriteLine( "/n化和物:{0}-----", name ); Console.WriteLine( "分子式:{0}-----", molecularFormuna ); Console.WriteLine( "分子量:{0}-----", molecularWeight ); Console.WriteLine( "熔点:{0}-----", boilingPoint ); Console.WriteLine( "沸点:{0}-----", meltingPoint ); } } //适配器(母接口)-化学数据银行 class ChemicalDatabank { //取得临界点 public float GetCriticalPoint( string compound, string point ) { float temperature = 0.0F; //熔点 if( point == "M" ) { switch( compound.ToLower() ) { case "Water": temperature = 0.0F; break; case "benzene": temperature = 5.5F; break; case "alcohol": temperature = -114.5F; break; } } //沸点 else { switch( compound.ToLower() ) { case "Water": temperature = 100.0F; break; case "benzene": temperature = 80.5F; break; case "alcohol": temperature = 78.5F; break; } } return temperature; } //取得分子式 public string GetMolecularStructure( string compound ) { string structure = ""; switch( compound.ToLower() ) { case "Water": structure = "H2O"; break; case "benzene": structure = "C6H6"; break; case "alcohol": structure = "C2H602"; break; default: break; } return structure; } //取得分子量 public double GetMolecularWeight( string compound ) { double weight = 0.0; switch( compound.ToLower() ) { case "Water": weight = 18.015; break; case "benzene": weight = 78.1134; break; case "alcohol": weight = 46.0688; break; default: break; } return weight; } } public class AdapterApp { public static void Main( string[] args ) { //显示取得水的特性 Compound water = new Compound( "Water" ); water.Display(); //显示取得苯的特性 Compound benzene = new Compound( "benzene" ); benzene.Display(); //显示取得乙醇的特性 Compound alcohol = new Compound( "alcohol" ); alcohol.Display(); Console.ReadLine(); } }}
适配器模式
最新推荐文章于 2022-03-08 14:24:38 发布