Is it possible to implement only one function from EventListener with several functions?

I am developing an application for connecting to a multisensor based on bluetooth ( SensorDrone ). It contains about 15 sensors, all of which can interact with an Android device using a third-party library with closed source code created by chip manufacturers.

A third-party library requires me to implement my own Java EventListener, which contains a function for each sensor that launches when its data is ready for use:

DroneEventListener droneEventListener = new DroneEventListener() { @Override public void temperatureMeasured(EventObject arg0) { readTemperatureFromDrone(); } @Override public void pressureMeasured(EventObject arg0) { /*...*/ } // Etc... @Override public void disconnectEvent(EventObject arg0) { // Let the user know the bluetooth connection was lost } @Override public void connectEvent(EventObject arg0) { // Let the user know the device was connected } }; 

However, I want to work with another library called funf to get information from Android's built-in sensors. This library is ideal for collecting data for both Android's built-in sensors and external sensors. This allows you to create a custom implementation of Probe . But, it is understood that the Probe (as suggested by the library and convention) is used for only one sensor at a time.

The original question:

So my question is this; Is it possible to somehow subdivide the DroneEventListener () class into specific sensors? After that, I could easily create my own probe for each sensor on the multisensor.

I understand that it can be bad practice to try and subdivide an abstract class, because it is conceptually a contract for the developer. However, I feel that soft hacking this code, working with an already existing, reliable and well-kept library ( funf ) is worth it.

Alternatively, are there any other creative solutions that would allow me to use the manufacturer library with funf?

Edit (June 24, 2013):

I decided that my use of the subdivide wording was clear to me. I essentially meant that I did not want to implement 20 empty methods when all that I used was one. The selected answer explains how to do this.

Clarified Question:

Is it possible to implement only one function from EventListener (with several functions) without having to execute messy empty methods?

+7
source share
3 answers

If you want to use only one method to handle all events, you can create an abstract class that handles redirection.

Thus, you will need to implement this method to handle everything.

 public abstract DroneEventAdapter implements DroneEventListener { public abstract void eventOccured(EventObject event); @Override public void temperatureMeasured(EventObject event) { eventOccured(event); } @Override public void pressureMeasured(EventObject event) { eventOccured(event); } @Override public void disconnectEvent(EventObject event) { eventOccured(event); } @Override public void connectEvent(EventObject event) { eventOccured(event); } // Etc. } 

If you only need to implement some of the events and not have empty stubs for everything else, you can create an abstract class that implements each event using an empty method. It is called the “Adapter” and is quite common in the JDK (see MouseMotionListener and MouseMotionAdapter .

When you extend this class, you can only override the events that you want to support.

 public abstract DroneEventAdapter implements DroneEventListener { @Override public void temperatureMeasured(EventObject event) {} @Override public void pressureMeasured(EventObject event) {} @Override public void disconnectEvent(EventObject event) {} @Override public void connectEvent(EventObject event) {} // Etc. } 
+2
source

You cannot break the DroneEventListener, but you can pass its event processing to another object for each sensor.

 class SplittingListener implements Drone.DroneEventListener { public void adcMeasured(java.util.EventObject event) { adcProbe.heyAThingHappened(event); } public void altitudeMeasured(java.util.EventObject event) { altitudeProbe.itsYourProblemNow(event); } public void capacitanceMeasured(java.util.EventObject event) { capacitanceProbe.passTheBuck(event); } // ... } 
+2
source

My answer to your sophisticated question: I believe that you can implement only one EventListener function, however you still need to have all the methods in the code that it requires, even as empty stubs. I came across this several times, and I always had those that I did not need to implement as empty stubs below.

0
source

All Articles