I implemented a a listener, to get callbacks from the (Android) system when it finishes a specific task. That works well. Now (that i got passed basic testing) the task is to be performed from multiple places, and i want to do the same (listener related) actions each time.
By default, i would implement the listener in each class that needed it, and repeat the same code each time. That is, not just the listener, but some extra items, such as timing and translating the error messages. This seems redundant, so, i thought to create a class or object to implement the listener for me. I wrote the code for it in a separate file, but now wonder, how would a class that instantiates this new class know when things are done? Does the new object need to implement a callback itself? Should the individual classes just implement the listeners and only use this new class for the customizations (timing and translating)? Is any of this even a good idea?
Assuming I understand your question correctly (you want one listener class to be used in multiple places), you would have to know where the listener is being registered. This can be done in places where the code is something like:
or in various configuration/resource files around your application.
Without having some more specific details, I can't give a more specific answer, but that is the general idea. I do agree that code re-use is generally better than code-copy-and-paste, so your approach is generally correct.
Joel McNary wrote:This is an interesting design question, but probably not for Beginning Java; this is probably best in the Java in General forum.
If so, please move the thread to the correct forum.
Joel McNary wrote:(you want one listener class to be used in multiple places
If i understand you, not exactly. I want to employ the same service, which sends results to a listener. In my case, i am using Android to do speech recognition from multiple Activities. For the sake of example, let us imagine i want a testing Activity and a working Activity to each make use of this service. By default, it would be:And then a separate: With a lot of common code between them.
Instead, therefore, i created: with all the common code. I now want to implement it in both "test" and "work".
The problem is, the new class implemented a bunch of functions, including the all important onResults(), which is called by the system when it is finished recognizing the speech. When the system calls (according to my plan) the (instantiation of) the class, how does "test" or "work" itself get notified?
And the "Work" class would be implemented the same way as the "Test" class.
Joel McNary wrote:In this case, your test and work classes should call into the common speech-recognizer class.
That's a whole lotta implementin'.
While waiting for your reply, i just tried something. I changed speech_recognizer to implement AppCompatActivity (which only requires onCreate to be implemented, and it has to call super.onCreate). Then test extended speech_recognizer, and overrode onCreate (also calling super.onCreate) and then only overriding the methods it wants a hand in. So, onResults, calls super.onResults, and then does its own stuff.
Anything wrong with doing it that way?
Joel McNary wrote:There's nothing in particular wrong with doing that way -- I just got stuck thinking along one line of thought. I can probably come up with theoretical arguments for and against both designs, but if it works and is easy to understand and maintain, go for it.
Thank you for taking the time to post a solution and comment. So much to learn.