Introduction to the Adapter pattern of Java design patterns

  • 2020-04-01 03:41:53
  • OfStack

Adapter pattern definition: the use of two incompatible classes intermingled in a structured pattern that requires an Adaptee and an Adaptor(adapter) identity.

Why use the adapter pattern

The first solution is to change the interface of each class, but if we don't have the source code, or if we don't want to change the interface for an application. How to do the & # 63;

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

How to use the adapter pattern

As mentioned in the "class regeneration" section of "think in Java", there are two ways to implement Adapter: composition and inheritance.

Suppose we want to drive piles. There are two kinds: square piles and circular piles.


public class SquarePeg{
 public void insert(String str){
  System.out.println("SquarePeg insert():"+str);
 }
} public class RoundPeg{
 public void insertIntohole(String msg){
  System.out.println("RoundPeg insertIntoHole():"+msg);
    }
}

There is now an application that requires both square and circular piles. Then we need to apply these two unrelated classes together. Assuming that we have no source code for RoundPeg, or that we don't want to modify the source code, then we use Adapter to implement this application:


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

In the above code, RoundPeg is an Adaptee and is the adapter. A PegAdapter is an Adapter that ADAPTS the Adaptee(the Adapter RoundPeg) to the Target(Target SquarePeg). This is actually a combination of composition and inheritance methods.

PegAdapter first inherits from SquarePeg, then generates the object RoundPeg for RoundPeg using a combination of new to generate objects, and then overloads the parent insert() method. From here, you can also see the difference between using new to generate an object and using the extends inheritance to generate an object without modifying the original class or even knowing its internal structure and source code.

If you have any experience with Java, you have found that this pattern is often used.

Further use

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


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

Here are the new RoundPeg and SquarePeg, just like the ones above, except that they implement the interface.


public class SquarePeg implements ISquarePeg{
 public void insert(String str){
  System.out.println("SquarePeg insert():"+str);
 }
} public class RoundPeg implements IRoundPeg{
 public void insertIntohole(String msg){
  System.out.println("RoundPeg insertIntoHole():"+msg);
 }
}

Here is the new PegAdapter, called two-way adapter:


public class PegAdapter implements IRoundPeg,ISquarePeg{
 private RoundPeg roundPeg;
 private SquarePeg squarePeg;  //Constructor
 public PegAdapter(RoundPeg peg){this.roundPeg=peg;}
 //Constructor
 public PegAdapter(SquarePeg peg)(this.squarePeg=peg;)  public void insert(String str){ roundPeg.insertIntoHole(str);}
}

There's also a Pluggable adapter that you can dynamically obtain one of several Adapters. Using Reflection technology, Public methods in classes can be discovered dynamically.


Related articles: