Design Mode in Android-adapter Mode

Source: Internet
Author: User

Design Mode in Android-adapter Mode
Adapter Mode

From the name, we can see that the adapter is a compatible Method for interface mismatch,
Suppose we have an existing class Adaptee, and some of them have existing and implemented Methods methodA. But the customer does not want to call this class, but wants to call a specific interface, such as the Target interface.
So if you want to call Adaptee. the methodA () method creates an Adapter class to implement the Target interface required by the customer. In the Target interface, assume that the customer wants to call the callMethodA () method to implement Adaptee. methodA () method. You can think of the following two methods:

Class adapter Mode

Let the Adapter class implement the Target Interface and inherit the Adaptee class at the same time, so that the Adapter class inherits the Adaptee. methodA () and can call the methodA () method of the parent class in its callMethodA. When you create an Adapter class object, you can call Adapter. methodA () through the Target interface ().

Class Diagram

Code implementation: Adaptee waiting for adaptation
public class Adaptee {    public void methodA(){        System.out.println("I am the methodA in Adaptee");    }}
Target interface that the customer expects
public interface Target {    public void callMethodA();    public void otherMethod();}
Inherited Adaptee and Target interface adapter
public class Adapter extends Adaptee implements Target {    @Override    public void callMethodA() {        super.methodA();    }    @Override    public void otherMethod() {        System.out.println("I am the otherMethod in Adapter");    }}
Customer Test
public class Client {    public static void main(String[] args) {        Adapter mAdapter = new Adapter();        mAdapter.callMethodA();        mAdapter.otherMethod();    }}
Output result
I am the methodA in AdapteeI am the otherMethod in Adapter
Object Adapter Mode

Let the Adapter class hold an instance of the Adaptee class. When the Adapter class implements the methodA () method of the Target Interface, call the methodA () method of the Adaptee instance.

Class Diagram

Code Implementation

Based on the above class Adapter, you only need to modify the Adapter and customer class.

Inherited Adaptee and Target interface adapter
public class Adapter implements Target {    private Adaptee 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 Test
public class Client {    public static void main(String[] args) {        Adaptee mAdaptee=new Adaptee();        Adapter mAdapter = new Adapter(mAdaptee);        mAdapter.callMethodA();        mAdapter.otherMethod();    }}

The output results are the same.

Output result
I am the methodA in AdapteeI am the otherMethod in Adapter
Comparison between two adapters

The two modes have their own advantages and disadvantages. The adapter mode of the class has inherited the Adaptee class, so it cannot inherit other classes. In this case, its disadvantage is that the logic is clear, it is suitable for scenarios where only a single Adaptee is used. The object's adapter mode has no limitations on inheritance. It uses an aggregation method to adapt to the class to be adapted. For individuals, the object-oriented adapter mode is less coupled.

Default adapter Mode

There is also a third mode for the adapter, which looks different from the two above,
When we implement an interface that defines many methods, it is very troublesome to implement so many methods. Second, we don't need some methods. They all implement but confuse the key points.
To solve this problem, an abstract class is used to inherit the interface and an empty implementation is used in the abstract class to implement the methods in the interface. (Not necessarily, in some cases, the abstract class will have the default implementation of methods in the interface, but the subclass that inherits it only needs to complete its unique methods or add them on the basis of the original methods. For example, the PhoneBase class and BaseAdapter class in Android ).

Class chart 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 implements acttarget implements Target {@ Override public void A () {}// you can leave a B method not implemented, B is still abstract, leave it to the subclass to implement @ Override public void C () {System. out. println ("I am C in your acttarget") ;}@ Override public void D () {}@ Override public void E () {}@ Override public void F () {}} public class ConcreteTarget extends acttarget {@ Override public void B () {// B is a required System. out. println ("I am B in ConcreteTarget");} @ Override public void A () {// You can selectively Override Methods A and C System. out. println ("I am A in ConcreteTarget") ;}@ Override public void C () {super. C (); System. out. println ("I am C in ConcreteTarget ");}}
Test
    public static void main(String[] args) {        Target mTarget = new ConcreteTarget();        mTarget.A();        mTarget.B();        mTarget.C();    }
Adapter mode in Android

The most common Adapter mode in Android is the Adapter mode in Android. Its application integrates the default Adapter mode and the object's Adapter mode.
The abstract class BaseAdapter implements the ListAdapter and SpinnerAdapter interfaces, both of which are inherited from the Adapter interface, which define their own methods respectively.

public abstract class BaseAdapter implements ListAdapter, SpinnerAdapter 

However, BaseAdapter does not implement all methods in the interface
SimpleAdapter inherits BaseAdapter and implements all unimplemented interface methods. Some BaseAdapter methods have not been implemented.

public class SimpleAdapter extends BaseAdapter

Here is the default adapter mode above.

What about the Object Adapter,
When we use SimpleAdapter, for example:

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

Let's take a look at the SimpleAdapter constructor.

    public SimpleAdapter(Context context, List
  > data,            int resource, String[] from, int[] to)

Among them, the first parameter is a Map queue, which corresponds to the data of items in each List. The second parameter is the shaping ID of the layout resource of the View that expands the Item. The 4th parameter from array is the Map key of the 2nd parameter, and the last parameter to array is the component in which the data of each key of the from parameter is stored.

When ListView calls onMeasure to determine the component size, it will call mAdapter. getCount () and call the getCount () function of SimpleAdapter:

    public int getCount() {        return mData.size();    }    public Object getItem(int position) {        return mData.get(position);    }

Call the data transmitted by the constructor when SimpleAdapter is created.
When drawing, it will call

mAdapter.getView(position, null, this)

Obtain the View corresponding to the data adapted to the adapter for plotting.

Therefore, SimpleAdapter is the Object Adapter mode of the data object. If the adapter is not used, ListView can also obtain data directly by holding data. However, the essence of using the Adapter in ListView is that you don't need to worry about what the data is. You only need to create different adapters to make different effects and contain ListView of different components.

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.