0
votes

hi i was wondering how can i do to get a shared event in java let me explain to u what i want to do

first i got a Controller class all it methods are static

class TrafficController {

public static void controlTraffic() {

// does something //>> i want to Notify that this action was done }

}

in the other side i got a Listener

class TrafficListener{
public static void watchNewTraffic()
{ //does something when new traffic appears } }

have any one any idea how can i deal with it

i have found that there is Observer and Observable but i need to implement method of Observer also i have found Propertychangelistener which are useless in my case because i have static methods

3
1) Please add an upper case letter at the start of sentences. Also use a capital for the word I, and abbreviations and acronyms like JEE or WAR. 2) Please use the correct spelling for words like 'you', 'your' & 'please'. -- Doing these things makes it easier for people to understand and help.Andrew Thompson

3 Answers

0
votes

First, you should remove the static modifier from the TrafficListener.watchNewTraffic() method. Otherwise, you cannot have multiple listeners. Better yet, you should make TrafficListener an interface:

interface TrafficListener {
    void watchNewTraffic();
}

Each class that wants to be a listener needs to implement the interface. You can then maintain a collection of listeners in the TrafficController class:

class TrafficController {
    private static Set<TrafficListener> listeners = new HashSet<>();

    public static void controlTraffic() {
        // do stuff
        notifyListeners();
    }

    public static void addTrafficListener(TrafficListener listener) {
        listeners.add(listener);
    }

    public static void removeTrafficListener(TrafficListener listener) {
        listeners.remove(listener);
    }

    private static void notifyListeners() {
        for (TrafficListener listener : listeners) {
            listener.watchNewTraffic();
        }
    }
}

It's not clear what you are trying to accomplish, though. Something about your question suggests to me that you should consider using a producer-consumer pattern rather than an observer pattern.

0
votes

Using static methods here seems quite odd. A common way would be to have a list of listeners which are then called when the event is thrown. That would require an instance, though.

Is there a specific reason why all those methods are static? In that case, calling watchNewTraffic() in controlTraffic() wouldn't seem that bad.

Alternatively create a listener which in turn calls watchNewTraffic(). I'll expand on Ted's example.

class StaticTrafficListener implements TrafficListener {
  public void watchNewTraffic() {
    TrafficWatcher.watchNewTraffic();  //this would be your TrafficListener which I renamed for this example to make sense ;)
  }
}
0
votes

you can build an inner class inside TriafficController class and name it eventHandler or any thing else then you can take an object from i

public class TriafficController  extends JFrame {

private JButton button;
private JButton button2;

// the constructor 
public TriafficController  (){


button = new JButton("push me");
button2 = new JButton("push me 2");
add(button);
add(button2);

// handler is an object of a inner class we will create latter , this object you can share it with and component

eventHandler handler = new eventHandler();

button.addActionListener(handler);
button2.addActionListener(handler);


}
// out of the constructor we will build the inner class
private class eventHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent event)

{

  // code your action here , when ever you post your object 'handler' this method will performed

}


}

}