jsp - shared event in java -


hi wondering how can shared event in java let me explain u want

first got controller class methods static

class trafficcontroller {

public static void controltraffic() {

// //>> want notify action done }

}

in other side got listener

class trafficlistener{
public static void watchnewtraffic()
{ //does when new traffic appears } }

have 1 idea how can deal

i have found there observer , observable need implement method of observer have found propertychangelistener useless in case because have static methods

first, should remove static modifier trafficlistener.watchnewtraffic() method. otherwise, cannot have multiple listeners. better yet, should make trafficlistener interface:

interface trafficlistener {     void watchnewtraffic(); } 

each class wants listener needs implement interface. can maintain collection of listeners in trafficcontroller class:

class trafficcontroller {     private static set<trafficlistener> listeners = new hashset<>();      public static void controltraffic() {         // stuff         notifylisteners();     }      public static void addtrafficlistener(trafficlistener listener) {         listeners.add(listener);     }      public static void removetrafficlistener(trafficlistener listener) {         listeners.remove(listener);     }      private static void notifylisteners() {         (trafficlistener listener : listeners) {             listener.watchnewtraffic();         }     } } 

it's not clear trying accomplish, though. question suggests me should consider using producer-consumer pattern rather observer pattern.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -