Results 1 to 6 of 6
  1. #1
    livnihai is offline Member
    Join Date
    Sep 2009
    Posts
    18
    Rep Power
    0

    Default Problem with observer pattern

    class A creates class B, and i need that class A will know
    about event that happens in class B.
    I've tried to use observer pattern, by useing mathod setState
    from class B i dont get any result.

    [code]

    public class A implements Observer{

    private String state = "100";

    public A(){
    System.out.println(state);
    }

    public void update(Subject o) {
    state = o.getState();
    System.out.println("Update received from Subject, state changed to : " + state);
    }

    public static void main(String[] args) {

    Observer a = new A();
    Subject b = new B();

    b.addObserver(a);
    b.setState("test from A");
    }
    }

    public class B implements Subject{

    private List observers = new ArrayList();
    private String state = "200";

    public B(){
    System.out.println("Constractor B befor setState: " + state);
    setState("Test from Constractor B");
    System.out.println("Constractor B after setState: " + state);
    }

    public String getState() {
    return state;
    }
    public void setState(String state) {
    System.out.println("setState: " + state);
    this.state = state;
    notifyObservers();
    }
    public void addObserver(Observer o) {
    observers.add(o);
    }
    public void removeObserver(Observer o) {
    observers.remove(o);
    }
    public void notifyObservers() {
    Iterator i = observers.iterator();
    while (i.hasNext()) {
    Observer o = (Observer) i.next();
    o.update(this);
    }
    }
    }

    thank for any help

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    805
    Rep Power
    6

    Default

    Hi,
    What the Subject interface is doing here?

    U just write as a pseudo code what u want then we can discuss about this Observer pattern.

    First tell me what is the role of Subject here and what u want 2 do execvtly?
    Ramya:cool:

  3. #3
    livnihai is offline Member
    Join Date
    Sep 2009
    Posts
    18
    Rep Power
    0

    Default

    I don't know if observer pattern is the best way,
    This is the problem
    I have the main class (A) that creates other class (B).
    class B listen to event.
    I need that class A will know when the event occur.

    [pseudo code]

    class A {

    creates class B
    if event happens in B do things
    }

    class B{

    Listen to event
    when event happen inporm class A
    }

    i hope now it's more clear

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    805
    Rep Power
    6

    Default

    what is the role of Subject interface here?
    Ramya:cool:

  5. #5
    livnihai is offline Member
    Join Date
    Sep 2009
    Posts
    18
    Rep Power
    0

    Default

    The Subject trace when the event happens

  6. #6
    pellebye is offline Member
    Join Date
    Apr 2009
    Posts
    54
    Rep Power
    0

    Default

    Look at this thread: http://www.java-forums.org/showthread.php?t=18877

    Good luck.
    Last edited by pellebye; 10-11-2009 at 10:20 PM.
    -
    Life is not the worst thing we have ... in a few minutes my coffee is ready.

Similar Threads

  1. MVC Pattern
    By bubbless in forum New To Java
    Replies: 12
    Last Post: 03-14-2009, 09:15 PM

Posting Permissions

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