在我当前的项目中,我试图为所有传感器驱动程序制作一个统一的接口(interface)。
例如,温度传感器
有一个温度传感器驱动程序可以从中获取数据
它。
现在,我的问题是每个传感器都有自己的数据对象响应。我写了以下温度传感器的示例。如何使传感器驱动程序接口(interface)统一,因此程序员应该只知道SensorResonse(而不是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
}
}
/** * SensorResponse 是传感器在任何时候传递给其被调用者的响应 * 要求提供一些数据。它本身携带传感器数据 * 有关它的一些元数据,例如产生的传感器的SensorInfo * 这个回应。 */
public class TempResponse extends SensorResponse {
public TempResponse(TempSensorInfo sensorInfo, TempSensorData payload) {
super(sensorInfo, payload);
// TODO Auto-generated constructor stub
}
}
最佳答案
使用泛型:
创建一个接口(interface)/基类。
public interface ISensor
<I extends ISensor.SensorInfo,
R extends ISensor.SensorResponse,
L extends ISensor.SensorListener>
{
/**
* Returns a SensorInfo object that describes this sensor.
*/
public I getSensorInfo();
/**
* Asks the sensor for a (possibly old) datapoint. Synchronous: returns
* immediately, even if that means returning an old value.
*/
public R getData();
/**
* 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.
*/
public void getData(L handler);
public static class SensorInfo { }
public static class SensorResponse { }
public static class SensorListener { }
}
让您的传感器实现/扩展接口(interface)/类:
public class TemperatorSensor
implements ISensor
<TemperatorSensor.TemperatorInfo,
TemperatorSensor.TemperatorResponse,
TemperatorSensor.TemperatorListener>
{
@Override
public TemperatorInfo getSensorInfo() {
// TODO Auto-generated method stub
return null;
}
@Override
public TemperatorResponse getData() {
// TODO Auto-generated method stub
return null;
}
@Override
public void getData(TemperatorListener handler) {
// TODO Auto-generated method stub
}
public static final class TemperatorInfo extends ISensor.SensorInfo { }
public static final class TemperatorResponse extends ISensor.SensorResponse { }
public static final class TemperatorListener extends ISensor.SensorListener { }
}
不使用泛型:
基础接口(interface)/类
public interface ISensor {
/**
* Returns a SensorInfo object that describes this sensor.
*/
public ISensor.SensorInfo getSensorInfo();
/**
* Asks the sensor for a (possibly old) datapoint. Synchronous: returns
* immediately, even if that means returning an old value.
*/
public ISensor.SensorResponse getData();
/**
* 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.
*/
public void getData(ISensor.SensorListener handler);
public static class SensorInfo { }
public static class SensorResponse { }
public static class SensorListener { }
}
扩展接口(interface)/类
public class TemperatorSensor implements ISensor {
@Override
public SensorInfo getSensorInfo() {
// TODO Auto-generated method stub
return new TemperatorInfo();
}
@Override
public SensorResponse getData() {
// TODO Auto-generated method stub
return new TemperatorResponse();
}
@Override
public void getData(SensorListener handler) {
if (handler instanceof TemperatorListener) {
// TODO Auto-generated method stub
}
}
public static final class TemperatorInfo extends ISensor.SensorInfo { }
public static final class TemperatorResponse extends ISensor.SensorResponse { }
public static final class TemperatorListener extends ISensor.SensorListener { }
}
关于java - 传感器驱动程序中的统一接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10785326/