Design mode in Android-adapter mode

Source: Internet
Author: User

Adapter mode

From the name you can see that the adapter is for the interface mismatch, and the compatibility method made,
Suppose we have an already existing class adaptee, some of which already exist and implement a good method MethodA. However, the client does not want to call this class, but instead wants to invoke a specific interface such as the target interface.
So if you want to call the Adaptee.methoda () method, create a adapter class to implement the client-requested target interface, the target interface assumes that the client wants to invoke the Callmethoda () method to implement Adaptee.methoda () Method functions. Can think of is the following two ways:

class's Adapter mode

Let the adapter class implement the target interface interface to inherit the Adaptee class, so that the adapter class inherits Adaptee.methoda () and calls the Callmethoda () method of the parent class in its MethodA (). When a client creates a new adapter class object, it can call Adapter.methoda () through the target interface.

Class diagram

Code implementation waits for the adaptee to be adapted
publicclass Adaptee {    publicvoidmethodA(){        System.out.println("I am the methodA in Adaptee");    }}
Client-expected Target interface
publicinterface Target {    publicvoidcallMethodA();    publicvoidotherMethod();}
An adapter that inherits the Adaptee and implements the target interface
publicclass Adapter extends Adaptee implements Target {    @Override    publicvoidcallMethodA() {        super.methodA();    }    @Override    publicvoidotherMethod() {        System.out.println("I am the otherMethod in Adapter");    }}
Customer class Testing
publicclass Client {    publicstaticvoidmain(String[] args) {        new Adapter();        mAdapter.callMethodA();        mAdapter.otherMethod();    }}
Output results
theinthein Adapter
The adapter mode of the object

Let the adapter class hold an instance of the Adaptee class, in which the Adaptee () method of the MethodA instance is invoked when the adapter class implements the MethodA () method of the target interface.

Class diagram

Code implementation

On the basis of the above class adapters, only the adapter and the client classes need to be modified.

An adapter that inherits the Adaptee and implements the target interface
 Public  class Adapter implements Target {    PrivateAdaptee madaptee; Public Adapter(Adaptee madaptee) { This. madaptee = Madaptee; }@Override     Public void Callmethoda() { This. Madaptee.methoda (); }@Override     Public void Othermethod() {System.out.println ("I am the Othermethod in Adapter"); }}
Customer class Testing
publicclass Client {    publicstaticvoidmain(String[] args) {        Adaptee mAdaptee=new Adaptee();        new Adapter(mAdaptee);        mAdapter.callMethodA();        mAdapter.otherMethod();    }}

The output is the same.

Output results
theinthein Adapter
Comparison of the two types of adapters

Both models have pros and cons, the class adapter mode has inherited the Adaptee class, it can no longer inherit other classes, then its drawbacks, but the advantage is the logic is clear, suitable for only a single adaptee situation. The adapter pattern for an object does not have an inherited limit and is aggregated to match the class to be adapted. A personal preference for the adapter mode of the object, this adapter way, the coupling is lower.

Default adapter Mode

The adapter also has a third mode that looks different than the two adapter modes above,
For an interface that defines more methods, it is cumbersome to implement so many methods when we implement the interface. Secondly, there are some ways that we do not need, but we have confused the focus.
To solve this problem, it is to inherit the interface with an abstract class and implement the method in the interface using an empty implementation in the abstract class. (not necessarily, there are situations in which an abstract class has the default implementation of a method in an interface, and a subclass that inherits it only needs to complete some of its unique methods, or increase it on the basis of the original method.) such as the Phonebase class in Android, the Baseadapter class, etc.).

Class Diagram Code implementation
 Public  interface Target {     Public void A(); Public void B(); Public void C(); Public void D(); Public void E(); Public void F();} Public Abstract  class abstracttarget implements Target {    @Override     Public void A() {    }//Can leave a B method not implemented, B or abstract, left to subclass must implement    @Override     Public void C() {System.out.println ("I am C in Abstracttarget"); }@Override     Public void D() {    }@Override     Public void E() {    }@Override     Public void F() {    }} Public  class concretetarget extends abstracttarget {    @Override     Public void B() {//b must be achieved.System.out.println ("I am B in Concretetarget"); }@Override     Public void A() {//selective rewriting of A and C methodsSystem.out.println ("I am A in Concretetarget"); }@Override     Public void C() {Super.        C (); System.out.println ("I am C in Concretetarget"); }}
Test
    publicstaticvoidmain(String[] args) {        new ConcreteTarget();        mTarget.A();        mTarget.B();        mTarget.C();    }
Adapter Mode in Android

The most common adapter mode in Android is adapter in Android, and its application incorporates the default adapter mode and the adapter mode of the object
The abstract class Baseadapter implements the ListAdapter and Spinneradapter two interfaces, both of which are inherited from the adapter interface, each of which defines its own method

publicabstractclass BaseAdapter implements ListAdapter, SpinnerAdapter 

However, all the methods in the interface are not implemented in Baseadapter
Simpleadapter inherits the Baseadapter and implements all the interface methods that are not implemented, and some of the methods that Baseadapter have implemented are not implemented.

publicclass SimpleAdapter extends BaseAdapter

This is the default adapter mode above.

So what about the adapter for the object,
When we use Simpleadapter, for example:

ListView.setAdapter(new SimpleAdapter(this, getData(path),                new"title" },                newint[] { android.R.id.text1 }));

First look at the Simpleadapter constructor

    publicSimpleAdapter(Context context, List<? extends Map<String, ?>> data,            intint[] to)

The 2nd parameter is a map queue, corresponding to the data for each list item, and the 3rd parameter is the shaping ID of the layout resource that expands the view of the item. The 4th parameter from array is the key of map of the 2nd parameter, and the last parameter to array is the component in which the data corresponding to each key of the from parameter is placed.

When the ListView calls Onmeasure to determine the dimension of the component, it calls to Madapter.getcount () and calls the Simpleadapter's GetCount () function:

    publicintgetCount() {        return mData.size();    }    publicgetItem(int position) {        return mData.get(position);    }

Indirect invocation of data that was passed by the constructor when Simpleadapter was created.
At the time of drawing, it is called

nullthis)

Gets the view that corresponds to the data that the adapter is fitting for drawing.

So here Simpleadapter is the object adapter mode for the data object, and if the adapter is not used, the ListView can also get the data by holding the data directly. But the essence of the ListView using adapter is that there is no need to control what the data is, just to create different adapter, you can make different effects, with different components of the ListView.

Design mode in Android-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.