Android design mode (14)-adapter mode and android Design Mode
Tragedy interview questions:
Briefly Talk About the adapter mode in Android;
Which adapters does the Adapter have?
Draw the class diagram of the Adapter;
You can only know your abilities during the interview;
Only during the interview can we have a clearer understanding of our own shortcomings;
If it is not enough, add it.
Adapter mode:
1. Definition:
Converts an interface to another interface that the customer wants;
2. Purpose:
The Adapter mode allows classes that cannot work together due to interface incompatibility to work together.
3. Usage:
3.1 when the two classes do the same or similar things, but there is no interface for use, when both sides are not easy to modify, use the adapter mode;
3.2. classes already owned by the system, but they do not meet the requirements;
3.3. classes that are not particularly associated with each other need to be operated together;
4. Category:
4.1 class adapter;
4.2 Object Adapter;
The essence of class adapters in java is inheritance and polymorphism;
Class adapters and a single data source are caused by the java single inheritance mode.
The Object Adapter can adapt to multiple data sources to make up for the shortcomings of the class adapter. However, it is necessary to avoid the problem of too many branches and unclear structure due to too many data sources;
5. Advantages:
5.1. good reusability: Better reuse of existing classes without changing the original structure;
5.2. Scalability: the adapter can be further expanded;
6. Disadvantages:
The system logic is disordered. In some cases, the adapter mode is used only when you have to use it. Otherwise, you do not need to adapt the interface if it is designed!
7. Description Object Adapter:
Target Interface:
Package com. example. demo. adapter;/*** Target interface * @ author qubian * @ data June 19, 2015 * @ email naibbian@163.com **/public interface Target {public void function1 (); public void function2 ();}
Existing data classes:
If it is an abstract class and inherits Target, it is a default adapter mode.
Package com. example. demo. adapter;/*** existing class * @ author qubian * @ data June 19, 2015 * @ email naibbian@163.com **/public class BaseAdapter {public void function1 (){}}
Adapter:
Package com. example. demo. adapter;/*** Adapter * @ author qubian * @ data June 19, 2015 * @ email naibbian@163.com **/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 naibbian@163.com **/public class Obj {public void function1 () {} public void function2 () {}} package com. example. demo. adapter. objAdapter;/*** target object * @ author qubian * @ data June 19, 2015 * @ email naibbian@163.com **/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. adapter in Android:
In Android, The ListView we use is an Adapter mode that writes our own Adapter to adapt to the view.
The abstract class BaseAdapter implements the ListAdapter and SpinnerAdapter interfaces, both of which are inherited from the Adapter interfaces.
Adapter class diagram:
Simply put, the adapter we write (MyListAdapter) is compatible with the data source (ListView) of the Target Interface (BaseAdpter). It is also a default adapter mode;