Converting the interface of one class into another that the customer wants makes it possible for classes that would otherwise not work together due to interface incompatibilities to work together. The adapter pattern is divided into class adapters, which are more coupled than object adapters.

  1. Target interface: The interface expected by the current system business. It can be an abstract class or interface.
  2. Adapter class: This is a component interface in an existing component library that is accessed and adapted.
  3. Adapter class: It is a converter that converts the adapter interface into the target interface by inheriting or referencing the adapter’s object, allowing the client to access the adapter in the format of the target interface.

Target interface

public interface Sdcard{
    String readSd(a);
}
Copy the code
public class SdcardImpl implements Sdcard{
    @Override
    public String readTf(a){
        return " read from sdcard ."; }}Copy the code

Matches class

public interface Tfcard{
    String readSd(a);
}
Copy the code
public class TfcardImpl implements Tfcard{
    @Override
    public String readTf(a){
        return " read from tfcard. "; }}Copy the code

Adapter classes

public class SdAdapterTf extends TfcardImpl implements Sdcard{
    @Override
    public String readSd(a){
        returnreadTf(); }}Copy the code