The Adapter Pattern of JAVA Design Patterns

1 Overview

Convert the interface of a class to another interface that the client wants. The Adapter pattern enables classes to work together that would otherwise not work together due to incompatible interfaces.

  1. Problem solved

That is, the Adapter pattern enables classes that would otherwise not work together due to incompatible interfaces to work together.

Here are two very vivid examples

  1. Roles in the pattern

3.1 Target interface (Target): The interface expected by the client. Targets can be concrete or abstract classes, or interfaces.

3.2 The class that needs to be adapted (Adaptee): the class or adaptor class that needs to be adapted.

3.3 Adapter: Convert the original interface to the target interface by wrapping an object that needs to be adapted.  

  1. Implementation

(1) [Adapter mode] [Adapter mode] of class (implemented by inheritance)

(2) Object adapter (implemented by object combination)

Class Diagram for Adapter Pattern

one. Adapter pattern for classes

// An existing class with special functionality that does not conform to our existing standard interface 
class  Adaptee  {
     public  void  specificRequest ()  {
        System.out.println( "The adapted class has special functions..." );
    }
}

// target interface, or standard interface 
interface  Target  {
     public  void  request () ;
}

// Concrete target class, only provides common functions 
class  ConcreteTarget  implements  Target  {
     public  void  request ()  {
        System.out.println( "Ordinary classes have common functions..." );
    }
}

// The adapter class inherits the adapted class and implements the standard interface 
class  Adapter  extends  Adaptee  implements  Target {
     public  void  request ()  {
         super .specificRequest();
    }
}

// Test class public class Client { 
    public  static  void  main (String[] args)  {
         // Use normal functional class 
        Target concreteTarget = new ConcreteTarget();
        concreteTarget.request();

        // Use special function class, namely adapter class 
        Target adapter = new Adapter();
        adapter.request();
    }
}

Test Results:

Ordinary classes have ordinary functions...
The adapted class has a special function...

The above-implemented adapter is called a class adapter, because the Adapter class not only inherits Adaptee (adapted class), but also implements the Target interface (because Java does not support multiple inheritance, so it is implemented in this way), in the Client class we can Select and create any subclass that meets your needs to implement specific functions. Another adapter pattern is the object adapter, which does not use multiple inheritance or inheritance and re-implementation, but uses direct association, or delegation. The class diagram is as follows:

code show as below:

// Adapter class, directly associated with the adapted class, while implementing the standard interface 
class  Adapter  implements  Target {
     // Directly associated with the adapted class 
    private Adaptee adaptee;

    // You can pass in the adapted class object that needs to be adapted through the constructor 
    public  Adapter  (Adaptee adaptee)  {
         this .adaptee = adaptee;
    }

    public  void  request ()  {
         // Here is the use of delegation to complete special functions 
        this .adaptee.specificRequest();
    }
}

// Test class 
public  class  Client  {
     public  static  void  main (String[] args)  {
         // Use normal functional class 
        Target concreteTarget = new ConcreteTarget();
        concreteTarget.request();

        // Use a special function class, that is, an adaptation class, 
        // You need to create an object of the adapted class as a parameter 
        Target adapter = new Adapter( new Adaptee());
        adapter.request();
    }
}

The test results are consistent with the above. From the class diagram, we also know that what needs to be modified is only the internal structure of the Adapter class, that is, the Adapter itself must first have an object of the adapted class, and then delegate specific special functions to this object for implementation. Using the object adapter mode, the Adapter class (adaptee class) can achieve the function of adapting multiple different adapted classes according to the incoming Adaptee object. Of course, at this time, we can extract an interface for multiple adapted classes. or abstract class. In this way, it seems that the object adapter pattern is a little more flexible.

  1. Mode Summary

5.1 Advantages

5.1.1 Through the adapter, the client can call the same interface, so it is transparent to the client. Doing so is simpler, more direct, and more compact.

5.1.2 The existing classes are reused, which solves the problem of inconsistent requirements between the existing classes and the reuse environment.

5.1.3 Decouple the target class from the adapter class, and reuse the existing adapter class by introducing an adapter class without modifying the original code.

5.1.4 An object adapter can adapt multiple different adapter classes to the same target, that is, the same adapter can adapt both the adapter class and its subclasses to the target interface.

5.2 Disadvantages

For object adapters, the implementation process of replacing adapters is more complicated.

5.3 Applicable scenarios

5.3.1 The system needs to use existing classes whose interfaces do not conform to the system’s interface.

5.3.2 Want to create a reusable class to work with some classes that are not much related to each other, including some that may be introduced in the future.

5.3.3 When two classes do the same or similar things, but have different interfaces.

5.3.4 The class developed by the old system has already implemented some functions, but the client can only access it in the form of another interface, but we do not want to manually change the original class.

5.3.5 When using third-party components, the component interface definition is different from that defined by yourself. You do not want to modify your own interface, but you need to use the functions of the third-party component interface.

  1. Adapter application example

6.1 Developers who have used ADO.NET should have used DataAdapter, which is used as an adapter between a DataSet and a data source. DataAdapter provides this adapter by mapping Fill and Update.

6.2 Mobile phone power adapter

Author: jason0539

Weibo: http://weibo.com/2553717707

Leave a Comment

Your email address will not be published. Required fields are marked *