




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
JavaSE01DMSV3Controller單例模式應(yīng)用與測DMSV3DMSFrame的重構(gòu)DMSV3TimerTask模板模式應(yīng)DMSV3項目重構(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實例的方式,更新為通過單例方法獲得控制器實例如,按鈕方法在DMSV2中采用如下代碼實現(xiàn):list.addActionListener(newActionListener()publicvoidactionPerformed(ActionEvent更新成,控制器實例通過單例獲取方法獲得list.addActionListener(newActionListener()publicvoidactionPerformed(ActionEvent{Controllercontroller在DMSV2DMSFrame類的基礎(chǔ)上重構(gòu),去除控制器實例變量的controller,如果需要用到控制器實例,就更新為通過控制器單例獲取方法得到。OnlinePanel類無需重構(gòu),與DMSV2中的代碼一致(過來即可)。*重構(gòu)的DMSV3DMSFrame任何對Controller對象 *publicclassDMSFrameextendsJFrameprivateOnlinePanelonlinePanel;privateJTextArealistArea;//privateJTextFieldsearchKey;// privateJScrollPanelistPane;//列表顯示區(qū)域的滾動面板*字體顏色255,255,255*未選中按鈕背景33,177,169#*未選中按鈕邊框 *選中按鈕背景18,117,112#*選中按鈕邊框32,170,162#*搜索邊框169,251,246#*進度條背景0,0,0*進度條顏色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 進程 +"登錄時 +"登出時 +"持續(xù)時 登錄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);//默認顯示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)時更換為注:重構(gòu)時,業(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ù)據(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利用定時器的計劃任務(wù)實現(xiàn)定時處理日志數(shù)據(jù),定時的進行解析和匹配處理。定時器的設(shè)計采用了模板模式,定時器已經(jīng)將復雜的多線程處理封裝了,使用時候只需要繼續(xù)模板類TimerTask,實現(xiàn)run()方法就可以簡潔開啟計劃任務(wù)。如下代碼可以立即開啟每隔一執(zhí)行一次的計劃任務(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ù)據(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項目重構(gòu)指導publicvoiddynamic()publicvoiddurnAsc()publicvoiddurnDesc()publicvoidgroupBy()參考DMSV2能響應(yīng)方法實現(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ù)據(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)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年廣東省清遠市清城區(qū)中考一?;瘜W試題(含答案)
- 濟南工程職業(yè)技術(shù)學院《藏藥藥物分析學》2023-2024學年第二學期期末試卷
- 天津財經(jīng)大學珠江學院《傳統(tǒng)文化藝術(shù)》2023-2024學年第二學期期末試卷
- 商丘職業(yè)技術(shù)學院《互聯(lián)網(wǎng)醫(yī)療》2023-2024學年第一學期期末試卷
- 豫章師范學院《物聯(lián)網(wǎng)控制》2023-2024學年第二學期期末試卷
- 浙江同濟科技職業(yè)學院《書法鑒賞》2023-2024學年第一學期期末試卷
- 江蘇省揚州市安宜高中、汜水高中聯(lián)考2025屆高三下學期第18周物理試題考試試題含解析
- 四川省宣漢縣2025屆中考化學試題原創(chuàng)模擬卷(六)含解析
- 遼寧省丹東市五校協(xié)作體2025年高三第一次教學質(zhì)置檢測試題語文試題含解析
- 欽州幼兒師范高等??茖W?!断懔舷憔a(chǎn)工藝學》2023-2024學年第二學期期末試卷
- 九年級上冊歷史知識點復習課件(部編版)
- 中醫(yī)診所標準規(guī)章核心制度
- 行政事業(yè)單位公務(wù)出差審批單
- 2022年四川省阿壩州中考物理真題及答案
- 小徑分岔的花園
- 超星爾雅學習通《孫子兵法》與執(zhí)政藝術(shù)(浙江大學)網(wǎng)課章節(jié)測試答案
- 《叩問師魂》觀后感3篇
- 出版專業(yè)基礎(chǔ)知識中級
- GB/T 9575-2013橡膠和塑料軟管軟管規(guī)格和最大最小內(nèi)徑及切割長度公差
- GB/T 9163-2001關(guān)節(jié)軸承向心關(guān)節(jié)軸承
- GB/T 4857.19-1992包裝運輸包裝件流通試驗信息記錄
評論
0/150
提交評論