1. Summary of the foregoing

“Make up” ongoing: Design pattern series

2. Adapter mode

2.1 define

Adapter patterns are defined as follows:

Convert the interface of a class into another interface clients expect.Adapterlets classes work together that couldn’t Otherwise because of incompatibleinterfaces. (Convert the interface of a class to another interface that the client expects, so that two classes that would otherwise not work together because of interface mismatches can work together.)

  • Target: The Target role that defines the interface to which other classes are converted, that is, our desired interface.
  • Adaptee: Source role, which is an existing, well-functioning class or object that becomes a new role wrapped by the adapter role.
  • Adapter: Adapter role, core role, the other two roles are already existing roles, and the Adapter role needs to be newly established. Its responsibility is very simple: transform the source role into the target role through inheritance or class association.

Target Role:

public interface Target {
    // Target your own method
    void request(a);
}
Copy the code

Specific target role realization:

public class ConcreteTarget implements Target{
    @Override
    public void request(a) {
        System.out.println("Target method is run!"); }}Copy the code

Source:

public class Adaptee {
    public void doSomething(a){
        System.out.println("Adaptee method doSomething is run!"); }}Copy the code

The adapter:

public class Adapter extends Adaptee implements Target {
    @Override
    public void request(a) {
        super.doSomething(); }}Copy the code

The test class:

public class Test {
    public static void main(String[] args) {
        // The original business logic
        Target target = new ConcreteTarget();
        target.request();
        // Business logic decorated with adapters
        Target target1 = newAdapter(); target1.request(); }}Copy the code

2.2 the advantages

  • The adapter pattern allows two unrelated classes to run together.
  • Increased class transparency: We access the Target role, but the implementation is delegated to the source role, which is transparent to the high-level module and doesn’t need to be concerned about.
  • Improved class reuse: the source character can still be used in the original system, while the target character can also act as a new actor.

Case 3.

The best case of adapter mode is the power supply we often use. The domestic electricity in Our country is 220V AC, while in Japan it is 110V AC. However, we only need 5V to charge our mobile phone, or our laptop computer. They use 19V or they use 24V.

First of all, define an interface to output AC, hoping to output 110V AC, because the domestic production plug is 220V, when we buy directly imported electrical appliances, only 110V AC can be used:

public interface AC {
    int convert110v(a);
}
Copy the code

Two implementations of this AC interface are also included.

Next, create a source class, because it is our domestic plug, can only output 220V AC power at present

public class PowerPlug {
    public void OutPut220V(a) {
        System.out.println("Existing plugs can only output 220V."); }}Copy the code

Then, we install an adapter to convert the voltage of 220V into the voltage of 110V, which can make the electrical appliances we import back to use:

public class Adapter220 extends PowerPlug implements AC {
    @Override
    public void convert110v(a) {
        this.OutPut220V();
        System.out.println("Convert 220V to 110V through adapter."); }}Copy the code

Here we output 110V voltage, but it is still the 220V voltage before the call, we just converted the voltage to 110V at the adapter step.

Finally, there is a test class:

public class Test {
    public static void main(String[] args) {
        AC ac = newAdapter220(); ac.convert110v(); }}Copy the code

Final execution result:

Existing plugs can only output 220V voltage through the adapter, converting 220V to 110VCopy the code

Finally in the test class, although we are a new 220V instance, but when doing method call, directly called the adapter method, which is equivalent to the original output is 220V, but a 220V into 110V shell.


This article is constantly updated, you can search “Geek excavator” on wechat to read it in the first time, and the key words in reply have all kinds of tutorials PREPARED by me, welcome to read.