(完整版)設(shè)計模式_第1頁
(完整版)設(shè)計模式_第2頁
(完整版)設(shè)計模式_第3頁
(完整版)設(shè)計模式_第4頁
(完整版)設(shè)計模式_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)

文檔簡介

設(shè)計題get(intindex)方法遍歷鏈表所有的時get(intindex)方法遍歷鏈表所有的時mportjava.util.*;publicclassTestSpeed{publicstaticvoidmain(String[]args)mportjava.util.*;publicclassTestSpeed{publicstaticvoidmain(String[]args){LinkedList<String>list=for(inti=0;i<=60096;i++){list.add("speed"+i);}IteratorvString>iter=list.iterator();longstarttime=System.while(iter.hasNext()){Stringte=iter.next();newLinkedList<String>();currentTimeMillis();}currentTimeMillis();longendTime=System.currentTimeMillis();longresult=endTime-starttime;System.out.println(

starttime=System."使用迭代遍歷集合所用時間currentTimeMillis();result+"毫秒“);for(inti=0;i<list.size();i++){Stringte=list.get(i);();}();endTime=System.currentTimeMillisresult=endTime-starttime;System.out.println(”使用get方法歷集合所用時間"+result+"毫秒“);}}編寫?zhàn)I漢式和懶漢式單件模式示意代碼,并說明這兩種方式的適應(yīng)性。(1)餓漢式:publicclassSingleton{privatestatic SingletonunigueInstanee=newSingleton();privateSingleton(){}publicstaticSingletongetInstance(){publicstaticreturnunigueInstanee;}}(2)懶漢式:publicclassSingleton{privatestatic SingletonunigueInstanee;privateSingleton(){}publicstaticsynchronizedSingletongetInstance(){publicstaticif(unigueInstanee==null){unigueInstaneenewSingleton();}returnunigueInstanee;}}以下你看到的類和接口是取自一個動作冒險游戲,它們代表游戲角色(Character)的類和角色可以使用的武器行為的類。每個角色一次只能使用一種武器,但是可以在游戲的過程中換武器。你的工作是使用策略模式實現(xiàn)該游戲的架構(gòu)。 CftjfactarTnMIKfiifeBehaviorBiOwAndArrowBehAvicr「實區(qū)HITMAxeBehjiviarTnMIKfiifeBehaviorBiOwAndArrowBehAvicr「實區(qū)HITMAxeBehjiviar(]廣實班用磬npublicabstractclassCharacter{WeaponBehaviorweapon;publicvoidperformWeapon(){weapon.useWeapon();}publicabstractvoidfight();publicvoidsetWeaponBehavior(WeaponBehaviorw){this.weapon=w;}}publicclassQueenextendsCharacter{publicQueen(){weapon=newKnifeBehavior();}publicvoidfight(){System.out.println("皇后在戰(zhàn)斗。");}}publicclassKing extendsCharacter{publicKing(){weapon=newSwordBehavior();}publicvoidfight(){System.out.println("國王在戰(zhàn)斗。");}publicclassTrollextendsCharacter{publicTroll(){weapon=newBowAndArrowBehavior();publicvoidfight(){System.out.println("妖怪在戰(zhàn)斗。");}}publicclassKnightextendsCharacter{publicKnight(){weapon=newAxeBehavior();}publicvoidfight(){System.out.println("騎士在戰(zhàn)斗。");}}publicinterfaceWeaponBehavior{publicvoiduseWeapon();publicclassKnifeBehavior implementsWeaponBehavior{publicvoiduseWeapon(){System.out.println("使用匕首戰(zhàn)斗!");}}publicclassBowAndArrowBehaviorimplementsWeaponBehavior{publicvoiduseWeapon(){System.out.println("使用弓箭戰(zhàn)斗!");}}publicclassAxeBehavior implementsWeaponBehavior{publicvoiduseWeapon(){System.out.println(} 使用斧頭戰(zhàn)斗!");}publicclassSwordBehaviorimplementsWeaponBehavior{publicvoiduseWeapon(){//TODOAuto-generatedmethodstub}publicclassGame{publicstaticvoidmain(String[]args){//Kingking=newKing();Characterking=newKing();//顯示誰在戰(zhàn)斗king.fight();//使用武器king.performWeapon();System.out.println( 使用寶劍戰(zhàn)斗!");//改變武器(對象)king.setWeaponBehavior(newKnifeBehavior());//再次使用武器king.performWeapon();System.out.println("\n另一個角色");Charactertroll= newTroll();//Trolltroll=newTroll();troll.fight();troll.performWeapon();troll.setWeaponBehavior(newSwordBehavior());troll.performWeapon();}}使用責(zé)任鏈模式計算階乘,設(shè)計要求:設(shè)計一個類,該類使用int型數(shù)據(jù)計算階乘。設(shè)計一個類,該類使用long型數(shù)據(jù)計算階乘。設(shè)計一個類,該類使用BigInteger型數(shù)據(jù)計算階乘。使用責(zé)任鏈模式將上述對象組成一個責(zé)任鏈,要求責(zé)任鏈上對象的順序是:首先使用int型數(shù)據(jù)計算階乘,然后使用long型數(shù)據(jù)計算階乘,最后使用BigInteger型數(shù)據(jù)計算階乘。用戶可以使用責(zé)任鏈計算任意整數(shù)的階乘,類圖如下所示。注意:不夠?qū)懣蓪懙奖趁娌⒆⒚鱲oidcompuerMultiply(Stringnumber);voidsetNextHandler(Handlerhandler);publicinterfaceHandler{voidcompuerMultiply(Stringnumber);voidsetNextHandler(Handlerhandler);}publicclassUselnt implementsHandler{private Handlerhandlerprivate intresult=1;publicvoidcompuerMultiply(Stringnumber){try{intn=lnteger.parseInt(number);inti=1;while(i<=n){

result=result*i;);if(result<=0){);System.out.println( "超出我的能力范圍,我計算不了puerMultiply(number);return;}i++;}System.out.println(number+ "的階乘:"+result);}catch(NumberFormatExceptionexp){System.out.println(exp.toString());publicvoidsetNextHandler(Handlerhandler){this.handler=handler;}}PublicclassUseLongimplementSdAanprivateHandlerhandler;privatelongresult=1;publicvoidcompuerMultiply(Stringnumber){

try{longn=Long.parseLong(number);longi=1;while(i<=n){result=result*i;if(result<=0){System.out.println( "超出我的能力范圍,我計算不了 ");puerMultiply(number);return;}i++;}System.out.println(number+ "的階乘:"+result);}catch(NumberFormatExceptionexp){System.out.println(exp.toString());}}publicvoidsetNextHandler(Handlerhandler){this.handler=handler;}}importjava.math.Biglnteger;publicclassUseBigInteger implementsHandler{privateHandlerhandler;privateBigInteger result=newBigInteger("1");publicvoidcompuerMultiply(Stringnumber){try{BigIntegern=newBigInteger(number);BigIntegerONE=newBigInteger("1");BigIntegeri=ONE;while(pareTo(n)<=0){result=result.multiply(i);i=i.add(ONE);}System.out.println(number+ "的階乘:"+result);}catch(NumberFormatExceptionexp){System.out.println(exp.toString());}}publicvoidsetNextHandler(Handlerhandler){this.handler=handler;this.handler=handler;}publicclassApplication{privateHandleruseInt,useLong,useBig;publicvoidcreateChain(){useInt=newUseInt();useLong=newUseLong();useBig=newUseBigInt

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論