Sensor Driver中的统一接口

时间:2012-05-28 13:17:52

标签: java design-patterns

在我目前的项目中,我正在尝试制作所有传感器驱动程序的统一接口。

例如,Temperature sensor有一个温度传感器驱动程序来从中获取数据 它

现在,我的问题是每个传感器响应都有自己的数据对象。我写了以下温度传感器的例子。如何使传感器驱动程序接口统一,因此程序员应该只知道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
    }

}

2 个答案:

答案 0 :(得分:2)

使用泛型:

创建一个接口/基类。

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 { }
}

让您的传感器实现/扩展接口/类:

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 { }
}

不使用泛型:

基础界面/类

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 { }
}

扩展接口/类

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 { }
}

答案 1 :(得分:0)

除了Sensor类层次结构之外,您很可能还必须创建SensorResponse类层次结构。 getData()方法(应在Sensor基类中定义)将被定义为返回SensorResponse的实例。