Adapter Design Pattern (Adapter mode)

Source: Internet
Author: User

Adapter mode is a very common pattern in software development, and the function of this mode is to enable two incompatible interfaces to work together. Most of the time we are using this pattern consciously or unconsciously.

Because two interfaces are not compatible, the task of the adapter is to adapt the data or objects so that the calling interface can use the results produced by the other interface. The adapter is the object in which the adaptation works.

Many design pattern textbooks like to use the reader for example: mobile phone users commonly used TF card is not plugged into the computer to work, but need to plug in the card reader, and then connect to the computer's USB port, so that the data processing work; The same example also has a power converter, the U.S. voltage is generally 110V, The mainland of China is 220V, if the United States to plug the standard electrical appliances directly into the mainland to use, it is bound to appear the result of high voltage burned, this time need a voltage converter, 220V voltage conversion to 110V for use. These are all examples of adaptation modes and adapters.

This article with a simple example, there is a way to generate a user list, there are three ways to print user information to be able to call this interface, but none of the three methods can accept the table-like data, you need an adapter to adapt the data type, passed to the method used, the code is as follows:

Package Dp.adapter;import Java.util.arraylist;import Java.util.list;public Class usersmanager{/* * This method can only generate a list of user names, Such a data structure cannot be accepted by another three methods (these three call methods need, array, Vector, string separated by a vertical line) */public list<string> getusersnameslist () {list< string> usersnameslist = new arraylist<string> () Usersnameslist.add ("Jerry"); Usersnameslist.add ("Rich"); Usersnameslist.add ("John"), Usersnameslist.add ("Paul"), Usersnameslist.add ("Mark"), Usersnameslist.add ("Tom"); return usersnameslist;}}



Package Dp.adapter;import java.util.vector;/* * Adapter Interface */public Interface Usersmanageradapter{public string[] Getusersnamesarray ();p ublic vector<string> getusersnamesvector ();p ublic String getusersnamesseperatedbyline ( );}



Package Dp.adapter;import Java.util.list;import java.util.vector;/* * adapter is able to fit the caller with the appropriate data structure */public class Usersmanageradapterimpl implements Usersmanageradapter {Usersmanager Usersmanager = new Usersmanager (); @ Overridepublic string[] Getusersnamesarray () {list<string> List = Usersmanager.getusersnameslist (); String names[] = null;if (list!=null && list.size () >0) {names = new String[list.size ()];for (int i=0;i< List.size (); i++) {Names[i] = List.get (i);}} return names;} @Overridepublic vector<string> getusersnamesvector () {vector<string> usersvector = new vector<> (); list<string> list = Usersmanager.getusersnameslist (); if (list! = null && list.size () > 0) {for (int i = 0 ; I < list.size (); i++) {Usersvector.add (List.get (i));}} return usersvector;} @Overridepublic String getusersnamesseperatedbyline () {list<string> List = Usersmanager.getusersnameslist (); StringBuffer usersnamesstring = new StringBuffer (); if (list! = null && list.Size () > 0) {for (int i = 0; i < list.size (); i++) {Usersnamesstring.append (List.get (i)). Append ("|");} return usersnamesstring.tostring ();}}



Package Dp.adapter;import Java.util.vector;public class Main {public static void main (String args[]) { Usersmanageradapter usersmanageradapter = new Usersmanageradapterimpl (); Main main = new main (); System.out.println ("-----------------------"); Main.usersnamesarrayprinter ( Usersmanageradapter.getusersnamesarray ()); System.out.println ("-----------------------"); Main.usersvectorprinter (Usersmanageradapter.getusersnamesvector ( )); System.out.println ("-----------------------"); Main.usersstringprinter ( Usersmanageradapter.getusersnamesseperatedbyline ());} public void Usersnamesarrayprinter (String namesarrays[]) {if (namesarrays! = null) {for (int i = 0; i < Namesarrays.len Gth i++) {System.out.println (namesarrays[i]);}} public void Usersvectorprinter (vector<string> namesvector) {if (namesvector! = null) {for (int i = 0; i < namesve Ctor.size (); i++) {System.out.println (Namesvector.get (i))}}} public void Usersstringprinter (String usersnames) {if (usersnames! = null) {SYSTEM.OUT.PRINTLN (USersnames);}}} 



Operation Result:

-----------------------jerryrichjohnpaulmarktom-----------------------jerryrichjohnpaulmarktom-----------------------Jerr y| rich| john| paul| mark| Tom|



From the running results visible, three methods can call the adapter to convert the data and then perform normal printing.

Summary: Adapter mode in the daily development, especially the code refactoring process is very common, many times we are consciously and unconsciously in the use of this mode, after all, many old interfaces and methods returned by the data does not adapt to the new interface calls, taste the need to fit the data.

Adapter Design Pattern (Adapter mode)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.