




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、實(shí)驗(yàn)5:責(zé)任鏈模式一、實(shí)驗(yàn)?zāi)康谋緦?shí)驗(yàn)要求學(xué)生掌握責(zé)任鏈模式, 要求學(xué)生理解責(zé)任鏈模式中的二種角色并完成類的定義及測試。二、實(shí)驗(yàn)任務(wù)1計(jì)算整數(shù)的階乘。要求在計(jì)算階乘時(shí)綜合考慮計(jì)算速度與內(nèi)存占用多少,使用責(zé)任鏈模式分析有哪些類以及這些類的角色,完成這些類的定義并分別測試每一種處理情況。答:處理者(Handler )角色:類Handler具體處理者(ConcreteHandler)角色:類 UseInt、類 UseLong、類 UseBigIntegerHandler.java public interface publicvoidpublicvoid( 1 )處理者(Handler )Handle
2、r handleRequest(String str);setNextHandler(Handler handler);(2)具體處理者(ConcreteHandler )UseInt.javapublic class UseInt implements Handlerprivate Handler handler ;private int result = 1;public void handleRequest(String str) try int n=Integer. parseInt (str);int i=1;while (i<=n) result = result *i;if
3、 ( result <=0)System. out .println( " 超出我的能力范圍,我計(jì)算不了 " ); handler .handleRequest(str); return ; i+;System. out .println(str+ " 的階乘: " +result +"n" ); catch (NumberFormatException exp) System. out .println(exp.toString(); public void setNextHandler(Handler handler) th
4、is . handler =handler; UseLong.javapublic class UseLong implements Handler private Handler handler ;long result =1;public void handleRequest(String str) try int i=1; long n=Long. parseLong (str); while (i<=n) result =result *i; if ( result <=0)System. out .println( "超出我的能力范圍,我計(jì)算不了");
5、handler .handleRequest(str); return ; i+; System. out .println(str+ "的階乘:"+result +"n"); catch (NumberFormatException exp) System. out .println(exp.toString(); public void setNextHandler(Handler handler) this . handler =handler; UseBigInteger.javapublic class UseBigInteger implem
6、ents Handler private Handler handler ;private BigInteger result = new BigInteger( "1"); public void handleRequest(String str)BigInteger n =new BigInteger(str);BigInteger ONE =new BigInteger( "1");BigInteger i = ONE;while (pareTo(n)<=0) result =result .multiply(i); i=i.add(ONE)
7、;System. out .println(str+ "的階乘:"+result +"n"); public void setNextHandler(Handler handler)this . handler =handler; (3)測試類:Test.javapublic class Test private Handler useInt , useLong , useBigInteger public void createChain()useInt = new UseInt();useLong = new UseLong();useBigInte
8、ger = new UseBigInteger();useInt .setNextHandler( useLong );useLong .setNextHandler( useBigInteger );public void responseClient(String str)System. out .println( "計(jì)算"+str+ "的階乘");useInt .handleRequest(str);public static void main(String口 args) Test test= new Test();test.createChai
9、n();test.responseClient("10");test.responseClient("20");test.responseClient("32");運(yùn)行截圖如下:計(jì)算10的階乘工。的階乘:36283G0計(jì)苴20的階乘超出我的熊力范圍,我計(jì)算不了2口的階乘:243290200817 6640000計(jì)算獎(jiǎng)的階乘超出我的能力范圍,我計(jì)息不了超出我的熊力范圍,蓑計(jì)算不732 的階乘:i5315C-S5533353353O-S213t221Q0CQOC'2.采購審批往往是分級(jí)進(jìn)行的,其常常根據(jù)采購金額的不同由不同層次的主
10、管人員來審批。例如,主任可以審批5萬元以下(不包括 5萬元)的采購單,副董事長可以審批5萬元至10萬元(不包括10萬元)的采購單,董事長可以審批 10萬元至50萬元(不包括50萬元)的采購單,50萬元及以上的采購單就需要開董事會(huì)討論決定。使用責(zé)任鏈模式分析有哪些類以及這些類的角色,完成這些類的定義并分別測試每一種處理情況。答:處理者(Handler)角色:類 Handler具體處理者(ConcreteHandler)角色:類 ChairMan.java、類 Vice_president、類 President、類 Meetting(1)處理者(Handler)Handler.javapubli
11、c interface Handler publicvoidhandleRequest(String str);publicvoidsetNextHandler(Handler handler);(2)具體處理者(ConcreteHandler)ChairMan.javapublic classChairMan implements HandlerHandlerhandler;public void requestHandle(String str) try long money = Long.parseLong (str);if (money<50000KSystem. out .pr
12、intln("主任審批"+money+ "元");System. out .println(); else System. out .println(money+"元"+"超出主任范疇"); 3handler .requestHandle(str); catch (Exception e)public void setNextHandler(Handler handler) this . handler =handler;Vice_president.javaclass Vice_president impleme
13、nts Handler Handler handler ;public void requestHandle(String str) try long money=Long. parseLong (str);if (money>=50000&&money<100000) System. out .println("副董事長審批"+money+ "元");System. out .println(); else System. out .println(money+ "元"+"超出副董事長范疇 &q
14、uot;); handler .requestHandle(str); catch (Exception e) public void setNextHandler(Handler handler) this . handler =handler;President.javaclass President implements Handler Handler handler ;public void requestHandle(String str) try long money=Long. parseLong (str);if (money>=100000&&money
15、<500000KSystem. out .println( "董事長審批"+money+ "元"); System. out .println(); else System. out .println(money+ "元"+"超出董事長范疇"); handler .requestHandle(str); catch (Exception e)public void setNextHandler(Handler handler) this . handler =handler;Meetting.javaclas
16、s Meetting implements Handler Handler handler ;public void requestHandle(String str) try BigInteger money= new BigInteger(str);if (pareTo( new BigInteger( "500000")>=0)System. out .println("會(huì)議審批"+money+ "元");System. out .println(); catch (Exception e)public void setN
17、extHandler(Handler handler) this . handler =handler;(3)測試類:Test.javapublic class Test ChairMan chairman ;11Vice_presidentvice_president ;President president ;Meetting meetting ;public void createChine()chairman = new ChairMan();vice_president = new Vice_president();president = new President。;meettin
18、g = new Meetting ();chairman.setNextHandler(vice_president);vice_president .setNextHandler( president ); president.setNextHandler(meetting );public void setClientRequest(String str) System. out .println( "采購金額:"+str+ chairman .requestHandle(str);"元");public staticTest test =void
19、main(String口 args) new Test();test.createChine();test.setClientRequest( test.setClientRequest( test.setClientRequest( test.setClientRequest("6000");“60000");"110000");"6000000");運(yùn)行截圖如下:乘財(cái)金額+8???。元主任審批元采購金額:行"“元60。*元超出主件后暗 副董事長審批COOOO元采購金額打1cle1。0元二元超出主任兔璃 元超出副董
20、事長范瞬董事長市批110000元采購金額"QmQQQQ元EDgOQ 0元超出主任范疇0。亓超出副董事長范疇00元超出董事長范矚 會(huì)議審批元3.公司請假審批流程如下:如果請假小于3天只需要項(xiàng)目經(jīng)理批復(fù)就行;如果請假大于等于3天,小于7天需要人事經(jīng)理批復(fù);如果請假大于等于7天,小于15天需要總經(jīng)理批復(fù);如果申請請假大于等于15天,決絕批復(fù)。使用責(zé)任鏈模式分析有哪些類以及這些類的角色,完成這些類的定義并分別測試每一種處理情況。答:處理者(Handler)角色:類 Handler具體處理者(ConcreteHandler)角色:類 Project_Manager、類 Personel_Man
21、ager、 類 Top_Manager、類 Meetting(1)處理者(Handler)Handler.javapublic interface Handler publicvoidhandleRequest(String str);publicvoidsetNextHandler(Handler handler);(2)具體處理者(ConcreteHandler)Project_Manager.javapublic classProject_Managerimplements HandlerHandlerhandler ;public void requestHandle(String s
22、tr) try int day=Integer.parseInt (str);if (day<3)System.out .println("項(xiàng)目經(jīng)理批復(fù) n"); else System.out .println("超出項(xiàng)目經(jīng)理職權(quán) ");handler .requestHandle(str); catch (Exception e)public void setNextHandler(Handler handler) this . handler =handler;Personel_Manager.javapublic classPersonel
23、_Managerimplements HandlerHandlerhandler ;public void requestHandle(String str) try int day=Integer. parseInt (str);if (day>=3&&day<7) System.out .println("人事經(jīng)理批復(fù) n"); else System.out .println("超出人事經(jīng)理職權(quán) ");handler .requestHandle(str); catch (Exception e)public void
24、setNextHandler(Handler handler) this . handler =handler;Top_Manager.javapublic classTop_Managerimplements HandlerHandlerhandler ;publicvoid requestHandle(String str)try int day=Integer. parseInt (str);if (day>=7&&day<15) System. out .println("總經(jīng)理批復(fù) n"); else System. out .prin
25、tln("總經(jīng)理考慮");handler .requestHandle(str); catch (Exception e)public void setNextHandler(Handler handler) this . handler =handler;Meetting.javapublic classMeetting implements HandlerHandlerhandler;public void requestHandle(String str) try int day=Integer. parseInt (str); if (day>=15)System. out .println("決絕批復(fù) n"); catch (Exception e)public void setNextHandler(Handler handler) this . handler =handler;(3)測試類:Test.javapublic class Test Project_Man
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 雅安博雅農(nóng)旅發(fā)展有限責(zé)任公司勞派銷售人員筆試參考題庫附帶答案詳解
- 求職申請實(shí)訓(xùn)報(bào)告范文
- 浙江國企招聘2024浙江舟山群島新區(qū)浙東國有資產(chǎn)投資發(fā)展有限公司招聘1人筆試參考題庫附帶答案詳解
- 二零二五年度金飾抵押貸款合同糾紛解決協(xié)議
- 2025年度購物中心店面出租協(xié)議
- 二零二五年度孩子上學(xué)期間安全保障與生活輔導(dǎo)合同
- 2025年度模特時(shí)尚秀活動(dòng)合約
- 二零二五年度光伏發(fā)電燈具安裝與并網(wǎng)合同
- 二零二五年度戀愛雙方家庭背景調(diào)查及信任建立協(xié)議
- 2025年度智能穿戴設(shè)備研發(fā)資金托管管理合同
- 身體功能訓(xùn)練
- 部編人教版四年級(jí)語文下冊《全冊全套》課件ppt
- 英文版-你來比劃我來猜游戲
- 皖2015s209 混凝土砌塊式排水檢查井
- 五年級(jí)道德與法治下冊 (我參與我奉獻(xiàn))新課件
- 診所負(fù)責(zé)人聘用合同
- 單層工業(yè)廠房排架結(jié)構(gòu)設(shè)計(jì)正文
- 兩人合伙開旅行社合同范本
- 我的家鄉(xiāng)湖北宜昌介紹宜昌城市介紹課件
- 智能嬰兒床的設(shè)計(jì)與實(shí)現(xiàn)
- 小學(xué)生漫畫獨(dú)立學(xué)習(xí)力(全3冊)
評(píng)論
0/150
提交評(píng)論