Definition: Turn the two incompatible categories together, belong to the structural mode, need to have two identities of Adaptee and Adaptor (Adapter).
Why? We often encounter to use two categories that don't have a relationship, the first solution is to modify the interface of each category, but if we don't have source code, or, we don't want to modify each other for an application. Interface. How to do?
Use Adapter to create a mixed interface (mixed blood) between the two interfaces.
How to use? Implement the Adapter mode, in fact, "THINK IN JAVA" has been mentioned in the section, there are two ways: Composition and inheritance.
Suppose we have to pile, there are two categories: square pile circular piles. Public class squarepeg {public void insert (String str) {system.out.println ("Squarepeg INSERT ():" STR);}
}
Public class runk {public void insertintohole (string msg) {system.out.println ("Roundpeg InsertIntoHole ():" msg);}}
There is now an application that needs to be played with square piles and a circular pile. Then we need to integrate these two uncustomary categories. Suppose Roundpeg we don't have source code, or source code we don't want to modify, then we use Adapter to implement this application:
Public class pegadapter extends squarepeg {private roundpeg rugg;
Public Pegadapter (this.Roundpeg = peg;)
Public void insert (String str) {roundpeg.insertintohole (str);}
}
In the above code, Roundpeg belongs to Adaptee, which is an adapter. PEGADAPTER is Adapter to adapt Adaptee (Passer Roundpeg) and Target (Target Squarepeg). In fact, this is a combination method and inheritance method.
Pegadapter first inherits Squarepeg, then generates the object mode using NEW, generates Roundpeg's object RoundPEG, and then loads the parent category INSERT () method. From here, you also know how different uses new generation objects and uses Extends inheriting the generation of objects. The former does not need to modify the original category, and even do not need to know their internal structure and source code.
If you have some Java experience, this model has been discovered frequently.
Further use the PEGADAPTER inherits Squarepeg, if we need to inherit both while, inherit Squarepeg and inherit the Roundpeg, because more inheritance is not allowed in Java, but we can implement two interfaces (Interface)
Public interface IROUNDPEG {public void insertintohole (String MSG);
Public interface isquarepeg {public void insert (String Str);
}
Below is a new Roundpeg and Squarepeg, in addition to achieving this difference, and there is nothing difference. Public class squarepeg importpements {system.out.println ("squarepeg insert ():" str);}}}
Public class ruggage {public void insertintohole (string msg) {system.out.println ("Roundpeg INSERTINTOHOLE ():" msg);}}
Here is a new Pegadapter called Two-Way Adapter:
public class PegAdapter implements IRoundPeg, ISquarePeg {private RoundPeg roundPeg; private SquarePeg squarePeg; // Constructors public PegAdapter (RoundPeg peg) {this.roundPeg = peg;} // Constructors public PegAdapter (SquarePeg peg) (this.squarePeg = peg ;)
Public void insert (String str) {roundpeg.insertintohole (str);}
}
There is also a kind of Pluggable Adapters that can be dynamically obtained one of several Adapters. Using Reflection technology, you can dynamically discover the public method in the category.