Android design mode--Adapter mode--adapter Getting Started

Source: Internet
Author: User

Today, we're going to comb the adapter pattern for Java design mode.

Adapter, think of our usual use of the power adapter to know, is to put two incompatible things straightened out, can contact each other.

Also in Java, is to let two need to coordinate the class, through the adapter to coordinate the line, that is, the interface of a class to translate into another interface that the customer expects. Adapters allow classes that are incompatible with the original interface to work together.
First we look at two graphs, we usually have such a problem, the vendor interface to me and our existing interface is not up, old data and new data can not be connected and so on.

To solve this problem, without changing the original two interface, we can write a class, to be a middleman, like a translator, I will be the two languages, you talk all through me, this adapter implements you say the desired interface, and this class can also communicate with the manufacturer's interface.

In summary, we can often see that the adapter appears in our code, such as the use of the adapter in Android to achieve view and data contact, such as: ListView and Baseadapter.
let's give a small example to illustrate the adapter pattern.
take a look at our homepage,
our country's standard voltage is 220v, and our notebook needs the voltage is about 15v; so it is certainly not possible to send our AC directly to the notebook, at this time, we have a transformer (power adapter) on it, the 220v voltage to 15v;

Here's a look at the code:

notebook class

power supply 220v, 220v supply voltage

Adapter interface, with a voltage conversion interface

/** * * Power adapter * @author Studyjun * */public interface Voltageadapter {/** * conversion voltage */public int Transf Ormvoltage ();}

Notebook power adapter class, 15v voltage of 220v to notebook

/** *  *  Power adapter  *  @author  studyjun * */public class  myvoltageadapter implements voltageadapter{          private int voltage; //Voltage     /**     *   Standard voltage Equipment 220v     */    private powersupplydevice  powersupplydevice ;               Public myvoltageadapter (Powersupplydevice powersupplydevice)  {         this.powerSupplyDevice = powerSupplyDevice;    }        public int getvoltage ()  {         return voltage;    }       public  void setvoltage (Int voltaGE)  {        this.voltage = voltage;     }         public PowerSupplyDevice  Getpowersupplydevice ()  {        return powerSupplyDevice;     }       public void setpowersupplydevice ( Powersupplydevice powersupplydevice)  {         this.powersupplydevice = powersupplydevice;    }       /**     *  Conversion Voltage      */     Public int transformvoltage () {        int voltage  = powersupplydevice.powersupply ();         int lowervoltage  = voltage/14;    &nBSP;   SYSTEM.OUT.PRINTLN ("voltage in conversion =" +lowervoltage+ "V");         return lowerVoltage;              }}

test

public class test {          public static void main (String[] args)  {                  jotter jt  = new jotter ();        powersupplydevice  Powersupplydevice = new powersupplydevice ();         Voltageadapter adapter=new myvoltageadapter (Powersupplydevice);         jt.setadapter (adapter);         jt.inputvoltage ();     }     } 

Execution results

Here, let's summarize the advantages and disadvantages of the adapter pattern:
Advantages:
1, two different interfaces can be linked together, low coupling.
2, increase the transparency and reusability of the class, the specific implementation encapsulated in the adapter class, for the client class is transparent, and improve the reusability of the appropriate ligand
3, flexibility and extensibility are very good, in line with the opening and shutting principle

Disadvantages of class Adapters:
1, for Java, C # and other languages do not support multiple inheritance, a maximum of one adapter class at a time, and the target abstract class can only be an interface, can not be a class, its use has certain limitations, can not be an adapter class and his sub-class at the same time to adapt to the target interface.
The object adapter has some advantages:
2, compared with the class adapter mode, it is not easy to replace the method of the matching class.

Source code Download: Designpattern

Android design mode--Adapter mode--adapter Getting Started

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.