How can I send custom events from one object to another?
I have 2 Working objects of same type, main class uses worker.
if some condition is true in worker1 it should send ea event to the main class that will switch the workers.
This is my scenario:
class Worker1 extends Base{
public void DoSomeWork(Arg argument){
if(someCondition=true){
SendEvent();
}
}
}
class MainClassThatUsesWorker{
Base worker = new Worker1();
public void EventArrived(){
worker = new Worker2();
}
public void UseWorKer(){
worker.DoSomeWork();
}
}
I suggest you implement the observer pattern. Your main class is then the observer and the worker is the observable (or subject). When the worker fires the appropriate event, your main class handles it appropriately. Be careful with the synchronization because you work with multiple threads.