In design mode, the adapter pattern (English: Adapter pattern) is sometimes called a wrapper style or wrapper (wrapper). The interface of a class is forwarded to what the user expects. An adaptation makes it possible to work with classes that cannot work together because of incompatible interfaces, by wrapping the class's own interface in an existing class. ----WIKIPEDIA
Personal understanding
Adapter mode: Two inconsistent or not directly used classes or interfaces are compatible through adapter mode so that they can be used in one piece. Adapter mode in the previous project I was used to deal with the incompatible data, the other side of the data and our data format is not exactly the same, including, the type of inconsistency and inconsistent content, type of inconsistency is easier to solve, but inconsistent content such as: the other side of the data there is an ID corresponding to the two values, In our system, an ID only corresponds to a value, so it is obvious that there is no way to use directly, and the other side of the data to be stored, a certain setting before the data into our database, I was the solution is to put the local ID is set to string type, Then, by id_**, the ID_ identifies which id the value belongs to and the meaning it represents. In order to achieve this, I used the adapter mode, the data passed by the other side of the package, through the adapter to the encapsulated object into the object we can store locally.
The main code is as follows (all the code can be downloaded: design mode code download):
public class Datafrom {private int id;private int x;private int y;}
public class Datainto {private String id;private int num;}
convert from object to multiple into data using adapter mode
public static list<datainto> Gettransferdata (Datafrom datafrom) {if (dataintolist! = null && Dataintolist.size () > 0) dataintolist.clear ();D atainto Dataintoforx = new Datainto ();d Ataintoforx.setid ( Datafrom.getid () + "_x");d Ataintoforx.setnum (Datafrom.getx ());d Ataintolist.add (Dataintoforx);D Atainto Dataintofory = new Datainto ();d Ataintofory.setid (Datafrom.getid () + "_y");d Ataintofory.setnum (Datafrom.gety ()); Dataintolist.add (dataintofory); return dataintolist;}
Refer to the design pattern of Zen This book to look at an example.
The example in the book is this, the author side of the data is a single attribute, but the other side is a number of properties carried out two times the encapsulation, then in this case, the use of adapter mode so that the two can work together without influence.
When I actually write this example, I think this example is not very good, I re-construct this example, so that after the conversion directly to the object to return, the object as a whole to consider the problem.
The class diagram is as follows:
Here the Userinfotransfer class is the intermediate conversion class, and the Userfrom class is the data class of the other. Userinfointo is the form of the data for this party.
Now initialize the portion of the data in each other's class, as shown in the following code:
public class Userinfofrom implements userinfofrominterface{@Overridepublic map<string, string> GetUserInfo () { map<string, string> namemap = new hashmap<string, string> (); Namemap.put ("Wy", "Wangyang"); Namemap.put ("W") , "Wang"); return namemap;} @Overridepublic map<string, string> gethomeaddress () {map<string, string> addressmap = new hashmap< String, string> (); Addressmap.put ("Wy", "SH"); Addressmap.put ("W", "S"); return addressmap;}}
The classes for this party's data are:
public class Userinfointo {private string username;private string address;}
the adapter Mode transformation Class code looks like this:
public class Userinfotransfer extends Userinfofrom implements Userinfointointerface{private map<string, string> Nameinfo = Super.getuserinfo ();p rivate map<string, string> addressinfo = super.gethomeaddress (); @Overridepublic Userinfointo GetUser () {Userinfointo into = new Userinfointo (); Into.setusername (Nameinfo.get ("WY")); Into.setaddress ( Addressinfo.get ("WY")); return into;}}
Benefits of the adapter
1. The adapter mode allows two classes that are not very consistent to run together.
2. More flexible, if one day do not want to use the adapter, delete the adapter can be removed (provided that the class is not called in other classes, or can not be directly deleted). When a business change is found, the data or class of the other party changes, it can be used to achieve compatibility by changing the class of the adapter.
Adapter Mode Considerations
Instead of solving the problem that is being developed, he solves the incompatibility of the project being used.
Source Address: Design mode source code
"Onlookers" design mode (11)--Structural type Adapter mode (Adapter pattern)