SoFunction
Updated on 2025-03-09

Introduction to the adapter mode (Adapter mode) of Java design mode

Adapter mode definition: Use two incompatible classes together, which belongs to the structural mode and requires two identities: Adaptee (adapter) and Adaptor (adaptor).

Why use adapter mode

We often encounter two unrelated classes that need to be combined together. The first solution is: modify the interfaces of each class, but if we do not have source code, or we are unwilling to modify their interfaces for one application. what to do?

Using Adapter, create a hybrid interface (hybrid) between these two interfaces.

How to use Adapter Mode

To implement the Adapter method, in fact, it has been mentioned in the "class regeneration" section of "think in Java", there are two ways: composition and inheritance.

Suppose we want to drive piles, there are two types: square piles and circular piles.

Copy the codeThe code is as follows:

public class SquarePeg{
 public void insert(String str){
("SquarePeg insert():"+str);
 }
}

public class RoundPeg{
 public void insertIntohole(String msg){
("RoundPeg insertIntoHole():"+msg);
    }
}

There is now an application that requires both square piles and circular piles. Then we need to comprehensively apply these two unrelated classes. Assuming that RoundPeg does not have source code, or the source code we do not want to modify, then we use Adapter to implement this application:

Copy the codeThe code is as follows:

public class PegAdapter extends SquarePeg{
 private RoundPeg roundPeg;
 public PegAdapter(RoundPeg peg)(=peg;)
 public void insert(String str){ (str);}
}

In the above code, RoundPeg belongs to Adaptee and is the adapter. PegAdapter is an Adapter, adapting Adaptee (adapter RoundPeg) and Target (target SquarePeg). In fact, this is a comprehensive application of composition and inheritance methods.

PegAdapter first inherits SquarePeg, then uses the combination of new to generate the object method, generates the roundPeg object of RoundPeg, and then overloads the parent class insert() method. From here, you also understand the difference between using new to generate objects and using extends to inherit from objects. The former does not need to modify the original class, or even know its internal structure and source code.

If you have some experience in Java use, you have found that this pattern is often used.

Further use

The above PegAdapter inherits SquarePeg. If we need to inherit from both sides, that is, inherit SquarePeg and inherit RoundPeg, because multiple inheritance is not allowed in Java, but we can implement two interfaces:

Copy the codeThe code is as follows:

public interface IRoundPeg{
 public void insertIntoHole(String msg);
}
public interface ISquarePeg{
 public void insert(String str);
}

Below are the new RoundPeg and SquarePeg. Apart from the difference in implementing interfaces, there is no difference from the above.

Copy the codeThe code is as follows:

public class SquarePeg implements ISquarePeg{
 public void insert(String str){
("SquarePeg insert():"+str);
 }
}

public class RoundPeg implements IRoundPeg{
 public void insertIntohole(String msg){
("RoundPeg insertIntoHole():"+msg);
 }
}

Here is the new PegAdapter called two-way adapter:

Copy the codeThe code is as follows:

public class PegAdapter implements IRoundPeg,ISquarePeg{
 private RoundPeg roundPeg;
 private SquarePeg squarePeg;

// Constructing method
 public PegAdapter(RoundPeg peg){=peg;}
// Constructing method
 public PegAdapter(SquarePeg peg)(=peg;)

 public void insert(String str){ (str);}
}

There is also a type called Pluggable Adapters, which can dynamically obtain one of several adapters. Using Reflection technology, you can dynamically discover Public methods in classes.