1.适配器模式的概念
关于适配器的概念,网上已经有很多的解释,即将一个接口转化为另外一个接口。至于为什么要转换,我们可以想象日常生活中常见的两脚插头和三脚插头。现在市场上有同时支持两脚和三脚的插头的适配器,无论是那种插脚,都能够正常通电。根据适配器模式的使用场景,我认为适配器的定义还有一个更重要,聚合不同接口类型的方法,这个在对软件系统进行改造或者对基础设计良好的系统,需要对函数执行流程进行重新整合的时候,适配器模式能够起到聚合的作用。
2.简单的适配器例子
我们有两个类,一个是两脚插头,一个是三脚插头
class DoubleInputInterface
{
public void Connect()
{
Console.WriteLine("Double Input Interface worked.");
}
}
class TrangleInputInterface
{
public void Connect()
{
Console.WriteLine("Trangle Input Interface worked.");
}
}
每个类有个Connect方法,代表连接到电源。
接下来我们有一个抽象类和一个适配器类,适配器类继承抽象类。
class InputInterface
{
public virtual void Connect(InputInterfaceType type)
{
}
}
class InputInterfaceAdapter : InputInterface
{
private readonly DoubleInputInterface doubleInputInterface = new DoubleInputInterface();
private readonly TrangleInputInterface trangleInputInterface = new TrangleInputInterface();
public override void Connect(InputInterfaceType type)
{
switch (type)
{
case InputInterfaceType.Double:
doubleInputInterface.Connect();
break;
case InputInterfaceType.Trangle:
trangleInputInterface.Connect();
break;
default:
throw new Exception("No such input interface type.");
break;
}
}
}
类的代码很简单,就不画UML图了,Adapter类持有两脚插头和三脚插头的两个引用。根据插脚的类型选择执行的方法达到适配的目的。
class Program
{
static void Main(string[] args)
{
InputInterface commonInterface = new InputInterfaceAdapter();
commonInterface.Connect(InputInterfaceType.Double);
commonInterface.Connect(InputInterfaceType.Trangle);
Console.ReadKey();
}
}
< http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+My631s72PC9wPgo8cD7KysXkxvfEo8q9y+TIu7HIvc+6w7XYyMPTw7unsrux2LnY16LKysXkxvfE2rK/tcTKtc/Wz7i92qOstavKx87Sw8e3os/Wo6zI57n7ztLDx9DC1PbBy9K7uPbBrL3Tt73Kvc7Sw8e+zdDo0qrQ3rjEysrF5Mb3wO/D5rXEtPrC66OstNO0+sLryei8xrXEsuO0zsC0y7XKx7K7us/A7bXEo6zEx8O009DDu9PQsOy3qMiluMTJxrT6wuvE2KO/PC9wPgo8cD7K18/Io6zO0sPHytfP4NDo0qq008G9vcWy5c23us3I/b3HsuXNt7PpzOGz9tK7uPa907/ao6i1sci70rK/ydLUysez6c/zwOCjrMbkyrXQp7n7ysfSu9H5tcSjrLK7sdi+wL3h0+u0y6OpoaPIu7rzztLDx7XEQXBhcHRlcr7Nv8nS1NLAwLXT2r3Tv9qjrLb4srvSwMC109q+38zltcTBvb3Fu/LV38j9vcey5c23oaPV4tH5ztLDx9Ta1Pa807LlzbfW1sDgtcTKsbryo6zO0sPHvs2yu9PD0N64xMrKxeTG98HLoaM8L3A+CjxwPsbktM6jrNTatffTw7XEyrG68qOsztLDx8/I08PKtcD9u6/Su7j2yrXA/by0v8mjrNPDu6fQ6NKqwcu94tPQxMTQqbLlzbfW1sDgo6zU2tPDu6fNuMP30NTJz9PQy/nO/sn8o6y1q8rHz7XNs7j8vNO9odezoaM8L3A+CjxwPjwvcD4KPHByZSBjbGFzcz0="brush:java;">using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace AdapterPatternDemo { enum InputInterfaceType { Double, Trangle } interface IInputInterface { void Connect(); } class DoubleInputInterface : IInputInterface { public void Connect() { Console.WriteLine("Double Input Interface worked."); } } class TrangleInputInterface : IInputInterface { public void Connect() { Console.WriteLine("Trangle Input Interface worked."); } } class CommonInputInterface { public virtual void Connect() { } } class InputInterfaceAdapter : CommonInputInterface { private readonly IInputInterface inputInterface; public InputInterfaceAdapter(IInputInterface inputInterface) { this.inputInterface = inputInterface; } public override void Connect() { inputInterface.Connect(); } } class Program { static void Main(string[] args) { IInputInterface inputInterface = new DoubleInputInterface(); CommonInputInterface commonInterface = new InputInterfaceAdapter(inputInterface); commonInterface.Connect(); inputInterface = new TrangleInputInterface(); commonInterface = new InputInterfaceAdapter(inputInterface); commonInterface.Connect(); Console.ReadKey(); } } } 运行结果是一样的:
4.扩展
在开始的时候,我们提到,适配器模式能够组合已有的功能,如果是作为组合功能的使用方式,我们可以改造适配器模式的实现方式。
class InputInterfaceAdapter : InputInterface
{
private readonly DoubleInputInterface doubleInputInterface = new DoubleInputInterface();
private readonly TrangleInputInterface trangleInputInterface = new TrangleInputInterface();
public override void Connect()
{
doubleInputInterface.Connect();
trangleInputInterface.Connect();
}
}
5.总结
设计模式的运用贵乎灵活,见招拆招,每一种设计模式有它的特长和短处,在实际开发过程中,我们只有深刻认识到某种设计模式的长处和短处才能发挥设计模式的作用,让系统更加健壮,扩展性更强。