Adapter mode of Android design mode

This example shares the source code of Android adapter mode for your reference. The specific contents are as follows

1. Mode introduction

1.1 definition of mode:

The adapter pattern transforms the interface of a class into another interface expected by the client, so that two classes that could not work together because of interface mismatch can work together.

1.2 usage scenario of mode:

Take the power interface as an example. The power supply of notebook computers generally accepts 5V voltage, but the wire voltage in our life is generally 220V output. At this time, there is a mismatch. In software development, we call it interface incompatibility. At this time, an adapter is needed for interface conversion. In software development, there is a saying that just reflects this point: any problem can be solved by adding an intermediate layer. This layer can be understood as the adapter layer here. Through this layer, an interface conversion can achieve the purpose of compatibility.   

2. Simple implementation of mode

2.1 introduction to simple implementation:

In the above power interface example, 5V voltage is the target interface, 220V voltage is the Adaptee class, and converting the voltage from 220V to 5V is the adapter.

Type 2.2 adapter mode:

The target role gives the required target interface, while the Adaptee class is the object to be converted. The adapter is the interface that converts the volt220 into a target. Correspondingly, the goal of target is to obtain the 5V output voltage, and Adaptee, that is, the normal output voltage is 220V. At this time, we need the power adapter class to convert the 220V voltage into 5V voltage to solve the problem of interface incompatibility.

2.3. Pattern implementation in Android source code

Like the adapter mode of the class, the adapter mode of the object converts the API of the adapted class into the API of the target class. Different from the adapter mode of the class, the adapter mode of the object is not connected to the Adaptee class by inheritance relationship, but connected to the Adaptee class by proxy relationship.

As you can see from Figure 2, the Adaptee class (volt220) does not have a getvolt5 () method, which the client expects. To enable the client to use the Adaptee class, you need to provide a wrapper class adapter. This wrapper class wraps an instance of Adaptee, so that this wrapper class can connect the API of Adaptee with the API of target class. Adapter and Adaptee are delegation relationships, which determines that the adapter pattern is an object.

2.4. Trade off between class adapter and object adapter

*Class adapters use object inheritance, which is a static definition; The object adapter uses object composition, which is a dynamic combination.

*For class adapters, because the adapter directly inherits Adaptee, the adapter cannot work with the subclasses of Adaptee. Because inheritance is a static relationship, it is impossible to deal with the subclasses of Adaptee after the adapter inherits Adaptee. For object adapters, an adapter can adapt multiple different sources to the same target. In other words, the same adapter can adapt both the source class and its subclasses to the target interface. Because the object adapter adopts the relationship of object composition, it doesn't matter whether it is a subclass or not as long as the object type is correct.

*For class adapters, the adapter can redefine part of the behavior of Adaptee, which is equivalent to that the subclass overrides part of the implementation methods of the parent class. For object adapters, it is difficult to redefine the behavior of Adaptee. In this case, you need to define subclasses of Adaptee to achieve redefinition, and then let the adapter combine subclasses. Although it is difficult to redefine the behavior of Adaptee, it is convenient to add some new behaviors, and the newly added behaviors can be applied to all sources at the same time.

*For the class adapter, only one object is introduced, and no additional reference is required to indirectly obtain the Adaptee. For object adapters, additional references are required to indirectly get Adaptee.

It is recommended to use the implementation of object adapter as much as possible, using more composition / aggregation and less inheritance. Of course, specific problems are analyzed, and the implementation method is selected according to the needs. The most suitable is the best.

3. Adapter mode in Android listview

During the development process, the adapter of listview is one of our most common types. The general usage is as follows:

This seems troublesome. Seeing here, we can't help asking why listview uses the adapter mode? We know that, as the most important view, listview needs to be able to display a variety of views. Everyone needs different display effects, and the displayed data types and quantities are also changeable. So how to isolate this change is particularly important.

Android's approach is to add an adapter layer to deal with changes and abstract the interface required by listview into the adapter object. In this way, as long as the user implements the interface of the adapter, listview can display a specific item view according to the display effect, quantity and data set by the user. The listview is informed of the number of data (getcount function) and the type of each data (getitem function) through the proxy data set. The most important thing is to solve the output of item view. Item view is ever-changing, but it is a view type after all. The adapter uniformly outputs item view as a view (getview function), which well deals with the variability of item view.

So how does listview work through the adapter mode (not just the adapter mode)? Let's have a look. Listview inherits from abslistview. The adapter is defined in abslistview. Let's take a look at this class.

Abslistview defines the framework of collection view, such as the application of adapter mode, the logic of reusing item view, the logic of layout item view, etc. Subclasses only need to override specific methods to implement the function of collection view, such as listview.

Related methods in listview.

Listview overrides the layoutchild function in abslistview, in which the item view is laid out according to the layout mode. The number and style of item views are obtained through the method corresponding to the adapter. After obtaining the number and item views, these item views are laid out on the coordinates corresponding to the listview. Coupled with the reuse mechanism of item views, the whole listview basically runs.

Of course, the adapter here is not a classic adapter pattern, but it is an excellent example of the object adapter pattern and well reflects some basic principles of object-oriented. Here, the target role and the adapter role are integrated, and the method in the adapter is the target method; The Adaptee role is the data set of listview and item view, and the adapter proxy data set to obtain the number and elements of the data set.

By adding an adapter layer to abstract the operation of item view, listview and other collection views obtain the number of items, data elements and item views through the adapter object, so as to achieve the effect of adapting various data and various item views. Because item view and data types are ever-changing, Android architects give these changed parts to users to deal with. They abstract them through getcount, getitem, getview and other methods, that is, they give the construction process of item view to users to deal with. They flexibly use the adapter mode to achieve the purpose of unlimited adaptation and embracing change.

4. Miscellaneous talk

Advantages and disadvantages

@H_ 404_ 129 @ advantages

For better reusability, the system needs to use existing classes, and such interfaces do not meet the needs of the system. Then these functions can be reused better through the adapter mode.

Better scalability when implementing the adapter function, you can call the functions developed by yourself, so as to naturally expand the functions of the system.

@H_ 404_ 129 @ disadvantages

Excessive use of adapters will make the system very messy and difficult to grasp as a whole. For example, it is obvious that the a interface is called, but in fact, it is internally adapted to the implementation of B interface. If this happens too often in a system, it is tantamount to a disaster. Therefore, if it is not necessary, you can refactor the system directly instead of using adapters.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>