Results 1 to 2 of 2
  1. #1
    kiregad is offline Member
    Join Date
    Mar 2010
    Posts
    23
    Rep Power
    0

    Default Have I understood how the observer pattern works?

    I read about the observer pattern and tried to implement it. I have used 1 interface for the observer, and one interface for the observed.

    Subject.java
    Java Code:
    package observer;
    
    public interface Subject {
    	public void addObserver(Observer o);
    	public void removeObserver(Observer o);
    }
    Observer.java
    Java Code:
    package observer;
    
    public interface Observer {
    	public void update(Subject o);
    }
    CoolPerson.java
    Java Code:
    package observer;
    
    import java.util.ArrayList;
    import java.util.Iterator;
    
    public class CoolPerson implements Subject{
    	
    	ArrayList<Observer> observers = new ArrayList<Observer>();
    	
    	public void actCool() {
    		/* flex */
    		notifyObservers();
    	}
    	
    	public void addObserver(Observer o) {
    		observers.add(o);
    	}
    	
    	public void removeObserver(Observer o) {
    		observers.remove(o);
    	}
    	
    	private void notifyObservers() {
    		// Notify each observer
    		
    		Iterator i = observers.iterator();
    		while(i.hasNext()) {
    			Observer o = (Observer)i.next();
    			o.update(this);
    		}
    	}
    }
    CoolPersonFan.java
    Java Code:
    package observer;
    
    public class CoolPersonFan implements Observer{
    	public void update( Subject o ) {
    		System.out.println("the cool person did something!");
    	}
    }
    And at last some code to test it:

    Java Code:
    CoolPerson dag = new CoolPerson();
    		
    		CoolPersonFan fan1 = new CoolPersonFan();
    		CoolPersonFan fan2 = new CoolPersonFan();
    		CoolPersonFan fan3 = new CoolPersonFan();
    		
    		dag.addObserver(fan1);
    		dag.addObserver(fan2);
    		dag.addObserver(fan3);
    		dag.addObserver(fan3);
    		
    		dag.actCool();
    Which prints out 4 lines, as I would want it to, in the console.

    Have I understood how the pattern works? Why isnt the notifyObservers() in Subject.java? I mean, I think it should be a part of the "interface contract", since it is a vital part of the pattern.
    Last edited by kiregad; 05-16-2010 at 02:13 AM.
    Java beginner.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by kiregad View Post
    Have I understood how the pattern works? Why isnt the notifyObservers() in Subject.java? I mean, I think it should be a part of the "interface contract", since it is a vital part of the pattern.
    Yup, you understood what it's all about; think of it: if the notifyObservers() method would be part of the interface (the 'contract') it had to be a public method and then the entire world would be able to notify the observers, even if the observer itself didn't want it (even if it wouldn't be acting cool).

    kind regards,

    Jos

Similar Threads

  1. Problem with observer pattern
    By livnihai in forum New To Java
    Replies: 5
    Last Post: 10-11-2009, 03:34 PM
  2. Replies: 8
    Last Post: 07-22-2009, 02:16 AM
  3. How "Pattern.matches(regex, input)" function works?
    By kishan in forum Advanced Java
    Replies: 2
    Last Post: 04-26-2009, 01:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •