Developing a bridging model for design pattern learning

This week, learn a design pattern that is easy to learn and difficult to use - bridge pattern, also known as bridge pattern, which is one of the structural design patterns. You can separate the abstract part from the implementation part so that they can change independently.

There are three main application scenarios of bridging mode:

(1) if a system needs to add more flexibility between the abstract drawing role and the concrete role of components, avoid establishing a static inheritance connection between the two levels, but enable them to establish a connection at the abstract level through bridging mode;

(2) for those systems that do not want to use inheritance relationship, or the number of system classes increases sharply due to multi-layer inheritance, the bridging mode can also be considered;

(3) a class has two independently changing dimensions, and both dimensions need to be extended;

The structural design of bridge mode is mainly divided into four parts,

(1) abstract part: this class maintains a reference to the implementation part of the object. The implementation of the method called in the abstract part is realized by calling its method by the implementation part of the object. This class is generally an abstract class;

(2) optimized abstract part (subclass of the abstract part): the concrete implementation of the abstract part. This class generally perfects and extends the methods of the abstract part;

(3) implementation part: it can be an interface or abstract class, and its method does not have to be consistent with that in the abstract part. Generally, the implementation part defines the specific operation method to be used, while the abstract part defines the business method where the execution process of the basic operation method in the implementation part is located;

(4) specific implementation of the implementation part (subclass of the implementation part): improve the specific logic of the method definition in the implementation part;

In the Android source code, there are many cases involving bridging mode. For example, the function division of view layer and drawing layer of view is the embodiment of a wide range of applications. In the use of listview and listadapter, listview, as a subclass of AdapterView, belongs to the abstract part optimized in bridging mode, so AdapterView is regarded as the abstract part, There is a reference to the adapter object in the AdapterView, so the adapter is the implementation part. For listadapter or custom adapter, it is the specific implementation of the implementation part because it inherits from the adapter.

The logic of this part is very simple. In the implementation part, that is, the adapter interface, specific methods for operating data are defined, including well-known methods such as getcount (). The following is the source code

For the specific implementation of the implementation part, the top layer is the adapter that we can customize, which specifically implements getcount () and other methods, which will not be described too much here;

Let's look at the abstract part on the other side, that is, the abstract class of AdapterView. In the abstract part, we define the Adapter object obtained by the subclass, and then we can get the Adapter object through getAdapter (), then call the method to realize the internal business of AdapterView. Some relevant codes are as follows

Finally, let's take a look at the abstract part of the optimization, that is, the relevant contents of listview. It mainly implements the getadapter () and setadatper () methods, and provides setadapter () to set the adapter. The relevant source code of this part is as follows. It should be noted that the direct parent class of listview is abslistview. The listadapter object madapter is defined in abslistview as the default access, and abslistview inherits the AdapterView:

In short, summarize the bridge mode. The implementation part defines the operation method, and the abstract part defines the process method of the operation. Among them, the abstract part holds the object reference of the implementation part. Their implementation classes implement their method logic.

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
分享
二维码
< <上一篇
下一篇>>