天天看小說

第75章

小A:“師兄,如果能舉個(gè)例子的話,那就更好了。”

大B:“好。沒問題的。那就舉個(gè)通俗的模式吧!就拿一個(gè)男人和女人通過媒人約會(huì)的例子來實(shí)現(xiàn)這種設(shè)計(jì)模式吧。”

1、明確,男人和女人如果約會(huì),假設(shè)男方提出約會(huì)(女方提出過程類似)。如果沒有媒人,該過程如下:

A、男方如果想和女方約會(huì)

B、首先請示自己的父母(假設(shè)是封建家庭哦……),

C、然後通知女方

D、女方需要請示女方父母,是否同意約會(huì)。

弊端:需要男方交換的對象太多,且關(guān)係複雜,當(dāng)隨著男女之間的交往的複雜,會(huì)涉及到更多複雜的交換,雙方父母的交換也很更復(fù)雜,使得四個(gè)對象(男、女、男方家長、女方家長)關(guān)係複雜,難以控制。

模式改進(jìn):採取媒人做‘中介者’-模式,將變得簡單,其中無論那一個(gè)人有什麼要求,請求,只需要告訴‘媒人’,至於該請求需要告訴誰,和誰交換,只有媒人知道。

//模擬代碼類如下:

packagemeditator;

/**

*/

publicclassMan{

privateMatchmakermat;//媒人

publicMan(Matchmakermat){

this.mat=mat;

mat.registeMan(this);//把自己在媒人那裡註冊(聲明)

}

publicstaticvoidmain(String[]args){

}

/**

*考慮是否同意

*@return

*/

publicbooleanthinking(Stringsays){

System.out.println(“男人考慮:我該不該同意呢……”);

if(says.length()>5){

System.out.println(“我同意了”);

returntrue;

}

else{

System.out.println(“我不同意。”);

returnfalse;

}

}

/**

*提出約會(huì)

*男人提出約會(huì),只需要告訴媒人,由媒人負(fù)責(zé)跟其他人交互。

*@paramsays

*/

publicvoidtryst(Stringsays){

System.out.println(男人提出約會(huì)請求,說:+says);

mat.doManTryst(says);

}

}

packagemeditator;

/**

*《p》Title:男方家長類《/p》

*《p》Description:《/p》

*@version1.0

*/

publicclassManParent{

privateMatchmakermat;//媒人類

publicManParent(Matchmakermat){

this.mat=mat;

mat.registeManParent(this);//把自己在媒人那裡註冊(聲明)

}

}

/**

*考慮是否同意

*@return

*/

publicbooleanthinking(Stringsays){

System.out.println(“男方父母考慮:我們做父母的該不該同意呢……”);

if(says.length()>5){

System.out.println(“我們做父母的同意了”);

returntrue;

}

else{

System.out.println(“我們做父母的不同意。”);

returnfalse;

}

}

}

/////////////////////////////////////////////////////////////

packagemeditator;

/**

*《p》Title:女人類《/p》

*《p》Description:《/p》

*/

publicclassWoman{

privateMatchmakermat;//媒人

publicWoman(Matchmakermat){

this.mat=mat;

mat.registeWoman(this);//把自己在媒人那裡註冊(聲明)

}

/**

*考慮是否同意

*@return

*/

publicbooleanthinking(Stringsays){

System.out.println(“女人考慮:我該不該同意呢……”);

if(says.length()>5){

System.out.println(“我同意了”);

returntrue;

}

else{

System.out.println(“我不同意。”);

returnfalse;

}

}

/**

*提出約會(huì)

*女人提出約會(huì),只需要告訴媒人,由媒人負(fù)責(zé)跟其他人交互。

*@paramsays

*/

publicvoidtryst(Stringsays){

System.out.println(女人提出約會(huì)請求,說:+says);

mat.doWomanTryst(says);

}

}

////////////////////////////////////////////////////////

packagemeditator;

/**

*《p》Title:女方家長類《/p》

*《p》Description:《/p》

*/

publicclassWomanParent{

privateMatchmakermat;//媒人類

publicWomanParent(Matchmakermat){

this.mat=mat;

mat.registerWomanParent(this);//把自己在媒人那裡註冊(聲明)

}

/**

*考慮是否同意

*@return

*/

publicbooleanthinking(Stringsays){

System.out.println(“女方父母考慮:我們做父母的該不該同意呢……”);

if(says.length()>5){

System.out.println(“我們做父母的同意了”);

returntrue;

}

else{

System.out.println(“我們做父母的不同意。”);

returnfalse;

}

}

}

///////////////////////////////////////////////////////////////

packagemeditator;

/**

*《p》Title:媒人類——中介者《/p》

*《p》Description:《/p》

*@version1.0

*/

