Java Watcher pattern

Source: Internet
Author: User

1. Overview

Sometimes referred to as the Publish/subscribe pattern, 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.

2. Issues to solve

Splitting a system into a class in which some classes work together has a bad side effect, which is the need to maintain consistency among related objects. We do not want to be tightly coupled in order to maintain consistency, which can cause inconvenience to maintenance, expansion, and reuse. The observer is the solution to this kind of coupling relationship.

3. Roles in the pattern

3.1 Abstract Theme (Subject): It saves references to all observer objects in a single aggregation, each subject can have any number of observers. Abstract topics provide an interface that allows you to add and remove observer objects.

3.2 Specific topics (ConcreteSubject): deposit the relevant state into a specific observer object, and give notice to all registered observers when changes are made to the internal state of the specific subject.

3.3 Abstract Observer (Observer): Define an interface for all specific observers and update yourself when you get a topic notification.

3.4 Specific Observer (CONCRETEOBSERVER): Implements the update interface required by the abstract observer role in order to reconcile its state with the subject state.

Abstract Observer: public interface watchered {public void Say ();}

Specific Observer: No. 1th public class Watcheredimpl implements watchered {private string Name;public string GetName () {return name;} public void SetName (String name) {this.name = name;} Public Watcheredimpl (String name) {super (); this.name = name;} @Overridepublic void Say () {System.out.println (this.name+ "change!");}} Number 2nd public class WATCHEREDIMPL2 implements watchered {private string Name;public string GetName () {return name;} public void SetName (String name) {this.name = name;} Public WatcheredImpl2 (String name) {super (); this.name = name;} @Overridepublic void Say () {System.out.println (this.name+ "2nd has been updated");}} Number 3rd public class WatcheredImpl3 implements watchered {private string Name;public string GetName () {return name;} public void SetName (String name) {this.name = name;} Public WatcheredImpl3 (String name) {super (); this.name = name;} @Overridepublic void Say () {System.out.println (this.name+ "3rd has changed!");}}

Abstract theme public interface Watecher {void Removewatcher (watchered watecher); void Addwathtcher (watchered watecher); void Notifywatchered ();} Specific theme public class Watcherimpl implements Watecher {private list<watchered> li = new arraylist<watchered> (); @ overridepublic void Removewatcher (watchered watecher) {li.remove (watecher);} @Overridepublic void Addwathtcher (watchered watecher) {li.add (watecher);} @Overridepublic void notifywatchered () {for (watchered wa:li) {wa. Say ();}}}
Test Client

public class TestClient {public static void main (string[] args) {Watcheredimpl wa1 =new watcheredimpl ("Zs"); WATCHEREDIMPL2 wa2 =new WatcheredImpl2 ("Zs"); WATCHEREDIMPL3 wa3 =new WatcheredImpl3 ("Zs"); Watecher WD = new Watcherimpl (); Wd.addwathtcher (WA1); Wd.addwathtcher (WA2); Wd.addwathtcher (WA3); Wd.notifywatchered ( );}}
Operation Result:

ZS change!
ZS 2nd has been updated.
ZS has changed!



Java Watcher 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.