Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to implement just one function from an EventListener with multiple functions?

I'm developing an app to connect to a bluetooth-based multi-sensor (SensorDrone). It contains about 15 sensors total, all of which can communicate to an Android device using a third-party closed-source library that the manufacturers of the chip created.

The third-party library requires me to implement a custom Java EventListener which contains a function for each sensor that gets fired 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 wish to work with another library called funf to get information from Android's built-in sensors as well. This library is perfect for collecting data for both built-in Android sensors and external sensors. It does this by allowing you to create a custom "Probe" implementation. But, a Probe is meant (by suggestion from the library and convention) to be used for just one sensor at a time.

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 a custom Probe for each sensor on the multi-sensor.

I realize that it may be bad practice to try and subdivide an abstract class because it is conceptually a contract for the developer. However, I feel that a mild hack to get this code working with a pre-existing, reliable and well-maintained library (funf) would be worth it.

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

Edit (June 24, 2013):

I've decided that my use of the wording "subdivide" was only clear to me. I essentially meant that I did not want to implement 20-something blank methods when all I was using was one. The chosen answer explains precisely how to do that.

Refined question:

Is it possible to implement just one function from an EventListener (with multiple functions) without having to implement cluttering empty methods?

like image 252
codepringle Avatar asked Jun 18 '13 02:06

codepringle


2 Answers

If you want to use only one method to handle every events you can create an abstract class that handle the redirection.

This way, you will only have 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 what you need is to only implement some of the events and not have empty stubs for everything else, you can create an abstract class implementing every event with an empty method. It is called an "Adapter" and is pretty frequent in the JDK (see MouseMotionListener and MouseMotionAdapter for example).

When extending this class you will be able to override only the events 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.
}
like image 187
Raphaël Avatar answered Dec 09 '22 02:12

Raphaël


You can't split the DroneEventListener, but you can have it delegate its event handling to a different 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);
    }

    // ...
}
like image 23
user2357112 supports Monica Avatar answered Dec 09 '22 01:12

user2357112 supports Monica