I want to do something like this in Java but I don\'t know the way:
When event \"object 1 say \'hello\'\" happens, then object 2 responds to that event by saying \"hello\".
Can somebody give me a hint or sample code?
I want to do something like this in Java but I don\'t know the way:
When event \"object 1 say \'hello\'\" happens, then object 2 responds to that event by saying \"hello\".
Can somebody give me a hint or sample code?
You probably want to look into the observer pattern.
Here\'s some sample code to get yourself started:
import java.util.*;
// An interface to be implemented by everyone interested in \"Hello\" events
interface HelloListener {
void someoneSaidHello();
}
// Someone who says \"Hello\"
class Initiater {
private List<HelloListener> listeners = new ArrayList<HelloListener>();
public void addListener(HelloListener toAdd) {
listeners.add(toAdd);
}
public void sayHello() {
System.out.println(\"Hello!!\");
// Notify everybody that may be interested.
for (HelloListener hl : listeners)
hl.someoneSaidHello();
}
}
// Someone interested in \"Hello\" events
class Responder implements HelloListener {
@Override
public void someoneSaidHello() {
System.out.println(\"Hello there...\");
}
}
class Test {
public static void main(String[] args) {
Initiater initiater = new Initiater();
Responder responder = new Responder();
initiater.addListener(responder);
initiater.sayHello(); // Prints \"Hello!!!\" and \"Hello there...\"
}
}
Related article: Java: Creating a custom event
What you want is an implementation of the observer pattern. You can do it yourself completely, or use java classes like java.util.Observer
and java.util.Observable
There are 3 different ways you may wish to set this up:
Thrower
inside of Catcher
Catcher
inside of Thrower
Thrower
and Catcher
inside of another class in this example Test
THE WORKING GITHUB EXAMPLE I AM CITING Defaults to Option 3, to try the others simply uncomment the \"Optional
\" code block of the class you want to be main, and set that class as the ${Main-Class}
variable in the build.xml
file:
4 Things needed on throwing side code:
import java.util.*;//import of java.util.event
//Declaration of the event\'s interface type, OR import of the interface,
//OR declared somewhere else in the package
interface ThrowListener {
public void Catch();
}
/*_____________________________________________________________*/class Thrower {
//list of catchers & corresponding function to add/remove them in the list
List<ThrowListener> listeners = new ArrayList<ThrowListener>();
public void addThrowListener(ThrowListener toAdd){ listeners.add(toAdd); }
//Set of functions that Throw Events.
public void Throw(){ for (ThrowListener hl : listeners) hl.Catch();
System.out.println(\"Something thrown\");
}
////Optional: 2 things to send events to a class that is a member of the current class
. . . go to github link to see this code . . .
}
2 Things needed in a class file to receive events from a class
/*_______________________________________________________________*/class Catcher
implements ThrowListener {//implement added to class
//Set of @Override functions that Catch Events
@Override public void Catch() {
System.out.println(\"I caught something!!\");
}
////Optional: 2 things to receive events from a class that is a member of the current class
. . . go to github link to see this code . . .
}