2. Observer mode

Source: Internet
Author: User

Observer pattern Definition: defines a one-to-many dependency between objects, so that when an object state changes, all its dependents are notified and updated automatically. Personal understanding: When the subject changes, the changes are notified in a timely manner to each observer (subscriber) Observer pattern design method that subscribes to the subject, allowing for a loose coupling between the subject and the observer, adapting to change.
  • The Observer pattern defines a one-to-many relationship between objects
  • Topic using a common interface to update the Observer
  • A loosely coupled approach between the subject and the Observer
  • Subject can be used by the observer mode with push or pull data
  • Cannot rely on a specific notification order when there are multiple observers
  • Java has a variety of observer patterns, such as universal java.util.Observable
  • Swing uses observer patterns heavily
  • This mode is also used in JavaBeans, RMI applications.
Define the topic interface, provide a topic must implement three methods: registration, deletion, notification

PublicInterface Subject { void registerobserver (Observer Observer); void removeobserver (Observer Observer); void notifyobservers ();}

Achieve the specific weather theme: Implement subject interface, implement registration, deletion, notification method; Define the Observer collection used for observer registration; Define the Setmeasurements method for updating weather data. The Measurementschanaged method is called by default within the method (call Notifyobservers, loop Observer collection, call Update method for each observer)
 Public classWeatherdatasubjectImplementssubject{List<Observer>observers; Private floattemp; Private floathumidity; Private floatpressure;  PublicWeatherdatasubject () {observers=NewArraylist<>(); }     Public voidregisterobserver (Observer Observer) {observers.add (Observer); }     Public voidremoveobserver (Observer Observer) {observers.remove (Observer); }     Public voidnotifyobservers () { for(Observer observer:observers) {observer.update (temp, humidity, pressure); }    }     Public voidmeasurementschanaged () {notifyobservers (); }     Public voidSetmeasurements (floatTempfloatHumidity,floatpressure) {         This. temp =temp;  This. Humidity =humidity;  This. Pressure =pressure;    Measurementschanaged (); }}

Define the Observer interface: The Update method that each observer must implement to be called by the theme to implement the notification function
 Public Interface Observer {    void update (float temp,float humidity,float  pressure);}

Define the viewer display method:
 Public Interface displayelement {    void  display ();}

The following three observers implement the observer interface, the Displayelement display interface: In the Update method, the display method is called by default, and the personalized display mode is defined in the specific observer.
 Public classCurrentconditionsdisplayImplementsobserver,displayelement {Private floattemp; Private floathumidity; Private floatpressure; @Override Public voiddisplay () {System.out.println ("Currentconditionsdisplay:temp=" +temp+ "humidity=" +humidity+ ";p ressure=" +pressure); } @Override Public voidUpdatefloatTempfloatHumidity,floatpressure) {         This. temp =temp;  This. Humidity =humidity;  This. Pressure =pressure;    Display (); }} Public classStatisticsdisplayImplementsobserver,displayelement {Private floattemp; Private floathumidity; Private floatpressure; @Override Public voiddisplay () {System.out.println ("Statisticsdisplay:temp=" +temp+ "humidity=" +humidity+ ";p ressure=" +pressure); } @Override Public voidUpdatefloatTempfloatHumidity,floatpressure) {         This. temp =temp;  This. Humidity =humidity;  This. Pressure =pressure;    Display (); }} Public classThirdpartydisplayImplementsobserver,displayelement {Private floattemp; Private floathumidity; Private floatpressure; @Override Public voiddisplay () {System.out.println ("Thirdpartydisplay:temp=" +temp+ "humidity=" +humidity+ ";p ressure=" +pressure); } @Override Public voidUpdatefloatTempfloatHumidity,floatpressure) {         This. temp =temp;  This. Humidity =humidity;  This. Pressure =pressure;    Display (); }}

Test class:
 Public classTest { Public Static voidMain (string[] args) {//observersCurrentconditionsdisplay Currentconditionsdisplay =NewCurrentconditionsdisplay (); Statisticsdisplay Statisticsdisplay=NewStatisticsdisplay (); Thirdpartydisplay Thirdpartydisplay=NewThirdpartydisplay (); //ThemeWeatherdatasubject subject =NewWeatherdatasubject (); //registering the viewer with the subjectSubject.registerobserver (Currentconditionsdisplay);        Subject.registerobserver (Statisticsdisplay);        Subject.registerobserver (Thirdpartydisplay); Subject.setmeasurements (1,1,1); Subject.setmeasurements (2,3,4); }}

Output Result:

currentconditionsdisplay:temp=1.0;humidity=1.0;pressure=1.0
statisticsdisplay:temp=1.0;humidity=1.0;pressure=1.0
thirdpartydisplay:temp=1.0;humidity=1.0;pressure=1.0


currentconditionsdisplay:temp=2.0;humidity=3.0;pressure=4.0
statisticsdisplay:temp=2.0;humidity=3.0;pressure=4.0
thirdpartydisplay:temp=2.0;humidity=3.0;pressure=4.0

2. Observer 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.