Android design mode (14)--Adapter mode

Source: Internet
Author: User

Tragedy Interview Questions:

Simply talk about your adapter mode on Android;

What are the adapter of adapter?

Draw a class diagram of adapter;


Only when the interview to know their ability;

Only when the interview can be clearer knowledge of their own shortcomings;

If it's not enough, then fill it up.


Adapter mode:


1. Definition:

Turn an interface into another interface that the customer wants;


2. Purpose:

The adapter mode makes it possible to work with classes that would otherwise not work together because of incompatible interfaces.


3. Use:

3.1, two classes do things the same or similar, but with a non-communication interface when used, when both sides are not easy to modify, the use of adapter mode;

3.2, the system already has the class, but does not meet the needs;

3.3, no special related to each other class needs to complete the operation;


4. Classification:

4.1, class adapter;

4.2, object adapter;

The nature of class adapters in Java is the use of inheritance and polymorphism;

Class adapters are used with a single data source due to the single-inheritance mode of Java

Object adapter can fit multiple data sources to compensate for the shortage of class adapters, but it is necessary to avoid too many branches and unclear structure when the data source is too large.


5. Advantages:

5.1, reusability is good: for the existing classes without changing the original structure of the situation, better reuse;

5.2, the expansion of: The adapter can be better extended;


6. Disadvantages:

Cause system logic confusion, adapter mode in some cases, it is necessary to use the case will be used, otherwise, if the design of the interface does not need to fit!


7. Description Object Adapter:

Target interface:

Package com.example.demo.adapter;/** * Target interface * @author Qubian * @data June 19, 2015 * @email [EMAIL protected] * */public inte Rface Target {public void function1 ();p ublic void function2 ();}

Data classes that already exist:

This is an abstract class, and inheriting target is a default adapter pattern.

Package com.example.demo.adapter;/** * already exists class * @author Qubian * @data June 19, 2015 * @email [EMAIL protected] * */public CL baseadapter {public void function1 () {}}

Adapter:

Package com.example.demo.adapter;/** * Adapter * @author Qubian * @data June 19, 2015 * @email [EMAIL protected] * */public class Adapter extends Baseadapter implements target{@Overridepublic void Function2 () {}}




8. Object adapter:

Target object:

Package com.example.demo.adapter.objadapter;/** * Target Object * @author Qubian * @data June 19, 2015 * @email [EMAIL protected] * */ public class OBJ {public void function1 () {}public void function2 () {}}package com.example.demo.adapter.objadapter;/** *  Target Object * @author Qubian * @data June 19, 2015 * @email [EMAIL protected] * */public class Obj2 {public void Function3 () {}public void Function4 () {}}
Adapter:

Package Com.example.demo.adapter.objadapter;public class Adapter {private OBJ obj;private Obj2 obj2;public Adapter (Obj obj) {this.obj=obj;} public void SetObj2 (Obj2 obj2) {this.obj2 = Obj2;} public void Function1 () {This.obj.function1 ();} public void Function3 () {if (this.obj2!=null) {This.obj2.function3 ();}}}


9, the adapter in Android:

In Android, the ListView that we used to write our own adapter, to fit the view, is an adapter pattern.


Abstract class Baseadapter implements the ListAdapter and Spinneradapter two interfaces, both of which inherit from the Adapter interface.


Class diagram for Adapter:



Simply put: The adapter we write (Mylistadapter) is the right-to-target interface (Baseadpter) to the data source (ListView ) is a default adapter mode;







Android design mode (14)--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.