The observer pattern for Java design patterns

Source: Internet
Author: User

Observer pattern

The Observer pattern (sometimes called the Publish (publish)-subscribe (Subscribe) mode, model-view mode, source-listener (Listener) mode, or slave mode) is one of the software design patterns. In this mode, a target object manages all the observer objects that are dependent on it and proactively notifies when its own state changes. This is usually done by calling the methods provided by each observer. This pattern is often used to implement an event-handling system.

Observer Mode UML diagram

Observer Pattern Code
 PackageCom.roc.observer;/*** The person who notifies the abstract *@authorLIAOWP **/ Public InterfaceMessage { Public voidADD (Observer Observer);//Increase the viewer         Public voidDelete (Observer Observer);//Delete Viewer         Public voidnotice ();}
 PackageCom.roc.observer;Importjava.util.ArrayList;Importjava.util.List; Public classEmailmessageImplementsmessage{PrivateList<observer> observers=NewArraylist<observer>();  Public voidAdd (Observer Observer) {observers.add (Observer); }     Public voidDelete (Observer Observer) {observers.remove (Observer); }     Public voidnotice () { for(Observer observer:observers) {observer.notice (); }    }}
 package   Com.roc.observer;  /**   * abstract Observer *   @author   LIAOWP *  */ public  interface   Observer { public  void   notice (); }
 Package Com.roc.observer; /**  @author*/Publicclassimplements  observer{    @Override      Public void notice () {        System.out.println ("Observer1 received push");}        }
 PackageCom.roc.observer;/*** Viewer Mode *@authorLIAOWP **/ Public classClient { Public Static voidMain (string[] args) {Message message=NewEmailmessage ();//e-Mail specific notificationsMessage. ADD (NewObserver1 ());//the person to be notifiedMessage.notice (); }}
Observer Pattern Adaptation Scenario

1. Updates to an object state require other objects to be updated synchronously, and the number of other objects is dynamically variable.
2. Objects only need to notify their own updates to other objects without needing to know the details of other objects.

The observer pattern for Java design patterns

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.