1

Sorry for my confusing title, but i don't know any better. I have two classes: My classes

The first class (called MainClass) create a GUI that contain a Socket. When any data is received from Socket Class, onReceived() will be called. So in this onReceived() method, i want a way to send a message back to the MainClass so that MainClass can know position has change and call setPostion().

So what is the properly way to do this (if necessary, correct my model for better practice). Thanks

cdxf
  • 5,501
  • 11
  • 50
  • 65
  • 6
    Sounds like something the observer pattern could solve. Java has built-in support for that but you can also do it yourself. http://stackoverflow.com/questions/13744450/interview-when-do-we-use-observer-and-observable – Jeroen Vannevel Jul 18 '14 at 07:22
  • 2
    @JeroenVannevel Your comment should be an answer. – Fildor Jul 18 '14 at 07:26
  • 1
    @Fildor: the comment is not the quality I want to see reflected in my answers and I do not have the time to write a more extensive one right now. – Jeroen Vannevel Jul 18 '14 at 07:28
  • I've just read the Observer Pattern but i think it is overcomplicated in this case, do you have other way (changing the relationship) more simple. – cdxf Jul 18 '14 at 07:36
  • 1
    I do not see anything simpler than the Observer Pattern if you do not want to poll for state changes, which is obviously the case. It does exactly what you described, nothing more, nothing less. – Fildor Jul 18 '14 at 07:38
  • @Snoob- This is not even close to a good solution (yes I know that..But this is simple..). Why not just set a *global* boolean flag in `onReceived()` and in your `Main` class, keep checking/polling for that flag on some predefined interval (say 2 seconds) using a thread. If that flag has been set, `doWhatYouWant()` and reset the flag. – TheLostMind Jul 18 '14 at 08:00
  • 1
    @TheLostMind Something like using [CyclicBarrier](http://docs.oracle.com/javase/7/docs/api/index.html?java/util/concurrent/CyclicBarrier.html) ? Well, for my taste, that's even more complicated, than a simple Observer, but hey ... – Fildor Jul 18 '14 at 08:05
  • @Fildor - More or less, yes. :). Although even a single thread could do it (less efficiently..) – TheLostMind Jul 18 '14 at 08:06
  • @TheLostMind that's a bad solution, obeserver pattern and listeners should be used in those cases. – danizmax Jul 18 '14 at 08:10
  • @danizmax -I repeat.. *This is not even close to a good solution (yes I know that..But this is simple..)* – TheLostMind Jul 18 '14 at 08:14
  • Thanks Jeroen Vannevel, i've used that pattern and it is really a good solution – cdxf Jul 18 '14 at 08:46
  • btw, Can you explain the benefits of using Observer Pattern instead passing the GUI object to Socket and call GUI.setPostion() in onReiceved(). – cdxf Jul 18 '14 at 09:13

1 Answers1

1

You can do the following;

Define listener interface:

public interface MyListener(){

    public void doNotify(String message);

}

The Socket class (has a methot to add listeners that are notified on onRecieve()):

public class SocketClass {

    private List<MyListener> listeners = new ArrayList<MyListener>();

    public void addListener(MyListener listener) {
        listeners.add(listener);
    }

    public void onRecieve(){
        /* your code*/
        for (MyListener l : listeners)
            l.doNotify("Socket has recieved something ;P");
    }

}

The Gui class has a method that passes on the listener:

public class GuiClass() {

    SocketClass s = new SocketClass();

    public void addListener(MyListener listener) {
        s.addListener(listener);
    }

}

And finally the main class (implements the listener interface and adds itselef to GuiClass as listener):

public class MainClass implements MyListener {

    public static void main(String[] args) 
    {
        GuiClass g =  new GuiClass();
        g.addListener(this);

    }

    public void doNotify(String message) {
        System.out.println(message);
        setPostition();
    }

    private void setPosition()
        /* your code here */
    }

}

A good example is also THIS.

Good luck and Regards

Community
  • 1
  • 1
danizmax
  • 2,446
  • 2
  • 32
  • 41