适配器模式

在设计模式中，适配器模式（英语：adapter pattern）有时候也称为包装器（），它将一个类的接口转接成客户所预期的接口。适配器使得因接口不兼容而不能在一起工作的类能在一起工作，做法是将要被适配的已存在的类实例或接口，包裹在预期的目标接口的实现之中。
概述.
适配器模式是著名的《设计模式：可复用物件导向软体的基础》所收录的二十三个模式之一。此书描述了如何解决常见的设计问题，以设计出弹性且异重用的物件导向软件（亦即更容易实现、更改、测试、维护和重用的物件）。
结构.
在上面的UML类图中，codice_1类要求codice_2接口，而不可以直接重复使用codice_3类，因为它的接口不符合codice_2接口。codice_1转而合作于codice_6类，它依据codice_3而实现了codice_2接口：
对象适配器模式.
在这种适配器模式中，适配器容纳一个它包裹的类的实例。在这种情况下，适配器会调用被包裹对象的实例。
类适配器模式.
这种适配器模式使用多个多态接口，实现或者继承它所预期的目标接口，并且继承事先存在的被适配接口。预期的目标接口典型的被创建为纯接口类，特别是在不支持类的多重继承的编程语言比如Java（JDK 1.8以前）之中。
示例.
在下面的对象适配器模式例子中，充电器是客户codice_1，而与它不适配的手机是被适配者codice_3。为了清晰性可以为适配器采用类名字codice_19，比如例子中的codice_20。它应当有一个构造子方法，具有一个codice_3类变量作为参数。这个参数将被传递给适配器的一个实例成员。在调用codice_22的时候，它将访问到这个codice_3，从而允许访问所需求的codice_3的数据，并在这个数据上进行运算而生成想要的输出。
Java.
下面是Java例子：
interface ILightningPhone {
 void recharge();
 void useLightning();
interface IMicroUsbPhone {
 void recharge();
 void useMicroUsb();
class Iphone implements ILightningPhone {
 private boolean connector;
 @Override
 public void useLightning() {
 connector = true;
 System.out.println("Lightning connected");
 @Override
 public void recharge() {
 if (connector) {
 System.out.println("Recharge started");
 System.out.println("Recharge finished");
 } else {
 System.out.println("Connect Lightning first");
class Android implements IMicroUsbPhone {
 private boolean connector;
 @Override
 public void useMicroUsb() {
 connector = true;
 System.out.println("MicroUsb connected");
 @Override
 public void recharge() {
 if (connector) {
 System.out.println("Recharge started");
 System.out.println("Recharge finished");
 } else {
 System.out.println("Connect MicroUsb first");
/* exposing the target interface while wrapping source object */
class LightningToMicroUsbAdapter implements IMicroUsbPhone {
 private final ILightningPhone lightningPhone;
 public LightningToMicroUsbAdapter(ILightningPhone lightningPhone) {
 this.lightningPhone = lightningPhone;
 @Override
 public void useMicroUsb() {
 System.out.println("MicroUsb connected");
 lightningPhone.useLightning();
 @Override
 public void recharge() {
 lightningPhone.recharge();
public class AdapterDemo {
 static void rechargeMicroUsbPhone(IMicroUsbPhone phone) {
 phone.useMicroUsb();
 phone.recharge();
 static void rechargeLightningPhone(ILightningPhone phone) {
 phone.useLightning();
 phone.recharge();
 public static void main(String[] args) {
 Android android = new Android();
 Iphone iPhone = new Iphone();
 System.out.println("Recharging android with MicroUsb");
 rechargeMicroUsbPhone(android);
 System.out.println("Recharging iPhone with Lightning");
 rechargeLightningPhone(iPhone);
 System.out.println("Recharging iPhone with MicroUsb");
 rechargeMicroUsbPhone(new LightningToMicroUsbAdapter (iPhone));
输出： 
Recharging android with MicroUsb
MicroUsb connected
Recharge started
Recharge finished
Recharging iPhone with Lightning
Lightning connected
Recharge started
Recharge finished
Recharging iPhone with MicroUsb
MicroUsb connected
Lightning connected
Recharge started
Recharge finished
C#.
下面是C#例子：
public interface ILightningPhone {
 void ConnectLightning();
 void Recharge();
public interface IUsbPhone {
 void ConnectUsb();
 void Recharge();
public sealed class AndroidPhone : IUsbPhone {
 private bool isConnected;
 public void ConnectUsb() {
 this.isConnected = true;
 Console.WriteLine("Android phone connected.");
 public void Recharge() {
 if (this.isConnected) {
 Console.WriteLine("Android phone recharging.");
 else {
 Console.WriteLine("Connect the USB cable first.");
public sealed class ApplePhone : ILightningPhone {
 private bool isConnected;
 public void ConnectLightning() {
 this.isConnected = true;
 Console.WriteLine("Apple phone connected.");
 public void Recharge() {
 if (this.isConnected) {
 Console.WriteLine("Apple phone recharging.");
 else {
 Console.WriteLine("Connect the Lightning cable first.");
public sealed class LightningToUsbAdapter : IUsbPhone {
 private readonly ILightningPhone lightningPhone;
 private bool isConnected;
 public LightningToUsbAdapter(ILightningPhone lightningPhone) {
 this.lightningPhone = lightningPhone;
 public void ConnectUsb() {
 this.lightningPhone.ConnectLightning();
 public void Recharge() {
 this.lightningPhone.Recharge();
public void Main() {
 ILightningPhone applePhone = new ApplePhone();
 IUsbPhone adapterCable = new LightningToUsbAdapter(applePhone);
 adapterCable.ConnectUsb();
 adapterCable.Recharge();
输出：
Apple phone connected.
Apple phone recharging.
Python.
下面是Python例子：
from abc import ABCMeta, abstractmethod
connected_prompt = "{} connected."
connected_first_prompt = "Connect {} first."
recharge_prompt = ["Recharge started.", "Recharge finished."]
class MicroUsbPhone(metaclass=ABCMeta):
 @abstractmethod
 def recharge(self): pass
 @abstractmethod
 def use_micro_usb(self): pass
class LightningPhone(metaclass=ABCMeta):
 @abstractmethod
 def recharge(self): pass
 @abstractmethod
 def use_lightning(self): pass
class Android(MicroUsbPhone):
 def __init__(self):
 self.connector = "MicroUSB"
 self.connected = False
 def use_micro_usb(self):
 self.connected = True
 print(connected_prompt.format(self.connector))
 def recharge(self):
 if self.connected:
 for state in recharge_prompt:
 print(state)
 else:
 print(connected_first_prompt.format(self.connector))
class IPhone(LightningPhone):
 def __init__(self):
 self.connector = "Lightning"
 self.connected = False
 def use_lightning(self):
 self.connected = True
 print(connected_prompt.format(self.connector))
 def recharge(self):
 if self.connected:
 for state in recharge_prompt:
 print(state)
 else:
 print(connected_first_prompt.format(self.connector))
class LightningToMicroUsbAdapter(MicroUsbPhone):
 def __init__(self, phone):
 assert isinstance(phone, LightningPhone)
 self.connector = "MicroUSB"
 self.adaptee = phone
 def recharge(self):
 self.adaptee.recharge()
 def use_micro_usb(self):
 print(connected_prompt.format(self.connector))
 self.adaptee.use_lightning()
def micro_usb_recharger(phone):
 assert isinstance(phone, MicroUsbPhone)
 phone.use_micro_usb()
 phone.recharge()
def lightning_recharger(phone):
 assert isinstance(phone, LightningPhone)
 phone.use_lightning()
 phone.recharge()
def demo():
 android = Android()
 iphone = IPhone()
 print("Recharging Android with MicroUSB recharger.")
 micro_usb_recharger(android)
 print()
 print("Recharging iPhone with Lightning recharger.")
 lightning_recharger(iphone)
 print()
 print("Recharging iPhone with MicroUSB recharger.")
 micro_usb_recharger(LightningToMicroUsbAdapter(iphone))
其执行：
»&gt; demo()
Recharging Android with MicroUSB recharger.
MicroUSB connected.
Recharge started.
Recharge finished.
Recharging iPhone with Lightning recharger.
Lightning connected.
Recharge started.
Recharge finished.
Recharging iPhone with MicroUSB recharger.
MicroUSB connected.
Lightning connected.
Recharge started.
Recharge finished.