One Day Mode - Adapter Mode

1. The concept of adapter mode

Converts one interface into another that the client expects, enabling incompatible classes of the interface to work together, aliased as Wrapper.

2. When to use adapter mode

Common examples are: We use 220V for daily use, and generally 5V for mobile phone charging. It is not possible to charge 220V directly, so we need a transformer (adapter) to change 220V to 5V.

In programming terms, the requirements for the initial phase of the project are:

  • There is a method where the input parameter is a lion object;
  • Print out some features of the lion;

After the program comes online for a period of time, there is a change in demand. This method needs to also accept tiger objects, print the characteristics of tigers, and how to do it without changing the program?

The answer is to use the adapter mode to solve this problem.

Personally, there are two scenarios where the adapter mode is most used:

  • If the project requirements change, do not modify the original code, add adapter to solve the requirements change;
  • If there is no way to modify the source code, add an adapter to solve the problem;

3. How to use adapter mode

3.1 Implementation

Take an example of an actual project scenario:

  • There is a device (TBox) that is installed inside the car before it leaves the factory. It can report the ignition, control and extinction of the car to the service end according to the agreement.
  • The server side has a method called handler, which receives this protocol.

After the program comes online, over a period of time, new requirements need to be met:

  • A new device (OBD) with some steam not installed on the TBox can be purchased to install the OBD device on the car.
  • OBD can report ignition and extinguishing events to the service side, but cannot report remote controlled events.
  • Let the server adapt the OBD protocol without modifying the handler method on the server side;

In this case, you can use the adapter mode, and here is the class diagram and code:

Implementation code:

// Protocol interface for TBox.
public interface TBoxProtocol {

    // ignition
    void engineOn();

    // Extinguish fire
    void engineOff();

    // Remote Control
    void control();

}

// TBox object, implements TBox protocol
public class TBox implements TBoxProtocol {

    // ignition
    public void engineOn() {
        System.out.println("TBox Protocol: Ignition");
    }

    // Extinguish fire
    public void engineOff() {
        System.out.println("TBox Protocol: Fire off");
    }

    // Remote Control
    public void control() {
        System.out.println("TBox Protocol: Remote Control");
    }

}

// Simulate a server that receives the TBox protocol to do some business
public class Server {

    // Ignition first, remote control in progress, extinguishing
    public void handler(TBoxProtocol tBoxProtocol) {
        tBoxProtocol.engineOn();
        tBoxProtocol.control();
        tBoxProtocol.engineOff();
    }

}
// OBD Protocol
public interface OBDProtocol {

    // ignition
    void engineOn();

    // Extinguish fire
    void engineOff();

}

// OBD object, implements OBD protocol
public class OBD implements OBDProtocol {

    // ignition
    public void engineOn() {
        System.out.println("OBD Protocol: Ignition");
    }

    // Extinguish fire
    public void engineOff() {
        System.out.println("OBD Protocol: Fire off");
    }
}
// TBox adapter, adapting OBD to TBox
public class TBoxAdapter implements TBoxProtocol {

    // Inject an OBD protocol
    private OBDProtocol obdProtocol;

    public TBoxAdapter(OBDProtocol obdProtocol) {
        this.obdProtocol = obdProtocol;
    }

    // Call OBD protocol business methods in these scenarios when igniting, extinguishing, remote control
    public void engineOn() {
        obdProtocol.engineOn();
    }

    public void engineOff() {
        obdProtocol.engineOff();
    }

    public void control() {
        // If OBD does not support a method, you can do other things
        System.out.println("OBD Remote control is not supported.");
    }
}
public class Client {

    public static void main(String[] args) {
        // Create a server
        Server server = new Server();

        // Create a TBox protocol and send it to the server
        TBoxProtocol tBoxProtocol = new TBox();
        server.handler(tBoxProtocol);

        System.out.println();

        // TBox
        TBoxAdapter tBoxAdapter = new TBoxAdapter(new OBD());
        server.handler(tBoxAdapter);
    }

}

// input
TBox Protocol: Ignition
TBox Protocol: Remote Control
TBox Protocol: Fire off

OBD Protocol: Ignition
OBD Remote control is not supported.
OBD Protocol: Fire off

Benefits of 3.2 Adapter Mode

The benefits of an adapter are as follows:

  • Open and Close Principle: In response to changes in demand, achieve expected results without modifying existing code, and comply with the open and close principle;
  • Simple and transparent: Clients can call the same interface to complete business, that is, no impact on the client, low coupling;
  • Strong scalability: In the implementation of the adapter function, you can do other business during the call process, which enhances the scalability and flexibility;

3.3 Notes on using adapter mode

Too many adapters can disrupt the overall design of your system.

Therefore, you need to develop good design habits, update documents and legends in time, write simple and understandable, and be consistent with existing code.

4. Summary

The adapter mode approach, if understood from a procedural perspective, can be as follows:

  • Create an adapter to achieve or inherit a goal (inherit the target object if the target does not have an interface, and interface-based programming is strongly recommended to save valuable inheritance opportunities of class);
  • Inject the adapted class into the adapter and pass it parameterically to the adapter;
  • An adapter implements or overrides (corresponding to an implementation interface or inherited class) the target class method, calling within the method the method of the adapted class to implement the business;

From a business perspective, as described in the second subsection:

  • When the requirement changes, it conforms to the open and close principle, does not modify the original code, and joins the adapter to realize the interface adaption, reduces the test scope;
  • In case there is no way to modify the source code, join the adapter to complete the business;

Above is my understanding of the adapter mode, there are some deficiencies, please point out, thank you.

Tags: Programming Mobile Steam

Posted on Sun, 10 May 2020 11:32:28 -0700 by TGLMan