Android裡的觀察者模式應用

來源:互聯網
上載者:User

Android裡的觀察者模式應用

例子:Browser裡有許多Tab,現在比較流行的標籤頁瀏覽。每當Browser的BrowserSettings(公用)發生變化時,會對每一個
Browser的Tab馬上有效。在這裡TAb是觀察者,BrowserSettings是被觀察者。

class BrowserSettings extends Observable {    /**     * Add a WebSettings object to the list of observers that will be updated     * when update() is called.     *     * @param s A WebSettings object that is strictly tied to the life of a     *            WebView.     */    public Observer addObserver(WebSettings s) {        Observer old = mWebSettingsToObservers.get(s);        if (old != null) {            super.deleteObserver(old);        }        Observer o = new Observer(s);        mWebSettingsToObservers.put(s, o);        super.addObserver(o);        return o;    }    /**     * Delete the given WebSettings observer from the list of observers.     * @param s The WebSettings object to be deleted.     */    public void deleteObserver(WebSettings s) {        Observer o = mWebSettingsToObservers.get(s);        if (o != null) {            mWebSettingsToObservers.remove(s);            super.deleteObserver(o);        }    }    /*     * Package level method for obtaining a single app instance of the     * BrowserSettings.     */    /*package*/ static BrowserSettings getInstance() {        if (sSingleton == null ) {            sSingleton = new BrowserSettings();        }        return sSingleton;    }    void update() {        setChanged();        notifyObservers();    }    /*     * An observer wrapper for updating a WebSettings object with the new     * settings after a call to BrowserSettings.update().     */    static class Observer implements java.util.Observer {        // Private WebSettings object that will be updated.        private WebSettings mSettings;        Observer(WebSettings w) {            mSettings = w;        }        public void update(Observable o, Object arg) {    }    }}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.