第188章

小A:“命令模式怎樣去實現它?”

大B:“命令模式裡邊一般都有以下幾個角色:客戶端,請求者,命令接口,命令實現,接受者。下邊是簡單命令模式的實現代碼實現。”

publicclassClient{

publicstaticvoidmain(String[]args){

Receiverreceiver=newReceiver();

CommandmandOne=newConcreteCommandOne(receiver);

CommandmandTwo=newConcreteCommandTwo(receiver);

Invokerinvoker=newInvoker(mandOne,mandTwo);

invoker.actionOne();

invoker.actionTwo();

}

}

publicclassInvoker

{

privateCommandmandOne;

privateCommandmandTwo;

publicInvoker(CommandmandOne,CommandmandTwo){

this.orgmandOne=mandOne;

this.orgmandTwo=mandTwo;

}

publicvoidactionOne(){

mandOne.execute();

}

publicvoidactionTwo(){

mandTwo.execute();

}

}

publicinterfaceCommand{

voidexecute();

}

publicclassConcreteCommandOneimplementsCommand{

privateReceiverreceiver

publicConcreteCommandOne(Receiverreceiver){

this.receiver=receiver;

}

publicvoidexecute(){

receiver.actionOne();

}

}

publicclassConcreteCommandTwoimplementsCommand{

privateReceiverreceiver

publicConcreteCommandTwo(Receiverreceiver){

this.receiver=receiver;

}

publicvoidexecute(){

receiver.actionTwo();

}

}

publicclassReceiver{

publicReceiver(){

//

}

publicvoidactionOne(){

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

}

publicvoidactionTwo(){

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

}

}

第70章第57章第101章第24章第107章第163章第63章第149章第59章第157章第172章第146章第107章第21章第37章第205章第55章第92章第160章第102章第104章第100章第167章第211章第59章第51章第158章第18章第172章第15章第10章第147章第61章第59章第19章第160章第214章第160章第156章第116章第126章第46章第144章第186章第52章第39章第127章第47章第186章第217章第44章第30章第199章第7章第8章第68章第147章第26章第62章第53章第33章第116章第168章第202章第223章第197章第122章第184章第73章第108章第175章第29章第24章第173章第161章第166章第123章第138章第186章第63章第42章第39章第105章第118章第136章第56章第163章第189章第147章第62章第113章第86章第22章第81章第190章第175章第127章第170章
第70章第57章第101章第24章第107章第163章第63章第149章第59章第157章第172章第146章第107章第21章第37章第205章第55章第92章第160章第102章第104章第100章第167章第211章第59章第51章第158章第18章第172章第15章第10章第147章第61章第59章第19章第160章第214章第160章第156章第116章第126章第46章第144章第186章第52章第39章第127章第47章第186章第217章第44章第30章第199章第7章第8章第68章第147章第26章第62章第53章第33章第116章第168章第202章第223章第197章第122章第184章第73章第108章第175章第29章第24章第173章第161章第166章第123章第138章第186章第63章第42章第39章第105章第118章第136章第56章第163章第189章第147章第62章第113章第86章第22章第81章第190章第175章第127章第170章