** small C:** small Y, home jack does not have two holes how to do? ** small Y:**so easy, Taobao power conversion plug including shipping only $9.99, really only $9.99. . ** Small C:**iPhone X becomes Lightning connector, will the traditional headset not work? ** Y:** doesn’t have such a great machine. Why do you ask? Buy a transfer cord. ** Little C:** Oh, just ask.

Later, small Y a few months can only eat instant noodles to live, woo woo……

In fact, you should also learn from xiao Y’s spirit of tightening your belt to eat instant noodles in order to make a smile. Without further ado, there are a variety of adapters in real life, extremely convenient, so, the adapter such a job, in our program? The answer is yes, adapter pattern.

1. Basic Concepts

Definition 1.

Transform the interface of one class into another that the client expects, so that two classes that would otherwise not work together because of interface mismatches can work together. That is, define a wrapper class for wrapping objects with incompatible interfaces.

2. Application scenarios
  • When there is an incentive to modify an interface that is already live and does not meet your needs, the adapter pattern may be the best pattern for you.
3. Role introduction

  • Target Target role

    This role defines the interface to which the other classes are converted, which is our desired interface.

  • Adaptee source role

    The source role that needs to be transformed into the target role is an existing, well-functioning class or object that becomes a brand new role wrapped by the adapter role.

  • Adapter Adapter role

    The core role of the adapter pattern, 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.

Second, the implementation of the code

Well, it’s time to let the protagonist iPhone poor conversion line appear (infinite heartbreak…..)

Before you do that, you also need to know that the adapter pattern comes in the form of an adapter pattern for classes and an adapter pattern for objects.

1. UML comparison of two different forms of adaptation modes

The difference between the two is that one is through inheritance, one is through combination, using instances to carry out

2. Code implementation of two different forms of adaptation mode

(1) The same part of the implementation code for both

①iPhone X port (Target port)

Public interface PhoneInterface {/** * Lightning headset */ void lightningHeadSet(); }Copy the code

②Lightning connector headphones

public class LightningHeadSet implements PhoneInterface { @Override public void lightningHeadSet() { System.out.print(" Headphone connection using Lightning port." ); }}Copy the code

③ Traditional headphone jack (source class Adaptee)

Public interface TraditionPhoneInterface {/** * TraditionPhoneInterface */ void traditionphone (); }Copy the code

④ Traditional interface earphones

public class TraditionHeadSet implements TraditionPhoneInterface{ @Override public void traditionHeadSet() { System.out.print(" Headphone connection using a traditional interface." ); }}Copy the code

(2) the implementation of the adapter pattern of class

Create the adapter class (inheriting the traditional headset (source class) and implementing the Linghtning interface (Target))

public class HeadSetAdapter extends TraditionHeadSet implements PhoneInterface{  
	@Override  
	public void lightningHeadSet() {  
    	super.traditionHeadSet();  
	}  
}  
Copy the code

Why do you inherit traditional Headset and implement PhoneInterface? LightningHeadSet () is called because the user needs the Lightning interface to listen to music eventually. However, the function of the adapter is to convert the traditional headset to Lightning, so that the traditional headset can be connected eventually.

(3) Implementation of adaptor pattern for objects (recommended)

public class ObjectHeadSetAdapter implements PhoneInterface { private TraditionPhoneInterface traditionHeadSet; public ObjectHeadSetAdapter(TraditionPhoneInterface traditionHeadSet) { this.traditionHeadSet=traditionHeadSet; } @Override public void lightningHeadSet() { traditionHeadSet.traditionHeadSet(); }}Copy the code

The same reason why you only implement the PhoneInterface interface.

(4) Client output

Public class Client {public static void main(String[] args){// Connect LightningHeadSet via lightning. LightningHeadSet =new LightningHeadSet(); lightningHeadSet.lightningHeadSet(); HeadSetAdapter HeadSetAdapter =new HeadSetAdapter(); headSetAdapter.lightningHeadSet(); TraditionPhoneInterface traditionSet= New TraditionHeadSet(); // Connect TraditionPhoneInterface traditionSet= New TraditionHeadSet(); ObjectHeadSetAdapter objectHeadSetAdapter=new ObjectHeadSetAdapter(traditionSet); objectHeadSetAdapter.lightningHeadSet(); }}Copy the code

The output is:

① Headphone connection with Lightning port. ② Use the headset connection with the traditional interface. ③ Use the headset connection with the traditional interface.Copy the code

3. Object adapter vs class adapter

  • Class Adapter Adapter and Adaptee is the inheritance relationship, because Java is not support multiple inheritance, so it is not suitable for multiple source classes; Instead of using inheritance relationships to connect to an Adaptee class, the adapter pattern of an object uses delegate relationships to connect to an Adaptee class, a method that dynamically combines multiple source classes.
  • Class adaptors inherit from the adapted class, so they are relatively static; The object’s adapter contains the adapted class, so it is relatively flexible.
  • The adapter of the class extends the new requirement through Override; The object’s adapter cannot be extended because of containment relationships.

Three, advantages and disadvantages

1. The advantages and disadvantages of class adapters

(1)

  • Easy to use, code simplification, no need to introduce object instance.

(2) shortcomings

  • High coupling, low flexibility. Using object inheritance is statically defined.

2. The advantages and disadvantages of the object’s adapter

(1)

  • High flexibility, low coupling, using “object combination” mode, is a dynamic combination.

(2) shortcomings

  • The complexity of use requires the introduction of object instances.

3. Advantages and disadvantages of adapters

(1)

  • Improved class reuse. The source role can be used in the original system, and the target role can be used as a new role.
  • Increased class transparency allows clients to invoke the same interface, thus being transparent to clients.
  • Good flexibility. Additions and deletions do not affect the original code.
  • In line with the open and close principle.

(2) shortcomings

  • Too much use of adapters will make the system very messy, not easy to grasp as a whole.

Four,

In the selection of adapter mode according to the need to choose the appropriate implementation mode, as far as possible to use the object adapter mode, more dynamic combination, less inheritance.