Observer/Observable object model

M

Maurizio

Hi to all,

I've some problem with Observer/Observable object model.
I've found this interesting article:
http://www.javaworld.com/javaworld/javatips/jw-javatip29.html

I've try this simple program based on the instruction, but I can't get
DisplayObserver on my console.

Could you give me some suggestion

thank you very very much!

Regards
Maurizio

*******************************
*** FILE main.java ***
*******************************
public class main {

public static void main(String[] args) throws NumberFormatException,
IOException {

MsgObservable msgObj = new MsgObservable();
DisplayObserver display = new DisplayObserver();
msgObj.addObserver( display );
msgObj.test();
System.out.println("END");
*******************************
*** FILE MsgObservable.java ***
*******************************
import java.util.Observable;

public class MsgObservable extends Observable
{

public MsgObservable(){}
public void test()

{

Object msg = new Object();
notifyObservers( msg );
}
}
*********************************
*** FILE DisplayObserver.java ***
*********************************
import java.util.Observable;
import java.util.Observer;

public class DisplayObserver implements Observer
{

public DisplayObserver(){}
public void update( Observable obj, Object arg )
{
System.out.println("DisplayObserver");
}
}
 
M

Maurizio

Could you give me some suggestion

I've just found my mistake!
In class MsgObservable , I've not call setChanged before
notifyObservers.

Sorry for my post.

By
Maurizio
public class MsgObservable extends Observable
{

   public MsgObservable(){}
   public void test()

   {

         Object msg = new Object();
setChanged(); <-------------- ADD
setChanged();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top