For Android developers, the adapter mode is just too familiar, however, there are many applications that can be said to use the adapter mode, such as a ListView, directly or indirectly, every day.
The ListView is used to display list data, but as a list data collection there are many forms, there are arrays, there are cursor, we need the corresponding adapter as a bridge, processing the corresponding data (and can form the view required by the ListView).
It is precisely because these adapter interfaces and adapter classes are defined that our data is easily and flexibly displayed on the Adapterview implementation class.
Adapter mode, Adapter pattern, brave to fit, a lot of resources can be reused.
1. Intentions
The adapter mode, which transforms the interface of one class into another interface that the client expects, so that it does not match the two that cannot work together, the class can work together.
The adapter mode is divided into class adapter mode and object adapter mode.
With respect to the class adapter pattern, because Java is single-inheritance, if you inherit a class, the other is only the interface, you need to implement the appropriate method manually.
Hot words: Adapter mode for class Adapter Mode object default adapter mode Source class target interface
2. Structure diagram and code
In order to be concise and straightforward, I omitted the relevant other adapters, taking only these two adapters as an example.
ListViews as the client, he needs the target interface (target interface) is ListAdapter, including GetCount (), GetItem (), GetView () and other basic methods, in order to be compatible with the list <t>,cursor and other data types as data sources, we specifically define two adapters to match them: Arrayadapter and CursorAdapter. These two adapters, to be blunt, are compatible with the data source for the target interface.
This is the adapter mode.
Where Baseadapter implements such as the IsEmpty () method, so that subclasses do not need to implement this method after inheriting Baseadapter, this is the default adapter, which is one of the most obvious benefits of the default adapter.
We use the simplest of several methods as follows, ListAdapter interface as follows (for simplicity, I omitted to inherit from the adapter interface):
- Public interface ListAdapter {
- public int GetCount ();
- Object GetItem (int position);
- long Getitemid (int position);
- View GetView (int position, view Convertview, viewgroup parent);
- boolean isEmpty ();
- }
Abstract class Baseadapter, I omit other code, listing only two methods to make a signal:
- Public abstract class Baseadapter implements ListAdapter, Spinneradapter {
- // ... ...
- Public View Getdropdownview (int position, View Convertview, ViewGroup parent) {
- return GetView (position, convertview, parent);
- }
- Public Boolean isEmpty () {
- return GetCount () = = 0;
- }
- }
Arrayadapter list<t> is encapsulated into a listadapter implementation that satisfies the call of the ListView:
- Public class arrayadapter<t> extends Baseadapter implements filterable {
- private list<t> mobjects;
- //I'm just going to list this one constructor, and you know what that means.
- Public Arrayadapter (context context, int Textviewresourceid, t[] objects) {
- Init (context, Textviewresourceid, 0, Arrays.aslist (objects));
- }
- private void init (context context, int resource, int Textviewresourceid, list<t> objects) {
- Mcontext = context;
- Minflater = (layoutinflater) context.getsystemservice (Context.layout_inflater_service);
- Mresource = Mdropdownresource = resource;
- Mobjects = objects; //Reference object, also expresses the meaning that the combination is better than the inheritance
- Mfieldid = Textviewresourceid;
- }
- public int GetCount () {
- return mobjects.size ();
- }
- Public T GetItem (int position) {
- return Mobjects.get (position);
- }
- Public View GetView (int position, View Convertview, ViewGroup parent) {
- return Createviewfromresource (position, Convertview, parent, mresource);
- }
- // ... ...
- }
We have so successfully list<t> as a data source to the ListView desired target interface to the ListView, the same cursoradapter is the same truth, do not write specific code.
The adapter itself is not difficult, but it provides a idiomatic pattern for troubleshooting incompatibilities.
About when to use the adapter mode, there are about three scenarios:
(1). You want to use an already existing class, and its interface does not meet your needs, this is more common when dealing with legacy systems.
(2). You want to create a reusable class that can work with other unrelated classes or unforeseen fatigue, which is what our Android developers often encounter: We often customize a new adapter.
(3). You want to use some subclasses that already exist, but it is not possible to subclass each to match their interface, and the object adapter can fit his parent interface.
3. Effects
1. Structural mode
2. The above is mainly the object adapter, about the class adapter in addition to the target port, but also to implement the source class you want to be compatible, so you can write fewer lines of code, but from the combination better than the inheritance of the point of view, it is not so clean.
3. Bidirectional or even multi-directional adaptation of the same adapter (i.e. the same object) to the same source makes it suitable for two or more customer calls.
Android design mode Series-Adapter mode