Java implements the observer Mode

Source: Internet
Author: User

Introduction to observer mode:

Definition: defines the one-to-many dependency between objects. When the State of an object changes, all objects dependent on it are notified and automatically updated.

Principle: it consists of four parts: Abstract target class, specific target class, abstract observer class, and specific observer class.

 

Subject. java </P> <p> Public interface subject {<br/> void attach (Observer observer); // Add an observer <br/> void detach (Observer observer ); // Delete the observer <br/> void inform (); // notify all Observers <br/>}< br/> Public interface subject {<br/> void attach (Observer observer ); // Add an observer <br/> void detach (Observer observer); // Delete the observer <br/> void inform (); // notify all Observers <br/>}</P> <p> concretesubject. java </P> <p> public class conceretesubject implem Ents subject. java {<br/> Public void attach (Observer observer) {<br/> observersvector. addelement (observer); <br/>}< br/> Public void attach (Observer observer) {<br/> observersvector. removeelement (observer); <br/>}< br/> Public void inform () {<br/> // call the update method for all elements in observersvector <br/>}< br/> public class conceretesubject implements subject. java {<br/> Public void attach (Observer obser Ver) {<br/> observersvector. addelement (observer); <br/>}< br/> Public void attach (Observer observer) {<br/> observersvector. removeelement (observer); <br/>}< br/> Public void inform () {<br/> // call the update method for all elements in observersvector <br/>}</P> <p> observer. java </P> <p> Public interface observer {<br/> void Update (); // update the observer <br/>}< br/> Public interface observer {<br/> void Update (); // update the observer <br/>}</P> <P> concreteobserver. java </P> <p> public class concreteobserver implements observer {<br/> Public void Update () {<br/>}< br/> public class concreteobserver implements observer {<br/> Public void Update () {<br/>}</P> <p> use Java. util. observer interface and Java. util. observable class implements the observer mode: </P> <p> The Observer implements the observer interface and overwrites the update method, which is called when the observer status changes. </P> <p> the observable class is inherited by the observer. The observable class includes methods for adding and deleting the observable class, and methods for notifying all observers to update the class. When the status changes, setchanged and then policyobservers. </P> <p> observer Interface source code: </P> <p> package Java. util; <br/> Public interface observer <br/>{< br/>/** <br/> * When the observed object changes, this method will be called. <Br/> */<br/> void Update (observable o, object Arg); <br/>}< br/> package Java. util; <br/> Public interface observer <br/>{< br/>/** <br/> * When the observed object changes, this method will be called. <Br/> */<br/> void Update (observable o, object Arg); <br/>}</P> <p> source code of the observable class: </P> <p> package Java. util; <br/> public class observable <br/>{< br/> private Boolean changed = false; <br/> private vector obs; <br/>/** construct an observer with 0 observers. **/<Br/> Public observable () <br/>{< br/> obs = new vector (); <br/>}< br/>/** <br/> * add an observer to the observer list. <Br/> */<br/> Public synchronized void addobserver (Observer O) <br/>{< br/> If (! Obs. contains (O) <br/>{< br/> obs. addelement (o); <br/>}< br/>/** <br/> * deletes an observer object from the observer list. <Br/> */<br/> Public synchronized void deleteobserver (Observer O) <br/>{< br/> obs. removeelement (o); <br/>}< br/>/** <br/> * equivalent to policyobservers (null) <br/> */<br/> Public void policyobservers () <br/> {<br/> policyobservers (null ); <br/>}< br/>/** <br/> * if the object changes (then the haschanged method returns true) <br/> * call this method to notify all registered observers, that is, call their update () method. <br/> * pass this and Arg as parameters. <Br/> */<br/> Public void policyobservers (Object Arg) <br/> {<br/>/** <br/> * temporarily stores the status of the current observer. See memorandum mode. <Br/> */<br/> object [] arrlocal; <br/> synchronized (this) <br/>{< br/> If (! Changed) return; <br/> arrlocal = obs. toarray (); <br/> clearchanged (); <br/>}< br/> for (INT I = arrlocal. length-1; I> = 0; I --) <br/> (observer) arrlocal [I]). update (this, ARG ); <br/>}< br/>/** <br/> * clear the observer list <br/> */<br/> Public synchronized void deleteobservers () <br/>{< br/> obs. removeallelements (); <br/>}< br/>/** <br/> * Set "changed" to true <br/> */<br/> protected synchronized void setchanged () <br/>{< br/> CH Anged = true; <br/>}< br/>/** <br/> * RESET "changed" to false <br/> */<br/> protected synchronized void clearchanged () <br/>{< br/> changed = false; <br/>}< br/>/** <br/> * check whether the object has changed <br/> */<br/> Public synchronized Boolean haschanged () <br/>{< br/> return changed; <br/>}< br/>/** <br/> * return the observed object (that is, this object) total number of observers. <Br/> */<br/> Public synchronized int countobservers () <br/> {<br/> return obs. size (); <br/>}< br/> time to use: </P> <p> 1. when an object needs to change other objects at the same time, and it does not know how many objects need to be changed, the observer mode should be considered; </P> <p> 2. an abstract model has two aspects, one of which depends on the other. In this case, the observer mode can close the two aspects in an independent object so that they can be changed and reused independently; </P> <p> 3. the work done by the observer mode is actually decoupling. Let the two sides of coupling depend on abstraction, rather than on specifics, so that their changes will not affect the changes on the other side. </P> <p>
This article comes from: http://edu.codepub.com/2009/1102/17225_2.php

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.