0

I have come across this workaround for making custom events in java and it seems like just what I need.

Create a custom event in Java

Although, my concern is how to scale this on, lets say 20 different methods, and I also need them to be optional individually. It is known that you must override all the methods of an interface with no exceptions so at the end i would need 20 nearly similar methods to call event listeners in the exact same way. This is new stuff to me, is there any more convenient way to do this?

What I got in mind (this basically repeated 20 times):

private return_type foo(int foo1, String foo2){
    //method does what it is supposed to do
    //interested listeners are called, and their purpose is to execute some additional code, for this exact method
    fooListener(foo1, foo2);
    return something;
}
  • Sorry if i can't understant it correctly...your problem is you'll have 20 different listener classes? (So each one should implement the method by its own?) – aran Jan 07 '21 at 04:58
  • I want each method to make an unique event. 20 is just an approximate number of functions that need to execute an event. My problem is that each event needs to be able to have different response, and all of them need to be optional. One should play some sound, one should log some variables if that is how the listener responds and so on. How I have understood it, that would require 20 interfaces, 20 responders and 20 initiators which is a lot! –  Jan 07 '21 at 05:01
  • ok, so you want, for example, the same command to be executed in different ways on base of the nature of each listener? Like saying `for (Listener lis: listeners) {lis.doSomething()}` -- and each listener would execute its own doSomething logic, right? – aran Jan 07 '21 at 05:55

0 Answers0