Defined:
The use of two incompatible classes jiuge in a structured pattern requires two identities for adaptee (the adapter) and adaptor (adapters).
why use?
We often encounter the need to combine two classes that are not related to each other, the first solution is to modify the interfaces of the respective classes, but if we do not have the source code, or we do not want to modify the respective interfaces for an application.
What to do?
Using adapter, create a hybrid interface (half-breed) between the two interfaces.
How do I use it?
Implementation of the adapter way, in fact, "think in Java," The "Class regeneration" section has been mentioned, there are two ways: combination (composition) and Inheritance (inheritance).
Suppose we want to pile, there are two kinds: square pile round pile.
Public class squarepeg{ publicvoid Insert (String str) {System.out.println ("Squarepeg insert ( ): "+str"; }}publicclass roundpeg{ publicvoid insertintohole ( String msg) {System.out.println ("Roundpeg insertintohole ():" +msg);} }
Now there is an application that requires both square piles and round piles. Then we need to combine the two non-relational classes. 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 roundpeg; public pegadapter (roundpeg peg) (this . Roundpeg=peg;) public void insert (String str) {roundpeg.insertintohole (str);}}
In the code above, Roundpeg belongs to Adaptee and is the adapter. The Pegadapter is adapter, adapting Adaptee (the adapter roundpeg) and Target (squarepeg). This is actually the combination method (composition) and Inheritance (inheritance) Methods are used comprehensively.
Pegadapter first Inherits Squarepeg, then uses the combination of new to generate the object, generates Roundpeg object Roundpeg, and then overloads the parent class insert () method. From here, you also understand that using new to build objects differs from building objects using extends inheritance, which does not need to be modified for the original class, or even need to know its internal structure and source code.
If you have some experience with Java use, it has been found that this pattern is often used.
Further use
the above pegadapter is inherited squarepeg, if we need to inherit from both sides, that is, inherit Squarepeg and inherit Roundpeg, because Java does not allow multiple inheritance, but we can implement (implements) Two interfaces (interface)
Public Interface iroundpeg{ publicvoid insertintohole (String msg); Public Interface isquarepeg{ publicvoid Insert (String str);}
The following is the new Roundpeg and Squarepeg, in addition to implementing the interface of the difference, and the above is no different.
Public class Implements isquarepeg{ publicvoid Insert (String str) {System.out.println ("squarepeg insert (): "+str"; }}publicclassimplements iroundpeg{ publicvoid insertintohole (String msg) {System.out.println ("Roundpeg insertintohole ():" +msg); }}
Here is the new pegadapter, called Two-way adapter:
Public classPegadapterImplementsiroundpeg,isquarepeg{PrivateRoundpeg Roundpeg; PrivateSquarepeg Squarepeg; //Construction Method PublicPegadapter (Roundpeg peg) { This. roundpeg=peg;} //Construction Method PublicPegadapter (Squarepeg Peg) ( This. squarepeg=peg;) Public voidInsert (String str) {roundpeg.insertintohole (str);}}
There is also a kind of called pluggable adapters, can dynamically get a few adapters in one. Using reflection technology, you can dynamically discover public methods in a class.
Transferred from: http://www.jdon.com Banqiao
Design mode Adapter (adapter) (RPM)