适配器模式(Adapter Pattern)属于结构型模式。它将一个接口转换成客户希望的另一个接口,适配器模式使得原本由于接口不兼容的那些类可以一起工作,其别名为包装器(Wrapper)。适配器模式既可以作为类结构型模式,也可以作为对象结构型模式。适配器模式让我们用一个新的适配器类(XxxxAdapter)来解决原本接口不兼容的问题。
可分为两种:
- 类适配器:适配器集成适配者并实现新的接口
- 对象适配器:适配器包含适配者并实现新的接口
比如MacBookPro上面的雷电接口(IThunderboltPort),我们手头有U盘(USBFlashDisk)和SD卡(SDFlashDisk)想插入到MacBookPro拷贝文件。那么就需要对应的适配器(USB2ThunderboltPortAdapter和SD2ThunderboltPortAdapter)。
C#代码(类适配器)
using System;
using System.Collections.Generic;
namespace ConsoleApp2
{
//macbook pro的雷电口
public interface IThunderboltPort
{
bool Support(Type type);
void ThunderboltInsert();
}
//U盘,U盘要插到macbookpro上,要一个USB转Thunderbolt的适配器(usb口要被适配为Thunderbolt口)(ThunderboltPortAdapter)
public class USBFlashDisk
{
public void UsbInsert()
{
Console.WriteLine("USB接口插入");
}
}
public class SDFlashDisk
{
public void SDInsert()
{
Console.WriteLine("SD接口插入");
}
}
/// <summary>
/// 类适配模式1
/// </summary>
public class USB2ThunderboltPortAdapter : USBFlashDisk, IThunderboltPort
{
public bool Support(Type type)
{
return false;
}
public void ThunderboltInsert()
{
base.UsbInsert();
}
}
/// <summary>
/// 类适配模式2
/// </summary>
public class SD2ThunderboltPortAdapter : SDFlashDisk, IThunderboltPort
{
public bool Support(Type type)
{
return false;
}
public void ThunderboltInsert()
{
base.SDInsert();
}
}
/// <summary>
/// 对象适配模式1
/// </summary>
public class USB2ThunderboltPortAdapter2 : IThunderboltPort
{
private USBFlashDisk usbFlashDisk = new USBFlashDisk();
public bool Support(Type type)
{
return type == usbFlashDisk.GetType();
}
public void ThunderboltInsert()
{
usbFlashDisk.UsbInsert();
}
}
/// <summary>
/// 对象适配模式2
/// </summary>
public class SD2ThunderboltPortAdapter2 : IThunderboltPort
{
private SDFlashDisk sdFlashDisk = new SDFlashDisk();
public bool Support(Type type)
{
return type == sdFlashDisk.GetType();
}
public void ThunderboltInsert()
{
sdFlashDisk.SDInsert();
}
}
public class Program
{
static void Main(string[] args)
{
Console.WriteLine("类适配器模式-----------------------------------");
USB2ThunderboltPortAdapter usb2ThunderboltPortAdapter = new USB2ThunderboltPortAdapter();
usb2ThunderboltPortAdapter.ThunderboltInsert();
SD2ThunderboltPortAdapter sd2ThunderboltPortAdapter = new SD2ThunderboltPortAdapter();
sd2ThunderboltPortAdapter.ThunderboltInsert();
Console.WriteLine("对像适配器模式-----------------------------------");
USB2ThunderboltPortAdapter usb2ThunderboltPortAdapter2 = new USB2ThunderboltPortAdapter();
usb2ThunderboltPortAdapter2.ThunderboltInsert();
SD2ThunderboltPortAdapter sd2ThunderboltPortAdapter2 = new SD2ThunderboltPortAdapter();
sd2ThunderboltPortAdapter2.ThunderboltInsert();
Console.WriteLine("对像适配器模式 自动选择-----------------------------------");
Program p = new Program();
IThunderboltPort thunderboltPortAdapter;
thunderboltPortAdapter = p.GetThunderboltPortAdapter(typeof(USBFlashDisk));
thunderboltPortAdapter.ThunderboltInsert();
thunderboltPortAdapter = p.GetThunderboltPortAdapter(typeof(SDFlashDisk));
thunderboltPortAdapter.ThunderboltInsert();
Console.Read();
}
private List<IThunderboltPort> thunderboltPortList = null;
private IThunderboltPort GetThunderboltPortAdapter(Type type)
{
if (thunderboltPortList == null)
{
thunderboltPortList = new List<IThunderboltPort>();
thunderboltPortList.Add(new USB2ThunderboltPortAdapter2());
thunderboltPortList.Add(new SD2ThunderboltPortAdapter2());
}
foreach(var thunderboltPort in thunderboltPortList)
{
if(thunderboltPort.Support(type))
{
return thunderboltPort;
}
}
throw new Exception("error: 找不到合适的适配器");
}
}
}