Java (23)-Viewer mode

Source: Internet
Author: User

One. Observer mode:

(1). Observer pattern Definition: defines a one-to-many dependency that allows multiple observers to listen to a Principal object. When the Subject object changes, all the observer objects are notified, allowing them to update themselves automatically.

(2). The composition of the Observer pattern:

a). Specific topic role: Notifies all registered observers when changes occur within a specific subject role. (typically implemented with a class)

b). Abstract theme Role: Save references to all observer objects in a collection, and each abstract theme role can have any observer, typically implemented with an abstract class or interface.

c). Abstract Observer role: Define an interface for a specific observer and update yourself when you get a topic notification.

d). Specific Observer role: implements the update interface required by the abstract viewer role.

Two. Implement your own Observer pattern:

1). Abstract Theme Role:

The observed is the abstract theme role public interface watched {    //increase observer public void Addwatcher (Watcher watcher);//delete observer public void Removewatcher (Watcher Watcher);//notify observer public void Notifywatchers (String str);}


2). Abstract Observer Role:

Observed is the abstract observer role public interface Watcher {//update own public void update (String str);}

3). Specific Theme roles:

Specific theme role public class Concretewatched implements watched {    //Save Observer private list<watcher> List = new ArrayList <Watcher> (); @Overridepublic void Addwatcher (Watcher watcher) {List.add (watcher);} @Overridepublic void Removewatcher (Watcher watcher) {list.remove (watcher);} @Overridepublic void Notifywatchers (String str) {//Notify observer for (Watcher Watcher:list) {watcher.update (str);}}}


4). Specific Observer role:

The specific observer role public class Concretewatcher implements watcher{//implements an update method for the interface @overridepublic void update (String str) { System.out.println (str);}}


5). Test:

public class Index {public static void main (string[] args) {watched w = new concretewatched (); Watcher R1 = new Concretewatcher (); Watcher r2 = new Concretewatcher (); Watcher R3 = new Concretewatcher () W.addwatcher (R1); W.addwatcher (R2); W.addwatcher (R3)    ; W.notifywatchers ("haha");}}

Print:

Ha ha

Ha ha

Ha ha



Three. JDK's built-in support for observers:

1). The observable class and the Observer interface are available in the JDK:

The observable class is used to create subclasses that can observe other parts of your program. Notifies the observer when the seed class has changed. The observation class must implement the update () method of the Observer interface.

2). The method that must be called:

The Setchandeg () method must be called when a observed object changes, and the Notifyobservers () method must be called when the Observer is notified. If the Setchandeg () method is not called when the Observer is notified, there is no response.

3). The. Notifyobservers () method has two forms:

One is a parameter with no parameters. A method with parameters whose parameters are passed to the second parameter of update ().

Simple implementation:

The Observer class beingwatched  extends Observable{public void  observername (String str) {//changed from false to true, indicating that I can send a notification. This.setchanged ();//Notify Viewer this.notifyobservers (str);}} Observer object A class Watcher1 implements observer{@Overridepublic void update (Observable O, object arg) {System.out.println ( Arg.tostring () + "Watcher1");}} Observer object Two class Watcher2 implements observer{@Overridepublic void update (Observable O, object arg) {System.out.println ( Arg.tostring () + "Watcher2");}} public class Observabletest {public static void main (string[] args) {beingwatched  watched = new beingwatched (); Watcher1  w1 = new Watcher1 (); Watcher2  w2 = new Watcher2 (); Watched.addobserver (W1); Watched.addobserver (W2); Watched.observername ("I AM The Observer:");}}

Print:

I am the Observer: WATCHER2 I AM the Observer: Watcher1

Four. Application of the Observer:

1). Updates to an object state require other objects to be updated synchronously, and the number of other objects is dynamically variable.


2). Objects only need to notify their own updates to other objects without needing to know the details of other objects.





Java (23)-Viewer 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.