Recommended sites

Add to MyYahoo!
Subscribe in NewsGator Online
Add to Newsburst
Add to Google
Add to My AOL
Add to Pluck
Subscribe in FeedLounge
Add to Windows Live
Add to NetVibes
Subscribe in Rojo
Subscribe in Bloglines
Add to MyMSN
Add to Plusmo for your cellphone
Add to PageFlakes
Add to Technorati
Add to BlinkBits
Observer Print E-mail
User Rating: / 1
PoorBest 
Thursday, 20 January 2005 21:26
Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.



Source code

/**
 * Knows its observers. Any number of Observer objects
 * may observe a subject. Provides an interface for attaching
 * and detaching Observer objects.
 * @role __Subject
 */
public class Subject {
        private ArrayList observers = new ArrayList();        public void attach(Observer observer) {
                observers.add(observer);        }

        public void detach(Observer observer) {
                int idx = observers.indexOf(observer);                if (idx != -1) {
                        observers.remove(idx);                }
        }

        protected void notifyObservers() {
                Iterator it = observers.iterator();                while (it.hasNext()) {
                        ((Observer) it.next()).update(this);                }
        }

}

/**
 * Defines an updating interface for objects that
 * should be notified of changes in a subject.
 * @role __Observer
 */
public interface Observer {
        /**
  * Update method.
  */
        public void update(Subject subject);}

/**
 * Stores state of interest to ConcreteObserver objects.
 * Sends a notification to its observers when its state changes.
 */
public class ConcreteSubject extends Subject {

        // add your subject's specific stuff here
}

/**
 * Implements the Observer updating interface to keep 
 * its state consistent with the subject's.
 */
public class ConcreteObserver implements Observer {
        public void update(Subject subject) {
                // put your code here
        }

}


Comments
Add New Search RSS
Write comment
Name:
Email:
 
Title:
UBBCode:
[b] [i] [u] [url] [quote] [code] [img] 
 
:):grin;)8):p:roll:eek:upset:zzz:sigh:?:cry
:(:x
Please input the anti-spam code that you can read in the image.

3.20 Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

Last Updated ( Friday, 17 June 2005 23:37 )
 


Another articles:


Content View Hits : 2426288

Enter Amount: