afunms

          My Software,My Dream—Forge a more perfect NMS product.

          bridge pattern重構(gòu)report

          我們要實(shí)現(xiàn)三個功能:在 jsp 頁面上生成報表(包括一個表和一個圖)、生成 pdf 報表和 excel 報表。

          原來的代碼雖然已經(jīng)實(shí)現(xiàn)了這些功能,但每個類的功能不明確,而且存在大量重復(fù)代碼。于是想到用設(shè)計(jì)模式來解決這個問題。起初想用裝飾模式( Decorator Pattern ),但經(jīng)過分析,發(fā)現(xiàn)裝飾模式適合于那種需要把多個功能動態(tài)組合在一起的情況。但對于一個 report ,我們并不需要它同時能生成 pdf excel (即不是多個功能的組合)。

          橋梁模式( Bridge Pattern )才是最適合的。橋梁模式的用意是 " 將抽象化( Abstraction )與實(shí)現(xiàn)化( Implementation )脫耦,使得二者可以獨(dú)立地變化 " 。( According to GoF, the Bridge Pattern is intended to "Decouple an abstraction from its implementation so that the two can vary independently"

          類圖如下:
          Snap1.jpg

          ?

          類名

          功能

          角色

          AbstractionReport

          所有功能報表的父類

          抽象化(Abstraction)

          PdfReport

          生成pdf報表文件

          修正抽象化

          (Refined Abstraction)

          ExcelReport

          生成excel報表文件

          修正抽象化

          JspReport

          jsp中調(diào)用,生成報表

          修正抽象化

          ?

          ?

          ?

          ImplementorReport

          所有數(shù)據(jù)報表的父類

          實(shí)現(xiàn)化(Implementor)

          NetworkPerformanceReport

          網(wǎng)絡(luò)設(shè)備性能報表

          具體實(shí)現(xiàn)化

          (Concrete Implementor)

          ServerPerformanceReport

          服務(wù)器性能報表

          具體實(shí)現(xiàn)化

          PortTrafficReport

          接口流量報表

          具體實(shí)現(xiàn)化

          AbstractionReport的功能是生成pdfexcel文件,而ImplementorReport的功能是收集數(shù)據(jù),為生成報表作準(zhǔn)備。

          AbstractionReport的子類相對固定,因?yàn)槲覀儸F(xiàn)在只要實(shí)現(xiàn)三個功能,當(dāng)然,如果以后還想再實(shí)現(xiàn)其他功能,比如生成txt報表或html報表,我們還能再擴(kuò)展,增加兩個類TxtReportHtmlReport即可實(shí)現(xiàn)。

          ImplementorReport的子類就比較多了,列出的只是其中三個。

          任意一個AbstractionReportImplementorReport的組合都能有不同的功能。有了以上的各個類,我們就能生成各種各樣的報表,比如我們要生成一個“網(wǎng)絡(luò)設(shè)備性能”的pdf報表文件,就這么寫:

          ?????? AbstractionReport report = new PdfReport(new NetworkPerformanceReport());???????

          ?????? report.createReport();

          生成excel報表文件:

          ?????? AbstractionReport report = new ExcelReport(new NetworkPerformanceReport());???????

          ?????? report.createReport();

          ?

          生成“服務(wù)器性能報表pdf報表文件:

          ?????? AbstractionReport report = new PdfReport(new ServerPerformanceReport());???????

          ?????? report.createReport();

          ?

          生成“服務(wù)器性能報表excel報表文件:

          ??? ??? AbstractionReport report = new ExcelReport(new ServerPerformanceReport());???????

          ?????? report.createReport();




          posted on 2006-11-22 10:09 afunms 閱讀(201) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           

          My Links

          News

          留言簿(18)

          隨筆檔案

          相冊

          搜索

          最新評論

          閱讀排行榜

          主站蜘蛛池模板: 永昌县| 黎城县| 武宣县| 沂水县| 惠安县| 嘉鱼县| 崇礼县| 清河县| 临澧县| 泽库县| 宿迁市| 青铜峡市| 昌邑市| 天门市| 镇平县| 伊通| 元氏县| 综艺| 辛集市| 沙田区| 曲周县| 焉耆| 沭阳县| 南京市| 迭部县| 宁河县| 若尔盖县| 惠东县| 大渡口区| 永安市| 乾安县| 五峰| 安徽省| 邮箱| 霍邱县| 新竹市| 鹰潭市| 内乡县| 保山市| 栾城县| 鹤岗市|