publicclassMatchmaker{

privateManman;//男人

privateWomanwoman;//女人

privateManParentmanp;//男方父母

privateWomanParentwomanp;//女方父母

Matchmaker(){

}

publicstaticvoidmain(String[]args){

Matchmakermatchmaker1=newMatchmaker();

}

/**

*媒人處理男人提出的約會(huì),

*男人提出約會(huì),則只需要把該問題告訴媒人,

*由媒人負(fù)責(zé)去跟其女方,女方父母,男方家長交互

*@paramsays

*/

publicvoiddoManTryst(Stringsays){

System.out.println(“媒人開始處理約會(huì)問題開始”);

womanp.thinking(says);

System.out.println(“媒人處理約會(huì)問題結(jié)束,根據(jù)同意的結(jié)果做其他處理”);

}

/**

*媒人處理女人提出的約會(huì),

*由媒人負(fù)責(zé)去跟男方,女方父母,男方家長交互

*@paramsays

*/

publicvoiddoWomanTryst(Stringsays){

System.out.println(“媒人開始處理約會(huì)問題開始”);

man.thinking(says);

manp.thinking(says);

womanp.thinking(says);

System.out.println(“媒人處理約會(huì)問題結(jié)束,根據(jù)同意的結(jié)果做其他處理”);

}

/**

*處理彩禮等其他問題……

*@paramman

*/

publicvoiddoOther(Stringsays){

System.out.println(“處理其他問題”);

man.thinking(says);

woman.thinking(says);

manp.thinking(says);

womanp.thinking(says);

}

//以下四個(gè)註冊方法,可以理解爲(wèi),男方和女方之間的通訊必須通過媒人

/**

*註冊男人

*@paramman

*/

publicvoidregisteMan(Manman){

this.man=man;

}

/**

*註冊女人

*@paramwoman

*/

publicvoidregisteWoman(Womanwoman){

this.woman=woman;

}

/**

*註冊男方家長

*@parammanp

*/

publicvoidregisteManParent(ManParentmanp){

this.manp=manp;

}

/**

*註冊女方家長

*@paramwomanp

*/

publicvoidregisterWomanParent(WomanParentwomanp){

this.womanp=womanp;

}

}

packagemeditator;

/**

*《p》Title:測試類——可以運(yùn)行該類《/p》

*《p》Description:《/p》

*《p》Copyright:Copyright(c)2004《/p》

*@version1.0

*/

publicclassMatchmakerDemo{

publicMatchmakerDemo(){

}

publicstaticvoidmain(String[]args){

MatchmakerDemomatchmakerDemo1=newMatchmakerDemo();

Matchmakermat=newMatchmaker();//媒人出現(xiàn)

Manman=newMan(mat);//男人,以媒人爲(wèi)建立構(gòu)造參數(shù)

Womanwoman=newWoman(mat);

ManParentmanp=newManParent(mat);

WomanParentwomanp=newWomanParent(mat);

////////////////

System.out.println(“///////////////////////////////////////////////////”);

man.tryst(“我想和你約會(huì),可以嗎?”);

////////////////

System.out.println(“///////////////////////////////////////////////////”);

woman.tryst(“想和你約會(huì)”);

}

}

第125章第153章第44章第206章第133章第126章第203章第60章第131章第25章第129章第53章第5章第127章第123章第68章第28章第206章第208章第123章第53章第215章第208章第34章第153章第170章第113章第94章第165章第220章第184章第100章第11章第104章第126章第77章第19章第38章第147章第13章第170章第156章第176章第118章第141章第118章第171章第183章第11章第163章第110章第176章第177章第116章第51章第48章第40章第18章第61章第61章第27章第198章第15章第94章第131章第58章第122章第111章第125章第221章第64章第206章第109章第222章第168章第132章第167章第46章第76章第7章第199章第19章第101章第36章第133章第124章第139章第1章第151章第166章第73章第209章第72章第173章第193章第64章第110章第7章第44章
第125章第153章第44章第206章第133章第126章第203章第60章第131章第25章第129章第53章第5章第127章第123章第68章第28章第206章第208章第123章第53章第215章第208章第34章第153章第170章第113章第94章第165章第220章第184章第100章第11章第104章第126章第77章第19章第38章第147章第13章第170章第156章第176章第118章第141章第118章第171章第183章第11章第163章第110章第176章第177章第116章第51章第48章第40章第18章第61章第61章第27章第198章第15章第94章第131章第58章第122章第111章第125章第221章第64章第206章第109章第222章第168章第132章第167章第46章第76章第7章第199章第19章第101章第36章第133章第124章第139章第1章第151章第166章第73章第209章第72章第173章第193章第64章第110章第7章第44章
主站蜘蛛池模板: 湘潭市| 双流县| 祁门县| 齐河县| 绥江县| 陆丰市| 十堰市| 永靖县| 碌曲县| 金平| 东宁县| 樟树市| 称多县| 英山县| 杭锦后旗| 常州市| 柳林县| 平潭县| 闽侯县| 松滋市| 财经| 绥阳县| 肥城市| 榕江县| 扶绥县| 辽阳县| 泽州县| 拜城县| 泸西县| 秭归县| 崇左市| 娄底市| 资溪县| 南安市| 孟村| 阜宁县| 正蓝旗| 五家渠市| 永善县| 靖宇县| 河北区|