JAVA Design Pattern Observer pattern

Source: Internet
Author: User

Use

Observer Mode (Observer)

The Observer pattern defines a one-to-many dependency that allows multiple observer objects to listen to a Subject object at the same time.
This subject object notifies all observer objects when the state changes, enabling them to automatically update themselves.

The Observer pattern is a Behavioral Patterns


structure

Figure-Viewer Pattern structure diagram

Subject: The subject class, which holds all observers who subscribe to this topic, the Observer's the quantity is arbitrary。 Defined Add Observer ( Attach)And Delete Viewer (Detach)The interface.AbstractclassSubject {
protectedString name;
protectedString State;
protectedList<observer> observers =NewArraylist<observer> ();

PublicAbstractString getState ();
PublicAbstractvoidSetState (String state);
PublicAbstractvoidNotify ();

PublicSubject (String name) {
This. name = name;
}

PublicvoidAttach (Observer Observer) {
OBSERVERS.ADD (Observer);
}

Public voidDetach (Observer Observer) {
OBSERVERS.REMOVE (Observer);
}
}

Observer : The Observer class, which defines update interfaces (updates), Observer need to synchronize update information when Subject notifications are received.

AbstractclassObserver {
protectedString name;
protectedSubject Subject;
PublicObserver (String name, Subject Subject) {
This. name = name;
This. Subject = subject;
}
PublicAbstractvoidUpdate ();
}

ConcreteSubject : A specific topic class that stores all the observers interested in this topic. When the internal state changes, all registered observers (Notify)should be notified .

classConcreteSubjectextendsSubject {
PublicConcreteSubject (String name) {
Super(name);
}

@Override
PublicString GetState () {
returnState
}

@Override
PublicvoidSetState (String state) {
This. state = State;
}

@Override
PublicvoidNotify () {
System.out.println ("=======" + This. Name + "topic post new Message =======");
for(Observer observer:observers) {
Observer. Update ();
}
}
}

concreteobserver : The specific observer class that implements the update interfacefor Observer to synchronize state information with Subject.

classConcreteobserverextendsObserver {
PrivateString State;
PublicConcreteobserver (String name, Subject Subject) {
Super(name, subject);
}

@Override
PublicvoidUpdate () {
State = Subject.getstate ();
System.out.println ( This. Name + "received current status:" + state);
}
}


Test code

PublicclassObserverpattern {
PublicStaticvoidMain (string[] args) {
ConcreteSubject subject =NewConcreteSubject ("Weather");
Concreteobserver Observer1 =NewConcreteobserver ("Zhang San", subject);
Concreteobserver Observer2 =NewConcreteobserver ("John Doe", subject);
Concreteobserver Observer3 =NewConcreteobserver ("Harry", subject);

Subject. Attach (Observer1);
Subject. Attach (OBSERVER2);
Subject. Attach (OBSERVER3);
Subject.setstate ("Rain Today");
Subject. Notify ();

Subject. Detach (OBSERVER2);
Subject.setstate ("Sunny Tomorrow");
Subject. Notify ();
}
}View Code

Run results

======= Weather Topics release new messages =======
Zhang San received current status: Rain Today
John Doe received current status: Rain Today
Harry received current status: Rain Today
======= Weather Topics release new messages =======
Zhang San receive the current status: Sunny Tomorrow
Harry receive the current status: Sunny TomorrowView Code

JAVA 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.