Pseudo-mediation mode (5.5)

Source: Internet
Author: User

Mediator Pattern)Is a "flat table" or "Manager-type" delegation.

In the mediation mode, an intermediary is a common and unique proxy of all participants who want to interact with each other. It forwards or processes messages.

Platform intermediary

In real life, there are various mediator organizations, such as markets, stock exchanges, marriage agencies, and housing agencies. They effectively include participants) into the interaction between the participants and the intermediary. However, these intermediaries areExample of an error in the gof mediation Mode. Many people on the internet use QQ as an example to introduce the intermediary mode, which is obviously not analyzed from the code perspective,Take it for granted.

Here is a realistic scenario. A party is attended by boys, boys, girls, and girls. Everyone can interact with other objects. How complicated is the code to implement this scenario? Only when you actually write the code, you will find that 0.1 million objects are talking to each other, but there are only two classes of code to be written.

Routine 5 9 does not need to mediate package delegate. mediator. party; public abstract class actor {string name; public abstract void send (string message, actor to); public abstract void receive (string message, actor from);} package delegate. mediator. party; import static tool. print. *; public class boy extends actor {private int count; Public boy (string name) {This. name = Name ;}@ override public void send (string message, actor) {PLN (this. name + "to" +. name + ": \" "+ message +" \ "");. receive (message, this) ;}@ override public void receive (string message, actor from) {char c = (char) (math. random () * 4 + 'A'); If (C! = 'B') {string MSG = message + "" + C; send (MSG, from) ;}} package delegate. mediator. party; public class party {public static void main () {actor [] alist = new actor [] {New boy ("B1"), new boy ("B2 "), new boy ("B3"), new boy ("B4"), new girl ("g1"), new girl ("G2 "), new girl ("G3")}; For (INT I = 0; I <alist. length; I ++) {int Rand = (INT) (math. random () * alist. length); alist [I]. send ("hi", alist [Rand]) ;}}

In the test code, each Party participant initiates a conversation with random objects. The dialog content is a random character. The dialog must last until the random character is B (Bay. A running result ("// end" is not the output of the program ):

B1 to B4: "Hi"
B4 to B1: "Hi D"
B1 to B4: "Hi d"
B4 to B1: "Hi d"
B1 to B4: "Hi d"// End
B2 to B3: "Hi"// End
B3 to G2: "Hi" // end
B4 to B1: "Hi" // end
G1 to G2: [HI]
G2 to G1: [Hi D]
G1 to G2: [Hi d]
G2 to G1: [Hi d]
G1 to G2: [Hi d A]
G2 to G1: [Hi d a D]
G1 to G2: [Hi d a d c]
G2 to G1: [Hi d a d c a]
G1 to G2: [Hi d a d c a]
G2 to G1: [Hi d a d c a]
G1 to G2: [Hi d a d c a]
G2 to G1: [Hi d a d c a c] // actually talked so much// End
G2 to B2: [HI]
B2 to G2: "Hi C"
G2 to B2: [Hi c a]
B2 to G2: "Hi c"
G2 to B2: [Hi c a]
B2 to G2: "Hi c a c"
G2 to B2: [Hi c a]
G3 to G1: [HI]

When discussing the mediation mode, many people see the interaction of objects in a mesh structure, and almost every object needs to interact with other objects. [Gof] intermediary mode: "defines an intermediary object to encapsulate the interaction between a series of objects. The intermediary makes each objectMutual reference does not need to be displayedTo make the coupling loose andIndependent change of interaction between them". In fact, the above Code tells the truth that the Mediation mode is not required here:

① Objects do not need to be explicitly referenced by each other. Boy and girl are sub-classes of actor. Even if there are specific types such as boy and girl in the Party, the class bodies of boy and girl depend on actor. ② The interaction of a large number of objects in multiple classes (boy and girl) does not necessarily lead to excessive coupling between classes. There is no coupling between boy and girl. Similarly, if they are actors, there is no coupling between them.

Therefore, the above example is not required. Intermediary(Not here Intermediary Mode.

However, if an object needs a girlfriend to interact with every girl object one by one, it is very inefficient and unrealistic. At this time, he needsA broadcast platform". In a platform-based intermediary, no matter how diverse the participants are, the role of the intermediary is like a bulletin board. So it is very easy to think of the observer mode. In fact, as a billboardThe "mediation mode" is the observer mode..

Therefore, becauseIntermediary partyNow, as an interactive platform, it is observed by all interested participants!Again different from the [gof] intermediary Mode. In [gof], it writes"Implement mediator as an observer, and colleags as subject". At this time, our code is:Mediator is implemented asSubjectAnd each colleags serves as the observer.

This "desktop intermediary code is everywhere on the Internet.

Objects and Classes

[Gof] intermediary mode: "defines an intermediary object to encapsulate the interaction between a series of objects. The intermediary makes each objectMutual reference does not need to be displayedTo make the coupling loose ..."

This also brings us a question: what is the "object" here?

In object-oriented languages such as Java, yqj2065 has always stressed that classes are the first class. Programmer programming targets classes rather than objects. As a matter of fact, the [gof] intermediary mode is embodied in the "Manager-style" delegation mode.If the status of a class changes, many other classes (objects) change accordingly.Unlike stock exchanges and marriage agencies, although participants have many objects, there are not many real classes and the interaction is simple.

Some people say: some men and women in the party are always talking about the end of the party; some boys can always talk about basketball; some girls only talk about girls, some girls only talk to well-known people ...... How do you deal with these objects?

Another sentence: I will not process objects but classes-Anonymous classes! You can use an anonymous class or a Lambda expression to leave unlimited expansion space for the customer.


In short,Examples of stock exchanges, marriage agencies, and chat rooms are not in the [gof] intermediary mode..

Pseudo-mediation mode (5.5)

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.