• <label id="pxtpz"><meter id="pxtpz"></meter></label>
      1. <span id="pxtpz"><optgroup id="pxtpz"></optgroup></span>

        當前位置:雨林木風下載站 > 技術開發(fā)教程 > 詳細頁面

        Java Q&A: 運用Observer模式(轉)

        Java Q&A: 運用Observer模式(轉)

        更新時間:2022-05-06 文章作者:未知 信息來源:網絡 閱讀次數:

        Java Q&A: 使用Observer模式

        A:我想在自己的Java程序中使用Observer模式。基于此,能否提供某些示例代碼來演示如何去做?

        Q:正如面向對象編程有助于代碼復用一樣,設計模式可以促進設計復用。確實,設計模式可以讓你復用那些正確的、成熟的設計。但近來,批評設計模式的聲音越來越多。批評者指出,缺乏經驗的開發(fā)者會很容易地掉進 "模式陷阱" 之中。

        模式陷阱讓缺乏經驗的開發(fā)者迷失了方向。因而,他們在處理問題時不是去尋找可能存在的最好方案,而把最終目標集中在盡可能地實現(xiàn)更多的設計模式之上。在一些人看來,使用設計模式好象必然會帶來良好的設計。按照這個邏輯,只要大量使用設計模式,就必然為你產生一個優(yōu)秀的設計!然而現(xiàn)實中,這一觀點卻導致出許多毫無意義的設計--即使這個設計采用了多個設計模式。看來很遺憾,設計模式并不能保證良好的設計。

        要想在設計中正確地運用一個設計模式,必須確保三個條件:

        1. 弄清你的問題

        2. 了解這個模式

        3. 理解這個模式如何解決你的問題

        首先,最重要的是條件1。如果不能完全地弄清你要解決的問題,何談運用模式?

        還要知道條件2:必須完全了解你想運用的模式。不了解它怎么能運用它?更重要的是,連一個模式做什么都不知道,又如何想到去用它?

        最后一點,如果你不能清楚地說出模式將怎樣解決你的問題(為什么這個模式合適),那還是放棄它。僅僅為了用模式本身而去用它,就會掉進模式陷阱之中。

        我并不是在說提這個問題的讀者也一定會掉進模式陷阱。但從提問的表達上來看,它很容易誤導一些開發(fā)者去理解設計模式。我對這個提問的理解是,這位讀者應該清楚自己需要解決的問題,也了解Observer模式,他/她只是不知道如何用Java來實現(xiàn)。

        在給出一個Java示例之前,為了有助于其他讀者也能理解,先簡要介紹一下Observer模式。

        簡單來說,Observer模式讓一個對象(觀察者,Observer)去監(jiān)視另一個對象(目標,Subject);它使得目標和觀察者之間建立一種 "發(fā)布--訂閱"(publish-subscribe )的關系。通過Observer模式,觀察者可以向目標登記,表明自己要從目標接收事件。目標需要向觀察者通知事件時,只是簡單地將事件發(fā)給每一個觀察者。

        例如,有一個基于某種數據模型的電子表格。只要數據模型發(fā)生變化,電子表格就需要更新表格單元以及內嵌的圖表。這個例子中,目標是數據模型,觀察者是表格單元和圖表。當觀察者接收到數據模型已經變化的通知時,它們就更新自己。

        Observer模式的好處是:它解除了觀察者和目標之間的耦合關系。目標不需要知道它的觀察者的任何信息。相反,目標只是允許觀察者訂閱事件。當目標產生一個事件時,它簡單地將事件傳給每一個觀察者。

        看看下面的Java示例:

        public interface Subject {
        public void addObserver( Observer o );
        public void removeObserver( Observer o );
        }

        上面的代碼中,Subject接口定義了兩個方法(method),每個Subject都必須實現(xiàn)它們,以使Observer可以在Subject中增加或刪除自身。

        public interface Observer {
        public void update( Subject o );
        }

        Observer接口(如上)列出了一個方法(method),每個Observer都必須實現(xiàn)它,以使Subject可以發(fā)送更新消息給Observer。

        下面看看Subject的一個簡單的實現(xiàn)--IntegerDataBag:

        import java.util.ArrayList;
        import java.util.Iterator;

        public class IntegerDataBag implements Subject {

        private ArrayList list = new ArrayList();
        private ArrayList observers = new ArrayList();

        public void add( Integer i ) {
        list.add( i );
        notifyObservers();
        }

        public Iterator iterator() {
        return list.iterator();
        }

        public Integer remove( int index ) {
        if( index < list.size() ) {
        Integer i = (Integer) list.remove( index );
        notifyObservers();
        return i;
        }
        return null;
        }

        public void addObserver( Observer o ) {
        observers.add( o );
        }

        public void removeObserver( Observer o ) {
        observers.remove( o );
        }

        private void notifyObservers() {
        // loop through and notify each observer
        Iterator i = observers.iterator();
        while( i.hasNext() ) {
        Observer o = ( Observer ) i.next();
        o.update( this );
        }
        }
        }

        IntegerDataBag適用于使用Integer的場合。IntegerDataBag也允許Observer增加和刪除它們自身。

        再看看兩個Observer的實現(xiàn)--IntegerAdder和IntegerPrinter:

        import java.util.Iterator;

        public class IntegerAdder implements Observer {

        private IntegerDataBag bag;

        public IntegerAdder( IntegerDataBag bag ) {
        this.bag = bag; 
        bag.addObserver( this );
        }

        public void update( Subject o ) {
        if( o == bag ) {
        System.out.println( "The contents of the IntegerDataBag have changed." );
        int counter = 0;
        Iterator i = bag.iterator();
        while( i.hasNext() ) {
        Integer integer = ( Integer ) i.next();
        counter+=integer.intValue();
        }
        System.out.println( "The new sum of the integers is: " + counter );
        }
        }

        }

        import java.util.Iterator;

        public class IntegerPrinter implements Observer {

        private IntegerDataBag bag;

        public IntegerPrinter( IntegerDataBag bag ) {
        this.bag = bag; 
        bag.addObserver( this );
        }

        public void update( Subject o ) {
        if( o == bag ) {
        System.out.println( "The contents of the IntegerDataBag have changed." );
        System.out.println( "The new contents of the IntegerDataBag contains:" );
        Iterator i = bag.iterator();
        while( i.hasNext() ) {
        System.out.println( i.next() );
        }
        }
        }

        }

        IntegerAdder和IntegerPrinter將自己作為觀察者增加到IntegerDataBag。當IntegerAdder接收到一條更新消息時,它先統(tǒng)計bag中的總數,然后顯示結果。同樣,當IntegerPrinter接收到一條更新消息時,它打印出bag中的Interger。

        下面是一個簡單的main(),它使用了上面的幾個類:

        public class Driver {
        public static void main( String [] args ) {
        Integer i1 = new Integer( 1 ); Integer i2 = new Integer( 2 );
        Integer i3 = new Integer( 3 ); Integer i4 = new Integer( 4 );
        Integer i5 = new Integer( 5 ); Integer i6 = new Integer( 6 );
        Integer i7 = new Integer( 7 ); Integer i8 = new Integer( 8 );
        Integer i9 = new Integer( 9 );

        IntegerDataBag bag = new IntegerDataBag();
        bag.add( i1 ); bag.add( i2 ); bag.add( i3 ); bag.add( i4 );
        bag.add( i5 ); bag.add( i6 ); bag.add( i7 ); bag.add( i8 );

        IntegerAdder adder = new IntegerAdder( bag );
        IntegerPrinter printer = new IntegerPrinter( bag );

        // adder and printer add themselves to the bag

        System.out.println( "About to add another integer to the bag:" );
        bag.add( i9 );
        System.out.println("");
        System.out.println("About to remove an integer from the bag:");
        bag.remove( 0 );
        }
        }

        運行main,你將看到:

        c:\javaworld\java Driver
        About to add another integer to the bag:
        The contents of the IntegerDataBag have changed.
        The new sum of the intergers is: 45
        The contents of the IntegerDataBag have changed.
        The new contents of the IntegerDataBag contains:
        1
        2
        3
        4
        5
        6
        7
        8
        9

        About to remove an integer from the bag:
        The contents of the IntegerDataBag have changed.
        The new sum of the intergers is: 44
        The contents of the IntegerDataBag have changed.
        The new contents of the IntegerDataBag contains:
        2
        3
        4
        5
        6
        7
        8
        9

        IntegerDataBag/IntegerAdder/IntegerPrinter是應用Observer模式的一個很簡單的例子。Java本身有大量使用Observer模式的例子:AWT/Swing事件模型,還有java.util.Observer和java.util.Observable接口等,都是很好的例子。

        溫馨提示:喜歡本站的話,請收藏一下本站!

        本類教程下載

        系統(tǒng)下載排行

        主站蜘蛛池模板: 久久99国产乱子伦精品免费| 免费人人潮人人爽一区二区| 国产成人精品久久免费动漫| 人人狠狠综合久久亚洲| 在线永久免费观看黄网站| EEUSS影院WWW在线观看免费| 亚洲一级毛片视频| 色吊丝永久在线观看最新免费 | 色婷婷六月亚洲婷婷丁香| 四虎精品视频在线永久免费观看| 亚洲国产精品午夜电影| 免费a级毛片在线观看| 成人午夜视频免费| 免费国产成人午夜在线观看| 美女被吸屁股免费网站| 男人的天堂av亚洲一区2区| 亚洲午夜免费视频| 亚洲爆乳无码一区二区三区| 日本高清免费中文字幕不卡| 免费播放一区二区三区| a视频免费在线观看| 免费一级毛片在线播放视频免费观看永久 | 免费国产成人午夜电影| 免费一区二区三区| www成人免费视频| 久久国产福利免费| 美景之屋4在线未删减免费 | 在线免费观看国产| 大地资源在线资源免费观看| 免费人成视频在线观看免费| 国产成人精品日本亚洲18图| 亚洲国色天香视频| 国产成人亚洲精品青草天美| 亚洲AV无码精品无码麻豆| 亚洲av日韩av天堂影片精品| 99人中文字幕亚洲区| 亚洲色婷婷一区二区三区| 久久久久亚洲精品天堂| 亚洲美女大bbbbbbbbb| 亚洲一区二区三区播放在线| 亚洲小说图区综合在线|