• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
    • 軟件測試技術
    • 軟件測試博客
    • 軟件測試視頻
    • 開源軟件測試技術
    • 軟件測試論壇
    • 軟件測試沙龍
    • 軟件測試資料下載
    • 軟件測試雜志
    • 軟件測試人才招聘
      暫時沒有公告

    字號: | 推薦給好友 上一篇 | 下一篇

    Java設計模式之修飾模式篇(4)

    發布: 2008-6-04 14:40 | 作者: 不詳 | 來源: code365 | 查看: 46次 | 進入軟件測試論壇討論

    領測軟件測試網 關鍵字:Java設計模式

       在進行排序的時候,排序修飾者并沒有改變它修飾的真實對象,而是通過了一個數組來保存列的位置。當其他對象向它請求特定行和列的數據的時候,它通過行的值作為數組的索引并返回數組中相應位置的值。通過這種方式,排序修飾者在不改變表結構的前題下將排序功能疊加到了表結構上。TableSortDecorator同時還實現了TableModelListener接口并將自己注冊為一個監聽者。當真實對象,也就是原有的表對象發出一個表更改的事件后,修飾者將在數組中重新對行的位置進行排列,相應的代碼在tableChanged()方法中。還需要注意的是TableSortDecorator有11個公有方法,其中9個方法會被傳遞給真實對象。

         對排序修飾者的進一步改進

     上面的排序修飾者可以給任何的表模型增加排序功能。但是TableSortDecorator類的代碼重用性能并不是很好,這是因為它實現了兩個不應該由它實現的功能:第一個功能是將方法調用傳遞給真實對象,這是由于其他的表模型修飾者也會使用完全相同的代碼,由于該功能的普適性,它應該被移到類層次中較高的層次上;第二個是排序,在上面的例子中使用的是冒泡排序法,而排序的算法在類層次中是非常特殊的部分,因此需要被移到較低的層次上。圖8展示了根據上面兩點意見修改后的排序修飾者的類圖。

         圖8 經過修改后的排序修飾者的類圖

          

      經過修改后TableSortDecorator被分解成三個部分:

    · TableModelDecorator:實現了TableModel接口,將方法調用傳遞給真實對象。

    · TableSortDecorator:繼承了TableModelDecorator接口,增加了一個抽象方法sort()。

    · TableBubbleSortDecorator:繼承了TableSortDecorator接口并實現了冒泡排序。

      通過分解TableSortDecorator,我們可以重用將方法調用傳遞給真實對象的代碼。將TableModelDecorator中的代碼封裝起來使我們很容易對表模型添加其它的修飾者,例如過濾修飾者(TableFilterDecorator,)。抽象類TableSortDecorator將sort()方法的實現推遲到該類的子類中實現,因此可以在子類中實現不同的排序算法。下面是這些類的代碼:

      // TableModelDecorator.java
      import javax.swing.table.TableModel;
      import javax.swing.event.TableModelListener;
      // TableModelDecorator繼承了TableModelListener。
      // 當表模型發生變化的時候,會調用tableChanged()方法。
      // 該方法在抽象類中沒有實現,而是在繼承該類的子類中實現。
      public abstract class TableModelDecorator
       implements TableModel, TableModelListener {
       public TableModelDecorator(TableModel model) {
       this.realModel = model;
       realModel.addTableModelListener(this);
      }
        // 下面的九個方法定義在TableModel接口中。
      public void addTableModelListener(TableModelListener l) {
       realModel.addTableModelListener(l);
      }
      public Class getColumnClass(int columnIndex) {
       return realModel.getColumnClass(columnIndex);
      }
      public int getColumnCount() {
       return realModel.getColumnCount();
      }
      public String getColumnName(int columnIndex) {
       return realModel.getColumnName(columnIndex);
      }
      public int getRowCount() {
       return realModel.getRowCount();
      }
      public Object getValueAt(int rowIndex, int columnIndex) {
       return realModel.getValueAt(rowIndex, columnIndex);
      }
      public boolean isCellEditable(int rowIndex, int columnIndex) {
       return realModel.isCellEditable(rowIndex, columnIndex);
      }
      public void removeTableModelListener(TableModelListener l) {
       realModel.removeTableModelListener(l);
      }
      public void setValueAt(Object aValue,
       int rowIndex, int columnIndex) {
       realModel.setValueAt(aValue, rowIndex, columnIndex);
      }

    延伸閱讀

    文章來源于領測軟件測試網 http://www.kjueaiud.com/

    TAG: java JAVA Java 模式 設計

    31/3123>

    關于領測軟件測試網 | 領測軟件測試網合作伙伴 | 廣告服務 | 投稿指南 | 聯系我們 | 網站地圖 | 友情鏈接
    版權所有(C) 2003-2010 TestAge(領測軟件測試網)|領測國際科技(北京)有限公司|軟件測試工程師培訓網 All Rights Reserved
    北京市海淀區中關村南大街9號北京理工科技大廈1402室 京ICP備2023014753號-2
    技術支持和業務聯系:info@testage.com.cn 電話:010-51297073

    軟件測試 | 領測國際ISTQBISTQB官網TMMiTMMi認證國際軟件測試工程師認證領測軟件測試網

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>