Design Patterns---observer patterns

Source: Internet
Author: User

The observer pattern, or the so-called observer pattern. White, many dependencies between objects, when the state of an object changes. It all depends on who will be notified and update themselves proactively.


Then, we can define the following three kinds of interfaces:


The theme interface is the subject object of the observation:


Package Observer;public interface Subject {public void Registerobserver (Observer O);p ublic void Removeobserver (Observer o);p ublic void Notifyobservers ();}



An observer interface that updates itself when the Observer discovers that the subject object state has changed:


Package Observer;public interface Observer {public void update (double value);}


Displays the interface. The observed subject data needs to be shown in some way:


Package Observer;public interface Display {public void display ();}


For the sake of convenience, we give an example to explain. If we are to look at the stock market today, then for the stock market, all the shareholders of the computer or stock market bulletin Board is its observer, and the stock market itself is a subject of observation.


Then we can be very clear about the following categories of definitions:


The stock market class, which is the subject of real observation at this time:


Package Observer;import Java.util.arraylist;public class Stockquotation implements Subject{private arraylist< Observer> observers;private Double stockvalue;public stockquotation () {observers=new arraylist<observer> ();} public void Registerobserver (Observer o) {observers.add (o);} public void Removeobserver (Observer o) {int i=observers.indexof (o); if (i>=0) {observers.remove (i);}} public void Notifyobservers () {for (int i=0;i<observers.size (); ++i) {Observer observer= (Observer) observers.get (i) ; Observer.update (StockValue);}} public void valuechanged () {notifyobservers ();} public void SetValue (double stockvalue) {this.stockvalue=stockvalue;valuechanged ();}}


Bulletin board class. Used to display the stock market:


Package Observer;public class BillBoard implements Observer, Display{private double Stockvalue;private Subject Stockquotation;public BillBoard (Subject stockquotation) {this.stockquotation=stockquotation; Stockquotation.registerobserver (this);} public void display () {System.out.println ("Current stock quotation:" +stockvalue);} public void update (double stockvalue) {This.stockvalue=stockvalue;display ();} public void Deletethisdisplay () {this.stockQuotation.removeObserver (this);}}




The last is the main class:


Package Observer;public class Main {public static void main (string[] args) {stockquotation stockquotation = new Stockquotat Ion (); BillBoard BillBoard = new BillBoard (stockquotation); Stockquotation.setvalue (2099.9); Billboard.deletethisdisplay (); Stockquotation.setvalue (2090.9);}}

We found that the main class called
Billboard.deletethisdisplay ();

Then the bulletin board Billboard was no longer an observer, and all it could not receive was any stock market.


This simple code implements the Observer pattern.


We can find out. When a new type of observer appears. The code for the theme does not need to be changed.

The subject does not care about anything else, just sends a notification to all objects that implement the Observer interface.


The loose coupling between the theme and the audience, it is unique in the observer pattern.

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Design Patterns---observer 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.