I'm building an app that needs to use multiple types of similar sensors. Since the sensors could also have different behaviours, or combinations of behaviours, I decided to use the decorator pattern.
In short, I have a hierarchy that looks like this:
So any of the concrete ISensorDecorator
classes can decorate (wrap) any of the concrete IMeasureSensor
classes, but since a concrete ISensorDecorator
also is a concrete IMeasureSensor
, they can wrap each other. So for example
IMeasureSensor sensor = new FilteredSensorDecorator(
new CalibratedSensorDecorator(
new AccelerometerSensor()
)
);
is a valid statement that declares a filtered and calibrated accelerometer sensor.
Now let's say I have a method called setCalibration()
in CalibratedSensorDecorator
. Obviously I can't call
sensor.setCalibration();
because IMeasureSensor
doesn't have a setCalibration()
method. And trying to use
((CalibratedSensorDecorator)sensor).setCalibration()
won't work either, since sensor
is a FilteredSensorDecorator
.
How can I get to the CalibratedSensorDecorator
in this particular case, and more generally to any specific decorator in any "chain" of decorators? I don't want to store them as separate variables, I want to do it dynamically.
Aucun commentaire:
Enregistrer un commentaire