Skip to content

Latest commit

 

History

History
99 lines (75 loc) · 3.55 KB

README.md

File metadata and controls

99 lines (75 loc) · 3.55 KB

4.0 - Exercise 4 - Interface Segregation Principiple (ISP)

4.1 - Adding motion and heat sensors

📖 A new use case! This is no longer a alarm system for only detecting hazards (smoke and fire), it should now also include security sensors such as motion and heat sensors:

classDiagram
    Sensor <|-- MotionSensor
    Sensor <|-- HeatSensor

    class Sensor
    <<interface>> Sensor
    Sensor: +isTriggered() bool
    Sensor: +getLocation() String
    Sensor: +getSensorType() String
    Sensor: +getBatteryPercentage() double
    
    class MotionSensor{
      +isTriggered() bool
      +getLocation() String
      +getSensorType() String
      +getBatteryPercentage() double
    }
    
    class HeatSensor{
      +isTriggered() bool
      +getLocation() String
      +getSensorType() String
      +getBatteryPercentage() double
    }    
Loading

However, these new security sensors don't run on battery so one of the Sensor interface methods is suddenly redundant for a whole set of sensors.

✏️ Following the Interface Segregation Principle, we can start splitting the current Sensor interface into more fitting ones.

classDiagram
    BatteryDrivenSensor <|-- SmokeSensor
    BatteryDrivenSensor <|-- FireSensor

    Sensor <|-- SmokeSensor
    Sensor <|-- FireSensor
    
    Sensor <|-- MotionSensor
    Sensor <|-- HeatSensor

    class BatteryDrivenSensor
    <<interface>> BatteryDrivenSensor
    BatteryDrivenSensor: +getBatteryPercentage() double

    class Sensor
    <<interface>> Sensor
    Sensor: +isTriggered() bool
    Sensor: +getLocation() String
    Sensor: +getSensorType() String
    
    class SmokeSensor{
      +isTriggered() bool
      +getLocation() String
      +getSensorType() String
      +getBatteryPercentage() double
    }
    
    class FireSensor{
      +isTriggered() bool
      +getLocation() String
      +getSensorType() String
      +getBatteryPercentage() double
    }   

    class MotionSensor{
      +isTriggered() bool
      +getLocation() String
      +getSensorType() String
    }
    
    class HeatSensor{
      +isTriggered() bool
      +getLocation() String
      +getSensorType() String
    }    
Loading

✏️ Create a new BatteryDrivenSensor interface. Move the getBatteryPercentage method from the Sensor interface into this new interface.

✏️ Make the FireSensor and SmokeSensor classes implement BatteryDrivenSensor as well as Sensor.

✏️ Create a new MotionSensor sensor, which inherits from the Sensor interface. These new security sensors should be polled separately from the hazard sensors. This requires a way to distinguish between the two sensor categories. Make changes to the Sensor interface to accomodate this.

4.2 - Adding a time-check rule

✏️ Security sensors should only be polled at night between 22:00-06:00. This is the same for all security sensors. Since we don't want to mix security sensor and hazard sensor behaviour in the same polling mechanism, we decide to make use of inheritance and create a new SecurityControlUnit which extends the existing ControlUnit. Our intention is to pass in the security sensors through the super (parent) constructor and then implement a rule that checks the current time and if it's between 22:00-06:00, we run the super.pollSensors() method which already do the heavy lifting for us.

❓ Which SOLID principle are we maintaining/not breaking by doing this?

✏️ Create the SecurityControlUnit and extend ControlUnit ✏️ Implement the time-check rule and poll the sensors.