Cruise Controller Model


Design

Class ThrottleController implements main logic of system. It has current state and for execution of concrete actions for concrete state calls method 'update' of current state. Class ICarControlSystem publish interface for interaction with throttle (increase, decrease) as well as calculation of desired acceleration for current and set speeds. Classes ISpeedManager and IStateManager publishes interfaces for changing of speed and state and class ISetupManager inherits both interfaces in order to allow access to this private part of class ThrottleController for all derived from SystemState classes. Class PassiveSystemState implements actions for passive state and sends event to ThrottleController to clear set speed if current speed is below 30mph. Class ActiveSystemState implements actions for active state that do all necessary actions for acceleration control. This class uses interface ICarControlSystem for changing current acceleration.

Realization

Design pattern State was used for realization of this task. Class SystemState through method 'update' isolates class ThrottleController from all concrete actions in concrete state. This class is abstract and all derived classes have to implement method 'update'. Class ThrottleController receives all external events and changes current state on the basis of these events. It creates instance of ActiveSystemState and PassiveSystemState and it is hard-coded because instance of ActiveSystemState can not be shared with other objects and day when we need to add new state can never come. Also, state of ThrottleController can be changed to passive state by ActiveSystemState when resume button was pressed too late (for example, when speed was 32mph) and car was decelerating still and speed fall down to 29 mph. Class TrottleController implements interface ISetupManager in order to allow all classes, derived from SystemState, have access to its private part.