課件課后練習(xí)代碼經(jīng)典案例_第1頁
課件課后練習(xí)代碼經(jīng)典案例_第2頁
課件課后練習(xí)代碼經(jīng)典案例_第3頁
課件課后練習(xí)代碼經(jīng)典案例_第4頁
課件課后練習(xí)代碼經(jīng)典案例_第5頁
已閱讀5頁,還剩56頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

JavaSE01DMSV3Controller單例模式應(yīng)用與測DMSV3DMSFrame的重構(gòu)DMSV3TimerTask模板模式應(yīng)DMSV3項(xiàng)目重構(gòu)指DMSV3Controller重新構(gòu)建DMSV3Controller*publicclassController/**單例模式應(yīng)用privatestaticControllerinstance=newprivateController()publicstaticController{returnpublicclassTestCasepublicvoid//Controllerc0=newController();Controllerc1=Controllerc2=}}執(zhí)試案例:得創(chuàng)建DMSFramepublicclassDMSFrameextendsJFrame/**單例模式privatestaticDMSFrameinstance=newprivate{publicstaticDMSFrame{return*privatevoidinit()publicclassTestCasepublicDMSV3DMSFrame重構(gòu)DMSFrame類,更改對Controller實(shí)例的方式,更新為通過單例方法獲得控制器實(shí)例如,按鈕方法在DMSV2中采用如下代碼實(shí)現(xiàn):list.addActionListener(newActionListener()publicvoidactionPerformed(ActionEvent更新成,控制器實(shí)例通過單例獲取方法獲得list.addActionListener(newActionListener()publicvoidactionPerformed(ActionEvent{Controllercontroller在DMSV2DMSFrame類的基礎(chǔ)上重構(gòu),去除控制器實(shí)例變量的controller,如果需要用到控制器實(shí)例,就更新為通過控制器單例獲取方法得到。OnlinePanel類無需重構(gòu),與DMSV2中的代碼一致(過來即可)。*重構(gòu)的DMSV3DMSFrame任何對Controller對象 *publicclassDMSFrameextendsJFrameprivateOnlinePanelonlinePanel;privateJTextArealistArea;//privateJTextFieldsearchKey;// privateJScrollPanelistPane;//列表顯示區(qū)域的滾動(dòng)面板*字體顏色255,255,255*未選中按鈕背景33,177,169#*未選中按鈕邊框 *選中按鈕背景18,117,112#*選中按鈕邊框32,170,162#*搜索邊框169,251,246#*進(jìn)度條背景0,0,0*進(jìn)度條顏色28,163,156#privateColorfontColor=newColor(255,255,privateColorbuttonBackgrund=newColor(33,177,169);privateColorbuttonBorder=newColor(169,251,246);privateColorselectedButtonBackgrund=newColor(18,117,privateColorselectedButtonBorder=newColor(32,170,privateColorsearchBorder=newColor(169,251,246);privateColoronlineBackground=newColor(0,0,privateColoronlineColor=newColor(28,163,privateJButton[]buttons={};/**單例模式privatestaticDMSFrameinstance=newDMSFrame();privateDMSFrame() publicstaticDMSFramegetInstance()return *privatevoidinit()setTitle("DMSsetSize(800,JPanelcontent=newJLabelonline=new online.setBounds(30,60,60,JButtonlist=newJButton("list.setBounds(30,100,60,JButtondurnAsc=newJButtondurnAsc.setBounds(100,100,100,JButtondurnDesc=newJButton(durnDesc.setBounds(210,100,100,JButtongroupBy=newJButtongroupBy.setBounds(320,100,100,searchKey=newsearchKey.setBounds(430,100,270,JButtonsearch=newJButton(search.setBounds(705,100,60,JLabelheader=new 進(jìn)程 +"登錄時(shí) +"登出時(shí) +"持續(xù)時(shí) 登錄header.setBounds(30,135,740,listArea=newlistPane=newlistPane.setBounds(30,155,740,Fontmono=new listArea.setFont(mono);//Component[]all //allfor(Componentcomponent:all)if(componentinstanceofJTextField)continue; searchKey.setBorder(newonlinePanel=new onlinePanel.updateOnline(10);//默認(rèn)顯示10onlinePanel.setBounds(100,60,670,ImageIconimg=new//放 JLabelbackground=newbackground.setBounds(0,img.getIconWidth(),buttons=newbuttons[0]=buttons[1]=buttons[2]=buttons[3]=buttons[4]=list.addActionListener(newActionListener()publicvoidactionPerformed(ActionEvente)Controllercontroller= durnAsc.addActionListener(newActionListener()publicvoidactionPerformed(ActionEvente)Controllercontroller= durnDesc.addActionListener(newActionListener()publicvoidactionPerformed(ActionEvente)Controllercontroller= groupBy.addActionListener(newActionListener()publicvoidactionPerformed(ActionEvente)Controllercontroller= search.addActionListener(newActionListener()publicvoidactionPerformed(ActionEvente)Controllercontroller= publicvoidupdateOnlineNumbers(intonline) *****:publicvoidupdateView(Stringdata,inttype) privatevoidupdateButton(inttype)for(inti=0;i<buttons.length;i++)JButtonbtn=btn.setBorder(newif(i==type)btn.setBorder(new publicStringgetSearchKey()return 將DMSV3Controller數(shù)組重構(gòu)為集合,即將原有采用數(shù)組的在重構(gòu)時(shí)更換為注:重構(gòu)時(shí),業(yè)務(wù)對象類型LogData和LogRec不需要重新構(gòu)建,從DMSV2中過來即可。* publicclassController{*publicfinalstaticintDYNAMIC=*publicfinalstaticintDURN_ASC=*publicfinalstaticintDURN_DESC=*publicfinalstaticintGROUP_BY=*publicfinalstaticintSEARCH=*日志數(shù)據(jù)源對象的,需要到具體數(shù)據(jù)源對privateLogDataSource*顯示數(shù)據(jù)的狀態(tài):包括5privateint/**全部匹配的成對的Log記錄privateList<LogRec>matched=new/**已經(jīng)登錄沒有登出的log記錄,也就是不配對 記錄privateList<LogData>loginLogs=new/**定時(shí)器,用于定時(shí)獲取日志數(shù)據(jù)源數(shù)據(jù).privateTimertimer; privatestaticControllerinstance=newController();privateController()}publicstaticControllergetInstance()return publicvoidsetLogDataSource(LogDataSourcelogDataSource)this.logDataSource=}***@param**publicList<LogData>parseLogData(byte[]logs)List<LogData>list=newfor(intoffset=0;offset<offset+=LogData.LOG_LENGTH)////LogDatalogData=newLogData(logs, return *匹配登錄記錄1)要匹配未登出的記錄2)要匹配現(xiàn)在的記錄**publicvoidmatchLogData(List<LogData>newLogs)//System.out.println("CallmatchlogdataList<LogData>list=newlist.addAll(loginLogs);//上次未匹配的for(LogDatalog:list)//if(log.getType()==LogData.USER_PROCESS)LogDatalogin=//LogReclogRec=matchLogOut(list,//if(logRec!=null)}else ***@param *@param *@return登出日志privateLogRecmatchLogOut(List<LogData>LogDatalogin)LogReclogRec=newfor(LogDatalog:list)if(logRec.match(log))return return }dynamic()}durnAsc()}durnDesc()}groupBy()}search()}DMSV3TimerTask利用定時(shí)器的計(jì)劃任務(wù)實(shí)現(xiàn)定時(shí)處理日志數(shù)據(jù),定時(shí)的進(jìn)行解析和匹配處理。定時(shí)器的設(shè)計(jì)采用了模板模式,定時(shí)器已經(jīng)將復(fù)雜的多線程處理封裝了,使用時(shí)候只需要繼續(xù)模板類TimerTask,實(shí)現(xiàn)run()方法就可以簡潔開啟計(jì)劃任務(wù)。如下代碼可以立即開啟每隔一執(zhí)行一次的計(jì)劃任務(wù):Timertimer=newtimer.schedule(new{publicvoidrun()},0,1000);在Controller類中增加方法startCatchLogs(在Controller中添加方法startCatchLogsshowDataList*publicvoidstartCatchLogs()finalDMSFramedmsFrame=if(timer!=null) timer=newtimer.schedule(newTimerTask()publicvoidrun()System.out.println("CatchlogsList<LogData>newLogs=//if(showType== },0,1000); ***@param*publicvoidshowDataList(List<LogRec>list)StringBuilderbuf=newfor(LogReclogRec:list)logRec.getDurations(),16));}DMSFramedmsFrame=dmsFrame.updateView(buf.toString(),} 12));buf.append(Utils.leftPad(logRec.getPid(),logRec.getLoginTimeString(),logRec.getLogoutTimeString(),*publicclassControllerpublicfinalstaticintDYNAMIC=publicfinalstaticintDURN_ASC=publicfinalstaticintDURN_DESC=publicfinalstaticintGROUP_BY=*publicfinalstaticintSEARCH=*日志數(shù)據(jù)源對象的,需要到具體數(shù)據(jù)源對privateLogDataSource*顯示數(shù)據(jù)的狀態(tài):包括5按privateint/**全部匹配的成對的Log記錄privateList<LogRec>matched=new/**已經(jīng)登錄沒有登出的log記錄,也就是不配對的記錄privateList<LogData>loginLogs=new/**定時(shí)器,用于定時(shí)獲取日志數(shù)據(jù)源數(shù)據(jù).privateTimerprivatestaticControllerinstance=newprivateController()}publicstaticControllergetInstance()return}publicvoidsetLogDataSource(LogDataSourcelogDataSource)this.logDataSource=}*publicvoidstartCatchLogs()finalDMSFramedmsFrame=if(timer!=null) timer=newtimer.schedule(newTimerTask()publicvoidrun()System.out.println("CatchlogsList<LogData>newLogs=//if(showType== },0,1000); ***@param publicvoidshowDataList(List<LogRec>list)StringBuilderbuf=newfor(LogReclogRec:list) 12)); logRec.getLoginTimeString(),logRec.getLogoutTimeString(),logRec.getDurations(),16));}DMSFramedmsFrame=dmsFrame.updateView(buf.toString(),}***@param** publicList<LogData>parseLogData(byte[]logs)List<LogData>list=newfor(intoffset=0;offset<offset+=LogData.LOG_LENGTH)////LogDatalogData=newLogData(logs,}return}*匹配登錄記錄1)要匹配未登出的記錄2)**publicvoidmatchLogData(List<LogData>newLogs)//System.out.println("CallmatchlogdataList<LogData>list=newlist.addAll(loginLogs);//上次未匹配的for(LogDatalog:list)//if(log.getType()==LogData.USER_PROCESS)LogDatalogin=//LogReclogRec=matchLogOut(list,//if(logRec!=null)}else ***@param *@param *@return登出日志privateLogRecmatchLogOut(List<LogData>LogDatalogin)LogReclogRec=newfor(LogDatalog:list)if(logRec.match(log))return return publicvoiddynamic()}publicvoiddurnAsc()}}voiddurnDesc()}groupBy()}search()}*@parampublicvoidshowGroupLogList(List<GroupLog>list){StringBuilderbuf=newStringBuilder();for(GroupLoglog:list){}DMSFramedmsFrame=*class{StringintpublicGroupLog(LogReclog)this.user=this.durn= publicbooleansameUser(LogReclogRec)return publicbooleanadd(LogReclogRec)(sameUser(logRec))this.durn+=return}lsereturn}} publicStringtoString()Utils.leftPad(user,12)Utils.leftPad("",8)Utils.leftPad("",22)Utils.leftPad("",22)Utils.leftPad(durn,10)Utils.leftPad("",}}DMSV3項(xiàng)目重構(gòu)指導(dǎo)publicvoiddynamic()publicvoiddurnAsc()publicvoiddurnDesc()publicvoidgroupBy()參考DMSV2能響應(yīng)方法實(shí)現(xiàn),但是對于DMSFrame對象的通過單例獲得*publicvoiddynamic()//TimershowType= *publicvoiddurnAsc()showType=List<LogRec>allnewCollections.sort(all,newComparator<LogRec>()publicintcompare(LogReco1,LogReco2)returno1.getDurations() *publicvoiddurnDesc()showType=List<LogRec>allnewCollections.sort(all,newComparator<LogRec>()publicintcompare(LogReco1,LogReco2)return-(o1.getDurations()}}*publicvoidgroupBy()showType=List<LogRec>allnewCollections.sort(all,newComparator<LogRec>()publicintcompare(LogReco1,LogReco2)return}GroupLoggroupLog=List<GroupLog>groupLogsnewfor(LogReclogRec:all)if(groupLog!=null&&{}}elsegroupLog=new}}添加DMSMain類,作為軟件的publicclassDMSMainpublicstaticvoidmain(String[]args)LogDataSourcelogDataSource=newControllercontroller=Controller.getInstance();*publicclassControllerpublicfinalstaticintDYNAMIC=publicfinalstaticintDURN_ASC=publicfinalstaticintDURN_DESC=*publicfinalstaticintGROUP_BY=*publicfinalstaticintSEARCH=*日志數(shù)據(jù)源對象的,需要到具體數(shù)據(jù)源對privateLogDataSource*顯示數(shù)據(jù)的狀態(tài):包括5按privateint/**全部匹配的成對的Log記錄privateList<LogRec>matched=new/**已經(jīng)登錄沒有登出的log記錄,也就是不配對的記錄privateList<LogData>loginLogs=new/**定時(shí)器,用于定時(shí)獲取日志數(shù)據(jù)源數(shù)據(jù).privateTimerprivatestaticControllerinstance=newprivateController()}publicstaticControllergetInstance()return}publicvoidsetLogDataSource(LogDataSourcelogDataSource) this.logDataSource= *publicvoidstartCatchLogs()finalDMSFramedmsFrame=if(timer!=null)}timer=newtimer.schedule(newTimerTask()publicvoidrun()System.out.println("CatchlogsList<LogData>newLogs=//if(showType==}},0,1000);}***@param publicvoidshowDataList(List<LogRec>list)StringBuilderbuf=newfor(LogReclogRec:list) 12)); logRec.getLoginTimeString(),logRec.getLogoutTimeString(),logRec.getDurations(),16));}DMSFramedmsFrame=dmsFrame.updateView(buf.toString(),}***@param** publicList<LogData>parseLogData(byte[]logs)List<LogData>list=newfor(intoffset=0;offset<offset+=LogData.LOG_LENGTH)////LogDatalogData=newLogData(logs,}return}*匹配登錄記錄1)要匹配未登出的記錄2)**publicpublicvoidmatchLogData(List<LogData>newLogs)//System.out.println("CallmatchlogdataList<LogData>list=newlist.addAll(loginLogs);//上次未匹配的for(LogDatalog:list)//if(log.getType()==LogData.USER_PROCESS)LogDatalogin=//LogReclogRec=matchLogOut(list,//if(logRec!=null)}else}}}}***@param *@param *@return登出日志privateLogRecmatchLogOut(List<LogData>LogDatalogin)LogReclogRec=newfor(LogDatalog:list)if(logRec.match(log))return}}return *publicvoiddynamic()//TimershowType= *publicvoiddurnAsc()showType=List<LogRec>allnewCollections.sort(all,newComparator<LogRec>()publicintcompare(LogReco1,LogReco2)returno1.getDurations() *publicvoiddurnDesc()showType=List<LogRec>allnewCollections.sort(all,newComparator<LogRec>()publicintcompare(LogReco1,LogReco2)return

溫馨提示

  • 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論