Detailed explanation of intermediary mode of Android programming design mode

This paper illustrates the mediator mode of Android programming design mode. Share with you for your reference, as follows:

1、 Introduction

Mediator pattern is also called mediator pattern or mediator pattern. Mediator itself means mediator and mediator.

In our daily life, the role of mediators or mediators is much more than that of "peacemakers", that is to say, to mediate the role of two disputable persons. For example, parents and children quarrel with each other, and parents or children will come to the point of trying to persuade or prevent quarrels. The children or parents here are acting as mediators.

What about intermediaries? What you usually hear most is real estate agents. Today, with the rapid development of real estate, all kinds of real estate intermediary companies have sprung up. The responsibility of real estate agents is very simple, that is, to establish a bridge between buyers and sellers to communicate the two. For example, Xiaomin now has a house to sell, So he went to the real estate agent and told the real estate agent the expected selling price of the house and the relevant information of the house. Then the real estate agent collected and summarized these information, sometimes beautified it, and then hung the advertisement in the window waiting for the buyer. If someone wants to buy the house, take Xiaohui as an example, The real estate agent will tell Xiaohui about the relevant information of the house, such as the age and price of the house, and sometimes take Xiaohui to actually visit the house. Finally, Xiaohui will tell the real estate agent about his wishes, and then the real estate agent will communicate with Xiaomin. In other words, in the whole process of real estate negotiation, Xiaomin and Xiaohui buyers and sellers rarely have direct contact. In most cases, they convey information through real estate agents. Maybe Xiaomin and Xiaohui will officially meet only when the final real estate handover is carried out.

This is the common mediator and mediator in our real life. However, the mediator model we want to talk about here is much more complex than the above two examples. In the above examples, the mediator or mediator needs to coordinate only two types of objects, and the mediator model needs to coordinate multiple types of objects.

Here is a small example to illustrate. Most readers of this book are it practitioners. To put it more accurately, they are app makers. Generally speaking, an app maker can be divided into several modules: operation and maintenance, product, development, design and testing. Whenever a new app starts to be produced or a new version needs to be produced, the company will hold a seminar, The operation and maintenance department is responsible for the market to understand the user's use tendency. The R & D department will say that the user reflects that the interface is not good-looking and requires to modify the interface... In this way, the discussion between you and me for half a day has no substantive results. The main reason is that everyone considers problems from their own position. Even if such a team seminar continues, it will be meaningless, At this time, if someone who does not belong to either party of operation and maintenance, product, development or design stands up and tells me their opinions, I will weigh them and make the final decision. No matter what the decision is, you should follow it. Because this person does not represent any party, everyone unanimously affirmed that such a plan will be decided soon, and the person who makes the final decision is called the intermediary. The intermediary here faces many kinds of objects like our intermediary model. In fact, you can see some characteristics of the intermediary model, Every intermediary will know all his colleagues, such as operation and maintenance, product, development, design and testing, but these colleagues can not know each other, and the intermediary does not belong to or favor any of his colleagues. I believe everyone will have a very clear understanding of the intermediary model.

2、 Definition

The mediator pattern wraps the way a series of objects interact so that they do not have to interact significantly. So that they can be loosely coupled. When the interaction between some objects changes, it will not immediately affect the interaction between other objects. Ensure that these functions can change independently of each other. The mediator model transforms many to many interactions into one to many interactions. The mediator model abstracts the behavior and cooperation of objects, and processes the interaction between objects and other objects in small-scale behavior separately.

3、 Usage scenario

When there are many interactions between objects and the behavior operations of each object depend on each other, in order to prevent modifying the behavior of one object and modifying the behavior of many other objects, the mediator mode can be used to solve the tight coupling problem. This model changes the many to many relationship between objects into one to many relationship, and the mediator object changes the system from a mesh structure to a star structure centered on the mediator, so as to reduce the complexity of the system and improve the scalability.

4、 UML class diagram of mediator pattern

UML class diagram:

Role introduction:

Mediator: Abstract mediator role, which defines the interface from colleague object to mediator.

Concretemediator: a specific mediator role that receives messages from specific colleague objects and sends commands to specific colleague objects at the same time.

Collague: an abstract colleague role, which defines the interface of the mediator object. It only knows the mediation and does not know other colleague objects.

Concretecolleaguea, concretecolleagueb: specific colleague roles. Each specific colleague class knows its behavior in a small range, but does not know its behavior in a large range.

Template code:

Abstract mediator:

Specific Intermediaries:

Abstract colleagues:

Specific colleague a:

Specific colleague B:

5、 Simple example

In the computer, the host part is mainly divided into CPU, memory, graphics card and IO device, and the motherboard is the one that integrates them. Here, the motherboard is an intermediary. Take this as an example.

Abstract mediator:

Abstract colleagues:

Colleagues:

Optical drive colleague:

Graphics card colleague:

Sound card colleague:

Main board intermediary:

Play movie:

result:

It can be seen that the mediator model transforms the many to many interaction into one to many interaction, and changes the system from a mesh structure to a star structure centered on the mediator (here is the main board), so as to reduce the complexity of the system and improve the scalability.

6、 Mediator mode in Android source code

1. Keyguard unlock screen

Detailed mechanism reference: Android 4.0 keyguard unlocking screen mechanism

7、 Summary

In fact, in Android development, we may inadvertently use the mediator mode, such as the login registration interface. We use the addtextchangedlistener of EditText to monitor the number of digits of the input password, whether the user name is empty, and whether the password is consistent with the confirmation password. At this time, when multiple controls interact, the activity acts as the mediator to coordinate.

advantage:

Proper use of the mediator pattern can avoid excessive coupling between colleague classes, so that each colleague class can be used relatively independently.

Using the mediator pattern, the behavior and cooperation of objects can be abstracted, and the interaction between objects can be handled flexibly.

Using the mediator pattern, the many to many association between objects can be transformed into one to many association, which makes the relationship between objects easy to understand and maintain.

Disadvantages:

Intermediary model is a common model, and it is also a model that is easy to be abused. In most cases, the relationship between colleague classes will not be complicated to a chaotic network structure. Therefore, in most cases, it is OK to encapsulate the dependencies between objects within the colleague class, and there is no need to introduce the mediator mode. Abusing the intermediary model will only make things more complicated. Therefore, we should consider and weigh the pros and cons before we decide to use the mediator model.

More readers interested in Android related content can view the special topics of this site: introduction and advanced tutorial of Android development, summary of Android debugging skills and common problem solving methods, summary of Android basic component usage, summary of Android view skills, summary of Android layout skills and summary of Android control usage

I hope this article will help you in Android programming.

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