Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

Let’s take an example of life is the problem of electrical outlets. If we travel abroad, the sockets of electrical sockets we use are different. Our plugs cannot be used directly, so we need to use an adapter to do a conversion.

  1. Adapter Pattern converts the interface of a class into another interface table expected by the client. The main purpose is compatibility, so that two classes that cannot work together due to interface mismatch can work together. Its alias is Wrapper

  2. The adapter pattern is a structural pattern

  3. There are three main categories: class adapter pattern, object adapter pattern and interface adapter pattern

Adapter-like pattern

Adapter pattern: Converts an interface from one class to another. Classes that make the original interface incompatible

Compatible from the user’s point of view you can’t see the match, it’s decoupled

The user calls the target interface methods transformed by the adapter, and the adapter calls the corresponding interface methods of the adaptor

The user receives the feedback and feels like he is only interacting with the target interface

We use an example of mobile phone charging to type the code, we have our socket class, and our mobile phone class, but our mobile phone can not be directly used in our socket class, but need an adapter to charge.

Public class Voltage220V {public int output220V() {int SRC = 220; System.out.println(" voltage =" + SRC + "); return src; }}Copy the code

Then we are making an adapter interface

Public interface IVoltage5V {public int output5V(); }Copy the code

Our adapter inherits the interface and implements the adapter functionality

Public class VoltageAdapter extends Voltage220 implements IVoltage5V {@override public int output5V() {// TODO Auto-generated method stub // obtain 220V voltage int srcV = output220V(); int dstV = srcV / 44 ; // return dstV; }}Copy the code

Finally, the Phone class goes to charge

Public void bench (IVoltage5V IVoltage5V) {if(ivoltage5v.output5v () == 5) {public void bench (IVoltage5V IVoltage5V) { System.out.println(" voltage is 5V, can charge ~~"); } else if (iVoltage5V.output5v () > 5) {system.out.println (" voltage > 5V, can't charge ~~"); }}}Copy the code

Client to call

public class Client { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(" === class adapter pattern ===="); Phone phone = new Phone(); phone.charging(new VoltageAdapter()); }}Copy the code
  1. Java is a single-inheritance mechanism, so the fact that the class adapter needs to inherit SRC classes is a disadvantage because it requires DST to be an interface, which has some limitations.

  2. SRC class methods are exposed in Adapter, which increases the cost of using them.

  3. Because it inherits the SRC class, it can override the SRC class methods as needed, making the Adapter more active.

Object adapter pattern

  1. The basic idea is the same as the Adapter pattern of the class, except that the Adapter class is modified. Instead of inheriting the SRC class, the Adapter class holds an instance of the SRC class to resolve compatibility issues. That is: hold SRC class, implement DST class interface, complete SRC -> DST adaptation

  2. According to the principle of composite reuse, association relation is used to replace inheritance relation.

  3. The object adapter pattern is one of the commonly used adapter patterns

Let’s take the example of charging a cell phone

The classes and interfaces that are adapted do not need to be changed.

We modify its adaptation class

Public class VoltageAdapter implements IVoltage5V {private voltage220 voltage220; Public VoltageAdapter(Voltage220V Voltage220V) {this. Voltage220V = Voltage220V; } @Override public int output5V() { int dst = 0; if(null ! = voltage220V) { int src = voltage220V.output220V(); System.out.println(" use the object adapter for adaptation ~~"); dst = src / 44; System.out.println(" adaptation complete, output voltage =" + DST); } return dst; }}Copy the code

Our phone class is unchanged and we call it with new Voltage220V();

  1. Object adapters and class adapters are essentially the same idea, but implemented differently. In accordance with the principle of composite reuse, composition is used instead of inheritance, so it addresses the limitation that class adapters must inherit SRC and no longer require DST to be an interface.
  2. Cheaper to use and more flexible.

Interface adapter

  1. The Default Adapter Pattern or the Default Adapter Pattern.

  2. When it is not necessary to implement all the methods provided by the interface, an abstract class implementation interface can be designed first and provide a default implementation (empty method) for each method of the interface. Then the subclasses of the abstract class can selectively override some methods of the parent class to implement the requirements

  3. This applies when an interface does not want to use all of its methods.

First we create an abstract class

public interface Interface4 {
	public void m1();
	public void m2();
	public void m3();
	public void m4();
}
Copy the code

Create classes to implement interfaces

Public abstract class AbsAdapter implements Interface4 {// Default implements public void m1() { } public void m2() { } public void m3() { } public void m4() { } }Copy the code

Finally we call it using the inner class override method

Public class Client {public static void main(String[] args) {AbsAdapter AbsAdapter = new AbsAdapter() {// Just need to override us @override public void m1() {// TODO auto-generated method stub system.out.println (" m1 "); }}; absAdapter.m1(); }}Copy the code
  1. The three naming methods are based on what form SRC is given to the Adapter.

  2. SRC as an object; SRC as an object; SRC as an object; SRC as an object; SRC as an interface; SRC as an interface

  3. The Adapter mode’s greatest benefit is that it melds otherwise incompatible interfaces together. In real development, implementation is not limited to the three classical forms we explained