Design Pattern-Observer Pattern

Source: Internet
Author: User

Observer mode, or observer mode. To put it bluntly, it is a one-to-many dependency between objects. When the State of an object changes, all its dependent persons will receive a notification and update it automatically.


We can define the following three interfaces:


Topic interface, that is, the observed topic object:


package observer;public interface Subject {public void registerObserver(Observer o);public void removeObserver(Observer o);public void notifyObservers();}



Observer interface. When the observer finds that the state of the topic object changes, it must update itself:


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


Display interface, the observed topic data needs to be displayed in some way:


package observer;public interface Display {public void display();}


For convenience, let's give an example. If we want to observe the stock market, then for the stock market, the computer or stock market announcement board of all investors is its observer, and the stock market itself is an object to be observed.


We can clearly define the following classes:


Stock market, that is, the theme object to be observed 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();}}


The 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);}}




Finally, the main class is:


package observer;public class Main {public static void main(String[] args){StockQuotation stockQuotation = new StockQuotation();BillBoard billBoard = new BillBoard(stockQuotation);stockQuotation.setValue(2099.9);billBoard.deleteThisDisplay();stockQuotation.setValue(2090.9);}}

We found that the main class calls
billBoard.deleteThisDisplay();

Then the announcement board billboard will no longer be the observer, and it will not receive any stock market.


This simple code implements the observer mode.


We can find that when a new type of observer appears, the topic Code does not need to be modified. The topic only sends notifications to all objects that implement the observer interface.


The loose coupling between the subject and the observer is the uniqueness of the observer mode.

Design Pattern-Observer Pattern

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.