Adapter pattern -- Design Pattern

As the name suggests, adapters have to adapt to different current configurations and solve compatibility problems. Our life is full of all kinds of adapters. The modem used for Internet access is a kind of digital to analog conversion adapter, commonly known as "cat", but now it is all optical cat, that is, the mutual conversion of optical signals and electrical signals. In fact, the reason is the same. There are also various transformers that belong to voltage conversion adapters.

 

 

If you think it's not enough, you can take a look at the electrical appliances at home. For example, your TV has two plugs and the jack on the wall has three jacks. What should you do? Oh, someone said break the plug and insert it forcibly! What if three plugs are connected to two jacks? Pull out the zero line pin! Well, I can only say it's brute force cracking! Violation of design pattern principles. To get back to business, let's not destroy existing classes. What we need is a converter to resolve this incompatibility in an elegant and subtle way.

 

For example, let's start with the code part. First write three Jack interfaces on the wall, named TriplePin:

 

public interface TriplePin {
    //The parameters are live, null and earth
    public void electrify(int l, int n, int e);
}

 

We only define the three jack standard electrify method. The three parameters are live wire, zero wire and ground wire. It's very simple. Similarly, there are two jack interfaces, but without ground wire, named DualPin:

 

public interface DualPin {
    public void electrify(int l, int n);//There is no ground wire here
}

 

Please note that this is not the target interface on our wall, but the two plug standard of TV. OK, go on, our TV is on the stage. It uses two plugs. Of course, it implements DualPin's standard, Let's keep it simple, named TV:

 

public class TV implements DualPin {

    @Override//Since there are two plugs, of course, two plug standards can be realized
    public void electrify(int l, int n) {
        System.out.println("Live wire energization:" + l);
        System.out.println("Zero line energization:" + n);
    }

}

 

Then the problem comes. The interface on the wall is three plug standard, and the TV realizes two plug standard, so it can't be powered on. What should I do? Dismantle the TV and modify it again to realize the three plug standard? The answer is obviously No. since it is the design mode, switch the plug decisively! OK, write an Adapter to solve the irreconcilable contradiction between them.

 

 1public class Adapter implements TriplePin {
 2
 3    private DualPin dualPinDevice; 
 4
 5    //When creating an adapter, you need to plug in the dual plug-in device
 6    public Adapter(DualPin dualPinDevice) {
 7        this.dualPinDevice = dualPinDevice;
 8    }
 9
10    //The adapter implements the target interface
11    @Override
12    public void electrify(int l, int n, int e) {
13        //In fact, the dual plug power of the adapted device is called, and the ground wire e is discarded.
14        dualPinDevice.electrify(l, n);
15    }
16
17}

 

Note that the most critical and essence part is coming. The code in line 3 means that there is a dual plug interface inside the Adapter. It is compatible with any dual plug standard device. OK? I don't understand. Look at your Adapter. The process of completing the code in line 6 is actually that you connect the TV plug to the Adapter. In fact, the Adapter doesn't care what equipment it is. The washing machine and refrigerator can be connected as long as it is a double plug standard (the polymorphic concept mentioned in the first section). The power on method of line 12 realizes the three plug standard, but line 14 in the method is actually supplying power to the double plug of "a certain device" (it depends on what you connect). The parameter of ground wire e is not available, so it is not connected. Is it very clear and thorough?

 

Of course, in addition to the above method of injecting plugs (object adaptation), there is another simpler method called "class adapter". Let's take a look:

 

1public class ClassAdapter extends TV implements TriplePin{
2
3    @Override
4    public void electrify(int l, int n, int e) {
5        super.electrify(l, n);
6    }
7
8}

 

Do you see the difference? The plug (object combination) is not injected here, but the TV is inherited, so that you can directly call the double plug power on of the parent class (TV) instead of injecting it. As you can see, this adapter is inherited for the TV son, and the washing machine can't be used. Die? In fact, it's not completely bad. It depends on the specific application scenario.

 

At this point, our Adapter is almost completed, and there is no need to destroy the plug in the future, because the cost of rewriting the interface or modifying the class is too high. If other classes still depend on it, all of them need to be modified and unnecessary reconstruction is introduced. In short, violent modification violates the basic principle of design pattern. The opening and closing principle refers to opening to extension and closing to modification, that is, do not change the original class, Instead, it expands the existing functions and provides another mechanism for the whole system to realize the desired functions.

 

Tags: Java Spring Design Pattern interface Polymorphism

Posted by villas on Sun, 22 May 2022 13:37:03 +0300