Java Adapter mode (Adapter Mode)
Adapter mode definition: Used together two incompatible classes. It is a structural mode and requires two identities: Adaptee and Adaptor. Why do we often encounter the combination of two unrelated classes when using the adapter mode? The first solution is to modify the interfaces of their classes, but if we do not have the source code, or, we do not want to modify interfaces for an application. What should I do?
Use the Adapter to create a Hybrid Interface (mixed blood) between the two interfaces ). How to Use the Adapter mode to implement the Adapter mode, as mentioned in the "think in Java" "class regeneration" section, there are two methods: composition and inheritance ),
Suppose we want to Piling, there are two types: Square Pile round pile. 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 );
}
} Now there is an application that requires both square and circular piles. Then we need to integrate these two unrelated classes. If RoundPeg does not have the source code, or we do not want to modify the source code, we will use the 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 belongs to Adaptee and is an adapter. The PegAdapter is an Adapter that adapts Adaptee (RoundPeg) and Target (Target SquarePeg. In fact, this is a comprehensive application of the composition and inheritance methods.
PegAdapter first inherits SquarePeg, then uses the new combination to generate the RoundPeg object roundPeg, and then reloads the parent insert () method. From here, you also know the differences between generating objects using new and using extends to inherit the generated objects. The former does not need to be modified on the original class, or even has no need to know its internal structure and source code.
If you have some experience using Java, we have found that this mode is often used. We further use the above PegAdapter to inherit SquarePeg. If we need to inherit both sides, that is, inherit SquarePeg and inherit RoundPeg, because many inheritance are not allowed in Java, but we can implement (implements) two interfaces ):
- Public interface IRoundPeg {
Public void insertIntoHole (String msg );
} Public interface ISquarePeg {
Public void insert (String str );
}
The following are the new RoundPeg and SquarePeg. Apart from implementing the interface, there is no difference with the above. 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 );
}
} The following is a 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 is also Pluggable Adapters, which can dynamically obtain one of several adapters. Using Reflection technology, you can dynamically discover Public methods in the class.
Adapter ModeOverviewConverts an interface of a class to another interface that the customer wants. The Adapter mode allows the classes that cannot work together due to incompatibility of interfaces to work together.
Applicability1. You want to use an existing class, and its interface does not meet your needs. 2. You want to create a reusable class that can work collaboratively with other unrelated classes or unforeseen classes (that is, classes that may not be compatible with certain interfaces. 3. (applies only to object adapters) You want to use existing subclasses, but it is impossible to subclass each of them to match their interfaces. The Object Adapter can adapt to its parent class interface.
Participants1. Target defines the interfaces used by the Client in specific fields. 2. Collaboration between the Client and objects that comply with the Target interface. 3. Adaptee defines an existing interface, which must be adapted. 4. the Adapter adapts the Adaptee interface to the Target interface.
Class Diagram ExampleTargetpublic interface Target { void adapteeMethod(); void adapterMethod();}
Adapteepublic class Adaptee { public void adapteeMethod() { System.out.println("Adaptee method!"); }}
Adapterpublic class Adapter implements Target { private Adaptee adaptee; public Adapter(Adaptee adaptee) { this.adaptee = adaptee; }public void adapteeMethod() {adaptee.adapteeMethod();}public void adapterMethod() {System.out.println("Adapter method!"); }}
Clientpublic class Test { public static void main(String[] args) { Target target = new Adapter(new Adaptee()); target.adapteeMethod(); target.adapterMethod(); }}
ResultAdaptee method!Adapter method!