在实际的软件系统设计和开发中,为了完成某项工作需要购买一个第三方的库来加快开发。这带来一个问题,在应用程序中已经设计好的功能接口,与这个第三方提供的接口不一致。为了使得这些接口不兼容的类可以在一起工作,适配器模式提供了一种接口的适配机制。
适配器模式的设计思想在生活中经常会应用到,如我们在给手机充电的时候,不可能直接在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();
}
}
}
</div>
运行输出:
Called SpecificRequest() 请按任意键继续. . .</div>
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();
}
}
</div>
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()");
}
}
}
</div>
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()");
}
}
}
</div>
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();
}
}
}
</div>
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();
}
}
}
</div>
适配器模式实践应用
以手机充电的电源适配器为例,用适配器模式的解决方案。

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();
}
}
}
</div>
运行输出:
手机: 从电源中得到220V的电压 得到手机的充电电压! 请按任意键继续. . .</div>
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();
}
}
</div>
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的电压");
}
}
}
</div>
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("得到手机的充电电压!");
}
}
}
</div>
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();
}
}
}
</div>
适配器模式的优缺点
在引言部分已经提出,适配器模式用来解决现有对象与客户端期待接口不一致的问题,下面详细总结下适配器两种形式的优缺点。
1.类的适配器模式:
优点:
可以在不修改原有代码的基础上来复用现有类,很好

