Exploring the adapter mode of Android Development

Source: Internet
Author: User

Exploring the adapter mode of Android Development
When developing Android, we often connect the data and UI objects through the Adapter. controls such as spinner and ListView can be customized by the Adapter to enrich them. The adapter mode is used to combine two incompatible classes. It is a structural type and requires two identities: Adaptee and Adaptor. To put it simply, you have a robot that can only play football, and another robot can only play basketball. Then you want him to play both football and basketball, the two methods should be combined in his chip, but we do not know how to write the original method of playing football or basketball, and do not want to modify it, that is, the adapter mode is required to mix them together.

The adapter mode is roughly divided into two types: one is a combination of object adapters, the other is an inherited method adapter, and the other is derived from various types.

With MOOC's teaching ppt, let's take a look at what is an Object Adapter

An Object Adapter combines an object into an adapter class to modify the Target Interface package adapter, next we will look at the use of this mode through an example of the conversion current of Two-Phase Plug and three-phase plug.

Let's take a look at the Application Scenario. We have a notebook and a socket.

Our laptop

Public class NoteBook {private ThreePlugIf plug; public NoteBook (ThreePlugIf plug) {this. plug = plug;} // use a socket to charge public void charge () {plug. powerWithThree ();}

Three-phase current plug-in notebook
Public interface ThreePlugIf {// use three-phase current to supply public void powerWithThree ();}
Our socket
Public class GBTwoPlug {// use two-phase current for public void powerWithTwo () {System. out. println ("use two-phase current for power supply ");}}
We found a dilemma in the above situation. We have a notebook that must use three-phase current, but we only have two-phase current sockets. What should we do? 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 ("via conversion"); plug. powerWithTwo ();}}
Our adapter class inherits three socket interfaces, implements its transmission method, and transmits the two-phase current class as a parameter object. How can we use this adapter?
                 GBTwoPlug two =  new GBTwoPlug();ThreePlugIf three = new TwoPlugAdapter(two);NoteBook nb = new NoteBook(three);nb.charge();
The answer is that we use our new subclass (adapter class) to instantiate the parent class (three-phase socket interface). New objects can call the subclass method powerwithThree () to realize power conversion.

So what is a class adapter?

The class adapter mode is a mode that matches the Target Interface by inheriting incompatible interfaces with multiple classes.

Public class TwoPlugAdapterExtends extends GBTwoPlug implements ThreePlugIf {@ Overridepublic void powerWithThree () {System. out. print ("inherit from adapter"); this. powerWithTwo ();}}
After reading the two modes of the adapter, let's compare them.

Class adapters can only be implemented for a single class, with poor reusability.

The Object Adapter can adapt to different objects and is highly useful!

The adapter we use in Android development is a form of deformation. You can read the Android source code study, but it is not necessary.






Related Article

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.