Example of Weather Information Retrieval Based on Observer Mode

Source: Internet
Author: User

Observer mode definition:

The observer mode is a software design mode. It is also called the Model-View Mode, source-listener mode, or the dependent mode. The observer mode defines a one-to-many dependency, so that one or more observer objects can monitor a topic object, such changes in the status of a topic object can notify all observer objects dependent on this object so that these observer objects can be updated automatically.
Observer mode structure:

The observer mode has the following roles:

Abstract (subject) topic role: The topic role stores reference of all observer objects in a list. Each topic can have any number of observers. A topic provides an interface to add or remove an observer object. A Topic role is also called an abstract observable role;

Abstract observer role:Define an interface for all specific observers and update themselves when notified;

The specific topic (concretesubject) role:Saves the internal state that is useful to a specific observer object. When the internal state changes, a notification is sent to the observer. The specific topic role is also called the specific observer role;

Concreteobserver role:Saves a reference pointing to a specific topic object and a status that matches the topic status. The specific observer role implements the update interface required by the abstract observer role, so that the status of the observer role is the same as that of the topic.

Specific instance:

Observer interface:

/*** Observer interface */public interface observer {void Update (subject );}

Specific observer:

Public class concretobserver implements observer {private string observername; private string observerstate; Public void setobservername (string observername) {This. observername = observername;} @ override public void Update (subject) {observerstate = (concretesubject) subject ). getweathercontent (); system. out. println (observername + "received" + observerstate );}}

Abstract observer:

public class Subject {  private ArrayList<Observer> observers = new ArrayList<Observer>();    public void attach(Observer observer){        observers.add(observer);    }    public void detach(Observer observer){        observers.remove(observer);    }    protected  void notifyObservers(){        for (Observer observer:observers){            observer.update(this);        }    }}

Specifically observed:

public class ConcreteSubject extends  Subject {    private String weatherContent;    public String getWeatherContent() {        return weatherContent;    }    public void setWeatherContent(String weatherContent) {        this.weatherContent = weatherContent;        this.notifyObservers();    }}

Client call:

Public class client {public static void main (string [] ARGs) {concretesubject subject = new concretesubject (); concretobserver O1 = new concretobserver (); o1.setobservername ("Vivi"); Subject. attach (O1); concretobserver O2 = new concretobserver (); o2.setobservername ("Jimi"); Subject. attach (O2); Subject. setweathercontent ("sunny day, temperature 28 Breeze ");}}

Advantages and disadvantages of observer mode:

Advantages:

(1) The observer mode separates the presentation layer from the data logic layer, defines a stable message update transfer mechanism, and abstracts the update interface, this allows different presentation layers to act as specific observer roles.
(2) The observer mode establishes an abstract coupling between the observer and the observer. To observe a target, you only need to maintain a set of abstract observers without understanding the specific observer. Because the observation targets are not closely coupled with the observer, they can belong to different abstract layers.
(3) The observer mode supports broadcast communication. The observer object will send notifications to all registered observer objects, simplifying the design of one or more systems.
(4) The observer mode meets the requirements of the "Open and closed principle". adding a new observer does not need to modify the original system code. When there is no association between the observer and the observed object, it is also convenient to add new observation targets.

Disadvantages:

(1) If a target object has many direct and indirect observers, it takes a lot of time to notify all the observers.
(2) If there is a circular dependency between the observer and the observation target, the observation target triggers a circular call between them, which may cause the system to crash.
(3) The observer mode does not have a mechanism to let the observer know how the observed target object changes, but only knows that the observed target has changed.

GitHub Source: https://github.com/jingdq/ObserverPattern/tree/master/src/com/jing

 

Example of Weather Information Retrieval Based on 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.