We often connect data and UI objects through adapter when developing Android, and controls such as spinner and ListView can customize their build with adapters to enrich them. The adapter mode is used to jiuge two incompatible classes, which are structured and require two identities for adaptee (the adapter) and adaptor (adapters). In simple terms, is that you have a robot can only play football, the other robot will play basketball, and then you want to have to let him both play football and play basketball, that his chip to the two methods are together, but we do not know the original play football and play basketball method is how to write, do not want to change Ah, That is, the adapter mode is required to melted them together.
Adapter mode is broadly divided into two kinds, one is the combination of the way the object adapter, the other is the way of inheritance class adapter, and other derivative .
with the teaching ppt, we look at what an object adapter
The object adapter is to combine an "adapter" as an object into the adapter class to modify the target interface wrapper, and let's look at the use of this pattern with a two-phase plug and a three-phase plug conversion current example.
Let's take a look at the usage scenario, we have a notebook and a socket.
The Notebooks we bring
public class NoteBook {private Threeplugif plug;public NoteBook (threeplugif plug) {this.plug = plug;} Charge public void Charge () {plug.powerwiththree () using a socket;}
notebook Three-phase current socket
Public interface Threeplugif {//Use three-phase current supply public void Powerwiththree ();}
The socket we brought.
public class Gbtwoplug {//Using two-phase current supply public void Powerwithtwo () {System.out.println ("power supply using two-phase current");}
The above situation we found a dilemma, we have a notebook must use three-phase current, but we only have two phase current socket, then what to do, then we need a converter.
public class Twoplugadapter implements Threeplugif {private Gbtwoplug plug;public twoplugadapter (gbtwoplug plug) { This.plug = plug;} @Overridepublic void Powerwiththree () {System.out.println ("through conversion");p lug.powerwithtwo ();}}
Our adapter class inherits three socket interfaces, implements its transmission method, and passes the two phase current class as a Parameter object.How do we use this adapter?
Gbtwoplug = new Gbtwoplug (); Threeplugif three = new Twoplugadapter (both); NoteBook NB = new NoteBook (three); Nb.charge ();
The answer is that we instantiate the parent class (the three-phase socket interface) with our new subclass (the adapter Class), and the newly generated object can call the subclass's method Powerwiththree () for power conversion.
So what is a class adapter?
The class adapter pattern is to achieve the matching of the target interface through the multiple inheritance incompatible interface, a single class only to achieve the adaptation of such a pattern.
public class Twoplugadapterextends extends Gbtwoplug implements Threeplugif {@Overridepublic void Powerwiththree () { System.out.print ("with inheritance adapter"); This.powerwithtwo ();}}
After reading the two modes of the adapter, let's compare
Class adapters can only be implemented for a single class, with poor reusability
Object adapters can be adapted for different objects, with a strong usability!
our Android development using adapters is one of their variants, you can read under the Android source code under the research, the million change it