In my current project, I am trying to make a uniform interfaces of all sensor driver.
For instance, Temperature sensor has a temperature sensor driver to get data from
it.
Now, my problem is each sensor response with its own data Object. I have written following example of Temperature Sensor. How can I make sensor driver interface uniform, so programmer should only know SensorResonse (not TempSensorResponse).
public class TempSensor implements Sensor {
/**
* Returns a SensorInfo object that describes this sensor.
*/
@Override
public TempSensorInfo getSensorInfo() {
// TODO Auto-generated method stub
return null;
}
/**
* Asks the sensor for a (possibly old) datapoint. Synchronous: returns
* immediately, even if that means returning an old value.
*/
@Override
public TempResponse getData() {
// TODO Auto-generated method stub
return null;
}
/**
* Asks the sensor for a new datapoint. Asynchronous.
*
* @param handler A Handler object to be executed when the sensor has a
* new value. If this Sensor is event-based, this method starts listening
* for data, and calls the handler whenever new events are detected.
*/
@Override
public void getData(SensorListener handler) {
// TODO Auto-generated method stub
}
}
/**
* A SensorResponse is the response that a sensor passes to its callee whenever
* it is asked for some data. It carries both the sensor data itself as it does
* some metadata about it, such as the SensorInfo of the sensor that produced
* this response.
*/
public class TempResponse extends SensorResponse {
public TempResponse(TempSensorInfo sensorInfo, TempSensorData payload) {
super(sensorInfo, payload);
// TODO Auto-generated constructor stub
}
}
Use generics:
Make an interface/base classes.
Make your sensors implement/extend the interface/classes:
Without using Generics:
Base interface/classes
Extended interface/classes