Observerpattern (Viewer mode)

Source: Internet
Author: User

Importjava.util.ArrayList;Importjava.util.List;/*** Viewer Mode *@authorTmac-j * Reaching to describe the observer pattern in the right way * divided into pull mode and push mode, here is not detailed*/ Public classObserverpattern {Interfaceiobserved{voidAddobserver (IObserver observer); voidRemoveobserver (IObserver observer); voidnotifyobservers (); }          Public classObservedImplementsiobserved{PrivateList<iobserver> observers =NewArraylist<iobserver>(); @Override Public voidAddobserver (IObserver observer) {OBSERVERS.ADD (Observer); } @Override Public voidRemoveobserver (IObserver observer) {OBSERVERS.REMOVE (Observer); } @Override Public voidnotifyobservers () { for(IObserver observer:observers) {observer.update (); }        }            }        Interfaceiobserver{voidupdate (); }         Public classObserverImplementsiobserver{@Override Public voidUpdate () {SYSTEM.OUT.PRINTLN ("Do update ..."); }            }         Public voidTest () {iobserved observed=Newobserved (); IObserver Observer=NewObserver ();        OBSERVED.ADDOBSERVER (Observer);    Observed.notifyobservers (); }    }

Observerpattern (Viewer mode)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.