Design issue

Discussion in 'Java' started by David, May 23, 2004.

  1. David

    David Guest

    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.
     
    David, May 23, 2004
    #1
    1. Advertising

  2. David

    KC Wong Guest

    > 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.
     
    KC Wong, May 24, 2004
    #2
    1. Advertising

  3. David

    Adam Guest

    "David" <> wrote in message
    news:p...
    > 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
     
    Adam, May 24, 2004
    #3
  4. 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
     
    Michael Rauscher, May 24, 2004
    #4
  5. 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 =---
     
    William Brogden, May 24, 2004
    #5
  6. David

    Dale King Guest

    Hello, Adam!
    You wrote:

    >
    > "David" <> wrote in message
    > news:p...
    > > 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
     
    Dale King, Apr 15, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ZackS
    Replies:
    5
    Views:
    6,868
    Just an Illusion
    Jul 9, 2004
  2. SpamProof
    Replies:
    3
    Views:
    669
    SpamProof
    Dec 1, 2003
  3. dave
    Replies:
    5
    Views:
    616
    William Brogden
    Jul 17, 2004
  4. Tim Smith
    Replies:
    2
    Views:
    883
    Tim Smith
    Dec 15, 2004
  5. trint
    Replies:
    1
    Views:
    368
    trint
    Nov 21, 2006
Loading...

Share This Page