Design mode popularity (1) ---- Intermediar mode

xiaoxiao2021-03-06  93

The intermediard mode is a relatively common mode in the design mode. His principle is relatively simple, in fact, the complex interaction communication between each component (object) is processed by one of the "intermediaries".

The Midiator mode resolves complex exchange issues between objects by providing a loosely coupled manner .. In this mode, only one class thoroughly knows other classes, when the class changes, they The intermediaries will be notified, and other related classes (objects / components) will be notified by the intermediaries.

// Take a man and a woman to achieve this design mode through the example of the media dating ... :) Haha. This is "popular mode"

1. Clearly, if a man and a woman have a date, it is assumed that the man puts a date (similar to the woman's proposal). If there is no media, the process is as follows:

a) If the man wants to date with the woman, the man

b) First, please ask your parents (^ _ ^, assume that it is feudal family ...),

c) then inform the woman

d) The woman needs to ask the female parents. Do you agree to date.

Disadvantages: There are too many objects that need to exchange for men, and the relationship is complex. When the interaction between men and women, more complex exchanges will involve more complex exchanges, the exchange of parents of both sides is also more complex, so that four objects (male, Female, male parents, women's parents) complex. It is difficult to control.

Model improvement: Take the media to do "intermediaries" - mode, will become simple: no matter what the person has any requirements, request, just tell "media", as for the request, whoever, who exchanges, only the media know,

// The simulation code class is as follows: &&&&&& :))

Package Meditator;

/ ** *

Title: Men's class *

Description: *

Copyright: Copyright (c) 2004 *

Company: Le Open Studio * @AUTHOR Chen Shaokun * @version 1.0 * /

Public class man {private matchmaker mat; // media

Public Man (MatchMaker Mat) {this.mat = Mat; mat.registeman (this); // Register yourself in the media (declaration)} public static void main (string [] args) {

}

/ ** * Consider whether agreed * @return * / public boolean thinking (string seys) {

System.out.println ("Men Consider: I should not agree?" "); If (Says.length ()> 5) {system.out.println (" I agree); Return True;} Else {system.out.println ("I don't agree."); Return False;}

}

/ ** * Submit date * Men's offer, only need to tell the media, be responsible for interacting with others by the media. * @Param Says * / public void tryst (String Says) {system.out.println ("Men puts a date request, Say: " Says); Mat.DomanTryst (Says);

}

Package Meditator;

/ ** *

Title: Men's Parent Class *

Description: *

Copyright: Copyright (C) 2004 *

Company: Happy work Room * @AUTHOR Chen Shaokun * @version 1.0 * /

Public class manparent {private matchmaker mat; // media human public manparent (Matchmaker Mat) {this.mat = mat; mat.registemanparent (this); // Register yourself (declared)} public static void main (String [] args) {// manparent manparent1 = new manparent ();

/ ** * Consider whether agreed * @return * / public boolean thinking (String Says) {system.out.println ("Men's Parents Consider: Don't you agree?"); If (Says. Length ()> 5) {system.out.println ("We do your parents"); return true;} else {system.out.println ("We don't agree."); return false;}

}

}

/

Package Meditator;

/ ** *

Title: Women's class *

Description: *

Copyright: Copyright (C) 2004 *

Company: Le Open Studio * @AUTHOR Chen Shaokun * @version 1.0 * /

Public class woman {private matchmaker mat; // media

Public Woman (MatchMaker Mat) {this.mat = Mat; mat.registewoman (this); // Register yourself (declared)} public static void main (String [] args) {

}

/ ** * Consider whether to agree * @return * / public boolean thinking (STRING SAYS) {system.out.println ("Woman considers: I should not agree??"); If (Says.Length ()> 5) {system.out.println ("I agree"); return true;} else {system.out.println ("I don't agree."); Return false;}

}

/ ** * Submit date * Women's offer, only need to tell the media, is responsible for interacting with others by the media. * @Param Says * / public void tryst (string says) {system.out.println ("Woman puts a date request, Say: " Says); Mat.DowOMAntryst (Says);

}

Package Meditator;

/ ** *

Title: Women's Parents Class *

Description: *

Copyright: Copyright (C) 2004 *

Company: Happy work Room * @Author Chen Shaokun * @version 1.0 * / public class womanparent {private matchmaker mat; // media human public womanparent (MatchMAT = Mat; mat.registerwomanparent (this); // Registration in the media (declaration)} public static void main (String [] args) {// WomanParent WomanParent1 = new womanparent ();}

/ ** * Consider whether agreed * @return * / public boolean thinking (String Says) {system.out.println ("Women's Parents Consider: We don't have to agree with our parents?"); If (Says. Length ()> 5) {system.out.println ("We do your parents"); return true;} else {system.out.println ("We don't agree."); return false;}

}

}

///

Package Meditator; / ** *

Title: Media Human ----- Intermediaries *

Description: *

Copyright: CopyRight (C) 2004 *

Company: Le Open Studio * @Author Chen Shaokun * @version 1.0 * /

Public class matchmaker {private man; // man private man Woman; // Woman private manparent manp; // Men parent private womanparent womanparent Womanp; // Women's Parents Matchmaker () {} public static void main (String [] args) { Matchmaker matchmaker1 = new matchmaker ();

/ ** * The media handled the date of the man, * Men put forward the appointment, just tell the media, * is responsible for the media to interact with its women, women, male parents * @Param Says * / public void Domantryst String says) {system.out.println ("Many people start processing dating issues"); manp.thinking (seys); Womanp.thinking (Says); System.Out.println ("Media Manager The date of the date is over, and other processes are made according to the results of the consent. "

/ ** * The media handles the dating of women, * is responsible for the media to go with men, women's parents, men's parents, * @Param Says * / public void Dowomantryst (String Says) {system.out.println ("Many people start processing dating Problem start "); man.thinking (sign); womanp.thinking (sign); system.out.println (" "The end of the media handling date is over, do other processing according to the results of the consent");} / ** * Handle other issues such as color gifts ..... * @Param man * / public void doother (string says) {system.out.println ("Handling Other Issues"); Man.Thinking (Says); Woman. Thinking (Says); MANP.THINKING (SAYS); Womanp.Thinking (Says);

// The following four registration methods can be understood that the communication between men and women must pass the media / ** * registered man * @Param Man * / public void registeman (man man) {this.man = man;}

/ ** * Registered woman * @Param Woman * / public void registewoman (Woman Woman) {this.woman = Woman;}

/ ** * Registered men's parents * @Param manp * / public void registemanparent (manparent manp) {this.manp = manp;

}

/ ** * Registered women's parents * @Param Womanp * /

Public void registerwomanparent (WomanParent Womanp) {this.womanp = Womanp;}

}

Package Meditator;

/ ** *

Title: Test class --- can run this class *

Description: *

Copyright: CopyRight (C) 2004 *

Company: Happy Studio * @Author Chen Shaokun * @version 1.0 * /

Public class matchmakerdemo {

Public matchmakerdemo () {}

Public static void main (string [] args) {matchmakerDemo matchmakerDemo1 = new matchmakerDemo (); matchmaker mat = new matchmaker (); // media appears man man = new man (mat); // man, to establish constructor Woman Woman = New Woman (MAT); MANPARENT MANP = New MANPARENT (MAT); WomanParent Womanp = New WomanParent (MAT); System.Out.println ("///") ;; man.tryst ("I want to be with you Dating, can you? "); System.out.println (" /// "); Woman.Tryst (" I want to date you ");}

}

转载请注明原文地址:https://www.9cbs.com/read-123587.html

New Post(0)