Design issue

D

David

Hi everyone,

I have already started programming an application with the following model:

I have a class A and a class B.
Where,
class B extends Thread, receives data from an external source at
undetermined intervals of time and interpretes this data.

Class A is a Swing GUI which has an object of class B. Class A has to get
data from class B in order to show it in the graphical objects. In fact,
I'm wondering how class A would manage to know when class B has data
ready? I was thinking about a tight loop in class B that polls the class
A object to know when data from the latter is available.

Is there some mechanism more appropriate? Any idea, url or reference
material will really help.

Thanking you for your attention,

David.
 
K

KC Wong

I have already started programming an application with the following
model:
I have a class A and a class B.
Where,
class B extends Thread, receives data from an external source at
undetermined intervals of time and interpretes this data.

Class A is a Swing GUI which has an object of class B. Class A has to get
data from class B in order to show it in the graphical objects. In fact,
I'm wondering how class A would manage to know when class B has data
ready? I was thinking about a tight loop in class B that polls the class
A object to know when data from the latter is available.

Is there some mechanism more appropriate? Any idea, url or reference
material will really help.

Class B set a flag to tell if new data is available, and class A checks that
flag periodically.
 
A

Adam

David said:
Hi everyone,

I have already started programming an application with the following model:

I have a class A and a class B.
Where,
class B extends Thread, receives data from an external source at
undetermined intervals of time and interpretes this data.

Class A is a Swing GUI which has an object of class B. Class A has to get
data from class B in order to show it in the graphical objects. In fact,
I'm wondering how class A would manage to know when class B has data
ready? I was thinking about a tight loop in class B that polls the class
A object to know when data from the latter is available.

Is there some mechanism more appropriate? Any idea, url or reference
material will really help.

Observer/Observable or Listener design pattern.

Make thread in B notify object of A about receiving data.

Object of A could register in object B before B starts its thread,
and in that thread all registered listeners (in this case object of A
only)
would be notified about certain state in which B decided to
send the notification (in this case: when all data arrived).

Adam
 
M

Michael Rauscher

Hi David,
Hi everyone,

I have already started programming an application with the following model:

I have a class A and a class B.
Where,
class B extends Thread, receives data from an external source at
undetermined intervals of time and interpretes this data.

Class A is a Swing GUI which has an object of class B. Class A has to get
data from class B in order to show it in the graphical objects. In fact,
I'm wondering how class A would manage to know when class B has data
ready? I was thinking about a tight loop in class B that polls the class
A object to know when data from the latter is available.

Is there some mechanism more appropriate? Any idea, url or reference
material will really help.

Decouple A and B. You could e.g. use the observer design pattern or MVC:

Observer:

Two types are needed:

public interface Subject {
void addObserver( Observer o );
void removeObserver( Observer o );
void notify();
}

public abstract class Observer {
public void update();
}

Now, B implements Subject
import java.util.*;

class B extends Thread implements Subject {
ArrayList observers = new ArrayList();
// ...

public void addObserver( Observer o ) {
observers.add( o );
}

public void removeObserver( Observer o ) {
observers.remove( o );
}

public void notify() {
Iterator it = observers.iterator();
while ( it.hasNext() )
((Observer)it.next()).update();
}

public void run() {
while ... {
// read data
// interprete data
// store data
notify();
}
}
}

And A uses an implementation of Observer that is in the following
example an anonymous inner class that extends Observer:
class A {
B b;

public A() {
// ...
b.addObserver( new Observer() {
public void update() {
redrawState();
}
});
}

public void redrawState() {
// put the code that queries b for the actual state herein
}
}

This way, B doesn't need to know something about A. You could further
decouple in that way, that A doesn't need to know something about B. To
do this, you could use MVC (or a variant, called Document-View): Simply
separate data storage from B using another class, called the model. So
instances of B manipulate the model (thus B objects store its data
within the model) and the model notifies the registered views (instances
of A). This way A doesn't even need to know about the existence of B at
all. If you need methods of B within A you could define an interface
that B implements and then provide B as controller of the view.

Bye
Michael
 
W

William Brogden

Hi everyone,

I have already started programming an application with the following
model:

I have a class A and a class B.
Where,
class B extends Thread, receives data from an external source at
undetermined intervals of time and interpretes this data.

Class A is a Swing GUI which has an object of class B. Class A has to
get
data from class B in order to show it in the graphical objects. In fact,
I'm wondering how class A would manage to know when class B has data
ready? I was thinking about a tight loop in class B that polls the class
A object to know when data from the latter is available.

Is there some mechanism more appropriate? Any idea, url or reference
material will really help.

Take a look at the java.util.Observer interface and java.util.Observable
class. This kind of updating is exactly what they were designed for.

Bill
 
D

Dale King

Hello, Adam!
You said:
following

The first issue is that you should be implementing Runnable not
extending Thread. It is rarely ever appropriate to extend Thread.
A has
to get objects. In
fact, polls the
class

I think you got your A and B backwards there, but a polling loop
is a bad idea.
Observer/Observable or Listener design pattern.

The event listener pattern is more appropriate here. The
Observer/Observable implementations in java.util should be
avoided.
Make thread in B notify object of A about receiving data.

Object of A could register in object B before B starts its thread,
and in that thread all registered listeners (in this case object of A
only)
would be notified about certain state in which B decided to
send the notification (in this case: when all data arrived.

But it is important to realize that the notification is going to
occur on the thread executing in B which can have many
ramifications. The OP said that class A which is the listener is
a Swing GUI. Swing is in general not thread safe in order to make
it faster. It is not safe to call most of the methods in Swing on
your own thread. Therefore those listeners should not invoke
Swing operations. and most likely should use invokeLater to
execute approriate code in the GUI thread where it is safe. This
however should be an implementation detail within A and B should
not care.

For more info on Swing and threads see the articles at
http://java.sun.com/products/jfc/tsc/articles
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top