Compare the pros and cons of the Java Adapter adapter pattern (class adapter object adapter)

  • 2020-05-24 05:34:40
  • OfStack

The Java adapter pattern

Recently, I have been learning the basic knowledge of java, and I had a lot of doubts when learning the adapter. I searched the information on the Internet. For Adapter, I have a lot of information.

The adapter pattern is to transform the interface of a class into another interface that the client expects, so that two classes that don't match and can't work at 1 can work at 1. Functionally, these interface-incompatible classes generally have the same or similar functionality. Usually we fix this interface incompatibility by modifying the interface of the class, but the adapter pattern comes in handy if we're not willing to change the interfaces for one application, or if we don't have the source code for the object at all.

Advantages of adapters:

1. Decouple the target class from the adapter class

2. It increases the transparency and reusability of the class, encapsulates the concrete implementation in the adapter class, which is transparent to the client class, and improves the reusability of the adapter

3. Flexibility and expansibility are very good, in line with the open and closed principle

The roles involved in adapters include the following:

Target (Target) : defines a specific interface to be used by one client.

Client (Client) : USES the target interface to work with objects corresponding to target interface 1.

Adapter (Adaptee) : an existing interface that needs to be adapted.

Adapter (Adapter) : responsible for converting the Adaptee interface to the Target interface. The adapter is a concrete class, which is the core of the pattern.

Adapters are divided into class adapters and object adapters, which are described in more detail below.

The class adapter

By class adapter, we mean that the adapter Adapter inherits our adapter Adaptee and implements the target interface Target. Since Java is a single inheritance, this adapter can only serve the inherited adapter, Adaptee. The code is as follows:

Apportion (Adaptee)


package com.bluemsun.classadapter;

public class Person {
  private int id;
  private String name;
  /**
   * person Can only speak English now 
   */
  public void sayEnglish(){
    System.out.println("Person can say english!");
  }
  
  /**
   *  omit setter,getter.
   */
}

Target interface (Target)


package com.bluemsun.classadapter;

/**
 *  Goals and objectives person Speak English , French , Japanese . But now person Speak only English 
 * @author Administrator
 *
 */
public interface Target_Person {
  void sayEnglish();
  void sayFrench();
  void sayJapanese();
}

Adapters (Adapter)


package com.bluemsun.classadapter;

/**
 *  The class adapter , Because the inheritance Person, while Java Chinese can only inherit singly , So this adapter is just for person this 1 A kind of service 
 *  This adapter allows person The class implements the methods specified by the target interface without modifying the source code 
 * @author Administrator
 *
 */
public class Adapter_Person extends Person implements Target_Person{

  @Override
  public void sayFrench() {
    System.out.println("Person can say French!");
  }

  @Override
  public void sayJapanese() {
    System.out.println("Person can say Japanese!");
  }
}

Client (Client)


package com.bluemsun.classadapter;

public class Test {
  public static void main(String[] args) {
    Target_Person person = new Adapter_Person();
    
    person.sayEnglish();
    person.sayFrench();
    person.sayJapanese();
  }
}

The simple code above demonstrates what a class adapter can do. As we said at the beginning, this adapter, Adapter, can only serve the 1 class, Person. Now, you might think, well, if I have a lot of classes that I need to fit into, do I have to write Adapter for every class that I need to fit into? Is there a more flexible way? The answer is: yes! This is what we call an object adapter.

Object adapter

An object adapter simply means that the adapter implements our target interface, but does not inherit the classes that need to be adapted. Instead, the adapter is adapted by passing in the classes that need to be adapted in the adapter's constructor. The code is as follows :(Target, Adaptee, ibid.)

Adapters (Adapter)


package com.bluemsun.objectdapter;

import com.bluemsun.classadapter.Person;
import com.bluemsun.classadapter.Target_Person;

/**
 *  Object adapter , Unlike class adapters : Object adapters can adapt multiple sources to targets 
 * @author Administrator
 *
 */
public class Adapter_Person implements Target_Person{  // Just implement the target interface 
  private Person person;
  
  // In the constructor Adaptee class Person Passed in 
  public Adapter_Person(Person person){
    this.person = person;
  }

  // Implement in the target interface sayEnglish()-- call Adaptee In the sayEnglish()
  @Override
  public void sayEnglish() {
    this.person.sayEnglish();
  }

  // Implement other methods in the interface 
  @Override
  public void sayFrench() {
    System.out.println("person can say French!");
  }

  @Override
  public void sayJapanese() {
    System.out.println("person can say Japanese!");
  }
}

Client (Client)


package com.bluemsun.objectdapter;

import com.bluemsun.classadapter.Person;
import com.bluemsun.classadapter.Target_Person;

public class Test {
  public static void main(String[] args) {
    Target_Person person = new Adapter_Person(new Person());
    
    person.sayEnglish();
    person.sayFrench();
    person.sayJapanese();
  }
}

Object adapters can accommodate multiple classes with adapters. All you need to do is pass the different classes in the Adapter constructor. Flexibility.

Advantages of class adapters:

1. Since the adapter class is a subclass of the adapter class, some adapter methods can be replaced in the adapter class to make the adapter more flexible.

Disadvantages of class adapters:

1. For languages that do not support multiple inheritance, such as Java and C#, one adapter class can be adapted at most at a time, and the target abstract class can only be an interface, not a class. Its use has certain limitations, and one adapter class and its subclasses cannot be adapted to the target interface at the same time.

Advantages of object adapters:

1. Adapt multiple different adapters to the same target, that is, the same adapter can adapt both the adapter class and its subclasses to the target interface.

Disadvantages of object adapters:

1. Compared with the class adapter pattern, it is not easy to replace the method of the adapter class.

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: