Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Design issue

Reply
Thread Tools

Design issue

 
 
David
Guest
Posts: n/a
 
      05-23-2004
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.
 
Reply With Quote
 
 
 
 
KC Wong
Guest
Posts: n/a
 
      05-24-2004
> 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.


 
Reply With Quote
 
 
 
 
Adam
Guest
Posts: n/a
 
      05-24-2004

"David" <> wrote in message
news ldomain...
> 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


 
Reply With Quote
 
Michael Rauscher
Guest
Posts: n/a
 
      05-24-2004
Hi David,

David schrieb:

> 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

 
Reply With Quote
 
William Brogden
Guest
Posts: n/a
 
      05-24-2004
On Sun, 23 May 2004 12:24:59 +0400, David <>
wrote:

> 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


----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
 
Reply With Quote
 
Dale King
Guest
Posts: n/a
 
      04-15-2006
Hello, Adam!
You wrote:

>
> "David" <> wrote in message
> news ldomain...
> > 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.


The first issue is that you should be implementing Runnable not
extending Thread. It is rarely ever appropriate to extend Thread.

> > 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.


I think you got your A and B backwards there, but a polling loop
is a bad idea.

> >
> > Is there some mechanism more appropriate? Any idea, url or

> reference
> > material will really help.

>
> 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
--
Dale King
My Blog: http://daleking.homedns.org/Blog
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
CFP - Journal of Systems Architecture, Embedded Software Design(Elsevier), Special Issue on Hardware/Software Co-Design Juan A. Gomez-Pulido VHDL 0 08-24-2009 02:11 PM
2nd. CFP - Journal of Systems Architecture - Embedded Software Design(Elsevier) - Special Issue on HARDWARE/SOFTWARE CO-DESIGN Juan A. Gomez-Pulido VHDL 0 05-24-2009 03:14 PM
Class design/design pattern resources TomTom MCSD 2 10-09-2004 07:38 AM
Xilinx Schematic design vs VHDL code design ZackS VHDL 5 07-09-2004 07:51 AM
Looking for help/resources on Writing a nice detailed design / tech design for vb.net code SpamProof Java 3 12-01-2003 06:06 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57