文檔設計模式_第1頁
文檔設計模式_第2頁
文檔設計模式_第3頁
文檔設計模式_第4頁
文檔設計模式_第5頁
已閱讀5頁,還剩2頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

設計題編寫代碼比較使用迭代器遍歷鏈表和使用get(intindex)方法遍歷鏈表所有的時間。mportjava.util.*;publicclassTestSpeed{ publicstaticvoidmain(String[]args){ LinkedList<String>list=newLinkedList<String>(); for(inti=0;i<=60096;i++){ list.add("speed"+i); } Iterator<String>iter=list.iterator(); longstarttime=System.currentTimeMillis(); while(iter.hasNext()){ Stringte=iter.next(); } longendTime=System.currentTimeMillis(); ; longresult=endTime-starttime; System.out.println("使用迭代遍歷集合所用時間"+result+"毫秒"); starttime=System.currentTimeMillis(); for(inti=0;i<list.size();i++){ Stringte=list.get(i); } endTime=System.currentTimeMillis(); result=endTime-starttime; System.out.println("使用get方法歷集合所用時間"+result+"毫秒"); }}編寫?zhàn)I漢式和懶漢式單件模式示意代碼,并說明這兩種方式的適應性。(1)餓漢式:publicclassSingleton{ privatestaticSingletonunigueInstance=newSingleton(); privateSingleton(){} publicstaticSingletongetInstance(){ returnunigueInstance; }}(2)懶漢式:publicclassSingleton{ privatestaticSingletonunigueInstance; privateSingleton(){} publicstaticsynchronizedSingletongetInstance(){ if(unigueInstance==null){ unigueInstance=newSingleton(); } returnunigueInstance; }}以下你看到的類和接口是取自一個動作冒險游戲,它們代表游戲角色(Character)的類和角色可以使用的武器行為的類。每個角色一次只能使用一種武器,但是可以在游戲的過程中換武器。你的工作是使用策略模式實現(xiàn)該游戲的架構。publicabstractclassCharacter{ WeaponBehaviorweapon; publicvoidperformWeapon(){ weapon.useWeapon(); } publicabstractvoidfight(); publicvoidsetWeaponBehavior(WeaponBehaviorw){ this.weapon=w; }}publicclassQueenextendsCharacter{ publicQueen(){ weapon=newKnifeBehavior(); } publicvoidfight(){ System.out.println("皇后在戰(zhàn)斗。"); }}publicclassKingextendsCharacter{publicKing(){publicinterfaceHandler{ publicabstractvoidcompuerMultiply(Stringnumber); publicabstractvoidsetNextHandler(Handlerhandler);}publicclassUseIntimplementsHandler{ privateHandlerhandler; privateintresult=1; publicvoidcompuerMultiply(Stringnumber){ try{ intn=Integer.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; }}publicclassUseLongimplementsHandler{ privateHandlerhandler; 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.BigInteger;publicclassUseBigIntegerimplementsHandler{ privateHandlerhandler; privateBigIntegerresult=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; }}publicclassApplication{ privateHandleruseInt,useLong,useBig; publicvoidcreateChain(){ useInt=newUseInt(); useLong=newUseLong(); useBig=newUseBigInteger(); useInt.setNextHandler(useLong); useLong.setNextHandler(useBig); } publicvoidreponseCli

溫馨提示

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

評論

0/150

提交評論