afunms

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

          bridge pattern重構report

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

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

          橋梁模式( Bridge Pattern )才是最適合的。橋梁模式的用意是 " 將抽象化( Abstraction )與實現化( Implementation )脫耦,使得二者可以獨立地變化 " 。( 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中調用,生成報表

          修正抽象化

          ?

          ?

          ?

          ImplementorReport

          所有數據報表的父類

          實現化(Implementor)

          NetworkPerformanceReport

          網絡設備性能報表

          具體實現化

          (Concrete Implementor)

          ServerPerformanceReport

          服務器性能報表

          具體實現化

          PortTrafficReport

          接口流量報表

          具體實現化

          AbstractionReport的功能是生成pdfexcel文件,而ImplementorReport的功能是收集數據,為生成報表作準備。

          AbstractionReport的子類相對固定,因為我們現在只要實現三個功能,當然,如果以后還想再實現其他功能,比如生成txt報表或html報表,我們還能再擴展,增加兩個類TxtReportHtmlReport即可實現。

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

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

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

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

          生成excel報表文件:

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

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

          ?

          生成“服務器性能報表pdf報表文件:

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

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

          ?

          生成“服務器性能報表excel報表文件:

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

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




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


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


          網站導航:
           

          My Links

          News

          留言簿(18)

          隨筆檔案

          相冊

          搜索

          最新評論

          閱讀排行榜

          主站蜘蛛池模板: 凌源市| 嘉禾县| 保亭| 绍兴市| 全州县| 稻城县| 天峨县| 文昌市| 天柱县| 文成县| 遂昌县| 徐闻县| 宜川县| 囊谦县| 榆社县| 通榆县| 柳河县| 霞浦县| 固阳县| 孟连| 清河县| 台湾省| 承德县| 定西市| 禄丰县| 章丘市| 涿州市| 英德市| 苏尼特右旗| 文水县| 若羌县| 新野县| 喀什市| 乌兰浩特市| 易门县| 垣曲县| 穆棱市| 伊川县| 柳州市| 玉田县| 万荣县|