• <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-06-04來源:作者:點擊數: 標簽:設計javaJAVAJava模式
    關鍵字: Java 設計模式 在進行排序的時候,排序修飾者并沒有改變它修飾的真實對象,而是通過了一個數組來保存列的位置。當其他對象向它請求特定行和列的數據的時候,它通過行的值作為數組的索引并返回數組中相應位置的值。通過這種方式,排序修飾者在不改變
    關鍵字: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

    老湿亚洲永久精品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>