The Adapter Pattern in Java – Java中的适配器模式

最后修改: 2019年 2月 24日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this quick tutorial, we’ll have a look at the Adapter pattern and its Java implementation.

在这个快速教程中,我们将看一下适配器模式及其Java实现。

2. Adapter Pattern

2.适配器模式

An Adapter pattern acts as a connector between two incompatible interfaces that otherwise cannot be connected directly. An Adapter wraps an existing class with a new interface so that it becomes compatible with the client’s interface.

适配器模式作为两个不兼容的接口之间的连接器,否则无法直接连接。适配器用一个新的接口来包装一个现有的类,使其与客户的接口兼容。

The main motive behind using this pattern is to convert an existing interface into another interface that the client expects. It’s usually implemented once the application is designed.

使用这种模式的主要动机是将现有的接口转换为客户所期望的另一个接口。它通常在应用程序设计完成后实现。

2.1. Adapter Pattern Example

2.1.适配器模式实例

Consider a scenario in which there is an app that’s developed in the US which returns the top speed of luxury cars in miles per hour (MPH). Now we need to use the same app for our client in the UK that wants the same results but in kilometers per hour (km/h).

考虑这样一种情况:有一个在美国开发的应用程序,以每小时英里(MPH)为单位返回豪华汽车的最高速度。现在,我们需要为我们在英国的客户使用相同的应用程序,他们希望得到相同的结果,但以每小时公里数(km/h)为单位。

To deal with this problem, we’ll create an adapter which will convert the values and give us the desired results:

为了处理这个问题,我们将创建一个适配器,它将转换这些值并给我们提供所需的结果。

Rpt ER5p

First, we’ll create the original interface Movable which is supposed to return the speed of some luxury cars in miles per hour:

首先,我们将创建原始接口Movable,它应该是返回一些豪华汽车的速度,单位是每小时英里。

public interface Movable {
    // returns speed in MPH 
    double getSpeed();
}

We’ll now create one concrete implementation of this interface:

我们现在要创建这个接口的一个具体实现。

public class BugattiVeyron implements Movable {
 
    @Override
    public double getSpeed() {
        return 268;
    }
}

Now we’ll create an adapter interface MovableAdapter that will be based on the same Movable class. It may be slightly modified to yield different results in different scenarios:

现在我们将创建一个适配器接口MovableAdapter,它将基于同一个Movable类。它可以稍作修改,以便在不同的情况下产生不同的结果。

public interface MovableAdapter {
    // returns speed in KM/H 
    double getSpeed();
}

The implementation of this interface will consist of private method convertMPHtoKMPH() that will be used for the conversion:

这个接口的实现将包括私人方法convertMPHtoKMPH(),该方法将被用于转换。

public class MovableAdapterImpl implements MovableAdapter {
    private Movable luxuryCars;
    
    // standard constructors

    @Override
    public double getSpeed() {
        return convertMPHtoKMPH(luxuryCars.getSpeed());
    }
    
    private double convertMPHtoKMPH(double mph) {
        return mph * 1.60934;
    }
}

Now we’ll only use the methods defined in our Adapter, and we’ll get the converted speeds. In this case, the following assertion will be true:

现在,我们将只使用我们的Adapter中定义的方法,我们将得到转换后的速度。在这种情况下,下面的断言将是真的。

@Test
public void whenConvertingMPHToKMPH_thenSuccessfullyConverted() {
    Movable bugattiVeyron = new BugattiVeyron();
    MovableAdapter bugattiVeyronAdapter = new MovableAdapterImpl(bugattiVeyron);
 
    assertEquals(bugattiVeyronAdapter.getSpeed(), 431.30312, 0.00001);
}

As we can notice here, our adapter converts 268 mph to 431 km/h for this particular case.

正如我们在这里所注意到的,我们的适配器将268 mph转换为431 km/h的这种特殊情况。

2.2. When to Use Adapter Pattern

2.2.何时使用适配器模式

  • When an outside component provides captivating functionality that we’d like to reuse, but it’s incompatible with our current application. A suitable Adapter can be developed to make them compatible with each other
  • When our application is not compatible with the interface that our client is expecting
  • When we want to reuse legacy code in our application without making any modification in the original code

3. Conclusion

3.总结

In this article, we had a look at the Adapter design pattern in Java.

在这篇文章中,我们看了一下Java中的适配器设计模式。

The full source code for this example is available over on GitHub.

这个例子的完整源代码可以在GitHub上找到