一、认识
适配器模式:Adapter将一个类的接口转换成客户希望的另外一个接口。适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作
适配器模式的两种类型:类适配器模式和对象适配器模式
两个类所做的事情相同或相似,但是具有不同的接口时要使用它
在双方都不太容易修改的时候在使用适配器模式适配
DataAdapter用作DataSet和数据源之间的适配器以便检索和保存数据
DataAdapter通过映射Fill和Update来提供这一适配器
二、类图
Client关联Target:使一个类知道另一个类的属性和方法;Adaper继承Target,它指定了子类如何特化父类的所有特征和行为。子元素共享了父元素的结构和行为,箭头指向父类。Adapter关联Asaptee.
三 、使用场景
Target: 定义客户端使用的特定于域的接口。
Adapter:使接口Adaptee适应目标接口
Adaptee:定义需要调整的现有接口。
Client:与符合Target接口的对象进行协作。
适配器模式主要应用于希望复用一些现存的类,但是接口又与复用环境要求不一致的情况
四、基础代码模板
此结构代码演示了Adapter模式,该模式将一个类的接口映射到另一个类,以便它们可以一起工作。这些不兼容的类可能来自不同的库或框架。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 适配器模式
{
class Program
{
static void Main(string[] args)//对客户端来说,调用的就是Target的Request()
{
Target target = new Adapter();
target.Request();
Console.ReadKey();
}
}
}
//---------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 适配器模式
{
class Adapter :Target //通过在内部包装一个Adaptee对象,把源接口转换成目标接口
{
private Adaptee _adaptee = new Adaptee();//建立一个私有的Adaptee对象
public override void Request()
{
_adaptee.SpecificRequest();//这样就可以把表面上调用Request().方法变成实际调用SpecificRequest
}
}
}
//---------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 适配器模式
{
class Target//这是客户所期待的接口,目标可以是具体的或抽象的类,亦可以是接口
{
public virtual void Request()
{
Console.WriteLine("Called Target Request");//普通请求
}
}
}
//---------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 适配器模式
{
class Adaptee//需要适配的类
{
public void SpecificRequest()
{
Console.WriteLine("Called SpecificRequest()");//特殊请求
}
}
}
五、应用场景代码
using System;
namespace DoFactory.GangOfFour.Adapter.RealWorld
{
/// <summary>
/// MainApp startup class for Real-World
/// Adapter Design Pattern.
/// </summary>
class MainApp
{
/// <summary>
/// Entry point into console application.
/// </summary>
static void Main()
{
// Non-adapted chemical compound
Compound unknown = new Compound("Unknown");
unknown.Display();
// Adapted chemical compounds适应的化合物
Compound water = new RichCompound("Water");
water.Display();
// Wait for user
Console.ReadKey();
}
}
/// <summary>
/// The 'Target' class
/// </summary>
class Compound
{
protected string _chemical;
protected float _boilingPoint;
protected float _meltingPoint;
protected double _molecularWeight;
protected string _molecularFormula;
// Constructor构造函数
public Compound(string chemical)
{
this._chemical = chemical;
}
public virtual void Display()
{
Console.WriteLine("\nCompound: {0} ------ ", _chemical);
}
}
/// <summary>
/// The 'Adapter' class
/// </summary>
class RichCompound : Compound
{
private ChemicalDatabank _bank;
// Constructor构造函数
public RichCompound(string name)
: base(name)
{
}
public override void Display()
{
// The Adaptee适配者
_bank = new ChemicalDatabank();
_boilingPoint = _bank.GetCriticalPoint(_chemical, "B");
_meltingPoint = _bank.GetCriticalPoint(_chemical, "M");
_molecularWeight = _bank.GetMolecularWeight(_chemical);
_molecularFormula = _bank.GetMolecularStructure(_chemical);
base.Display();
Console.WriteLine(" Formula: {0}", _molecularFormula);
Console.WriteLine(" Weight : {0}", _molecularWeight);
Console.WriteLine(" Melting Pt: {0}", _meltingPoint);
Console.WriteLine(" Boiling Pt: {0}", _boilingPoint);
}
}
/// <summary>
/// The 'Adaptee' class
/// </summary>
class ChemicalDatabank
{
// The databank 'legacy API'
public float GetCriticalPoint(string compound, string point)
{
// Melting Point熔点
if (point == "M")
{
switch (compound.ToLower())
{
case "water": return 0.0f;
case "benzene": return 5.5f;
case "ethanol": return -114.1f;
default: return 0f;
}
}
// Boiling Point沸点
else
{
switch (compound.ToLower())
{
case "water": return 100.0f;
//举例
default: return 0f;
}
}
}
public string GetMolecularStructure(string compound)
{
switch (compound.ToLower())
{
case "water": return "H20";
//举例
default: return "";
}
}
public double GetMolecularWeight(string compound)
{
switch (compound.ToLower())
{
case "water": return 18.015;
//举例
default: return 0d;
}
}
}
}