Observer Pattern in Embedded System

Observer Pattern in Embedded System

https://github.com/JEENAFST/Design-Pattern.git

https://onlinegdb.com/0-TH3PVkR

The Observer Pattern is commonly used in embedded systems for monitoring state changes across various components, such as sensors, actuators, and communication modules. This design pattern is useful when multiple components need to react to changes in another component's state.

Observer Pattern Overview in Embedded Systems

In embedded systems, components are often constrained by limited resources and real-time requirements. The observer pattern can help improve modularity and scalability by allowing components to operate independently while still being notified of state changes. This pattern can also reduce polling overhead by allowing components to react only when relevant changes occur, thus conserving CPU and power resources.


Article content


Article content


Article content

Explanation

  • Observer (Interface): Defines the update method that each observer will implement to react to changes in the Vehicle state.
  • Concrete Observers:Engine: Implements update to start or stop the engine based on the state.Airbags: Implements update to deploy airbags when the state indicates a crash.CollisionSensor: Implements update to detect a collision and trigger a state change in Vehicle, which, in turn, deploys airbags.
  • Vehicle (Subject):Maintains a list of observers and has methods to attach observers, notify them of state changes, and count observers.Notifies observers of state changes when setState is called.


Explanation of Classes and Workflow

  1. Observer Interface: The Observer class is an interface (or abstract base class) with a virtual update function. This function is overridden by each observer to define specific behavior when the vehicle’s state changes. The update method takes an int state parameter, which is used to determine the state of the Vehicle.
  2. Vehicle Class (Subject): The Vehicle class is the "subject" in the observer pattern. It maintains a list of observers (std::vector<std::shared_ptr<Observer>>) and a state variable to represent the vehicle's current state. The attach method allows observers to be added to the vehicle. The setState method changes the state of the vehicle and calls notify to update all attached observers about the new state. The notify method iterates over all observers and calls each observer’s update method, passing the current state.
  3. Engine Class (Observer): The Engine class inherits from Observer and defines behavior in response to the vehicle's state changes. When the state is 1, the engine starts and outputs "Engine started...". When the state is 0, the engine stops and outputs "Engine stopped...".
  4. Airbags Class (Observer): The Airbags class also inherits from Observer and responds specifically when the vehicle's state is 2 (indicating a crash). If state is 2, it prints "Airbags deployed!" to simulate airbags being deployed.
  5. CollisionSensor Class (Observer): The CollisionSensor class is an observer that monitors for collision events. It has a reference to the Vehicle object and overrides the update method. When it detects a state of 3, it prints "Collision detected by sensor!" and then changes the vehicle’s state to 2 by calling vehicle.setState(2). This triggers a crash response, activating the Airbags.
  6. Main Function (Usage): An instance of Vehicle is created, and three observers (Engine, Airbags, and CollisionSensor) are attached to it. The vehicle state is set to 1 to start the engine. This change triggers the Engine observer, which outputs "Engine started...". The vehicle state is then set to 3, simulating a collision detection. This triggers the CollisionSensor, which sets the vehicle's state to 2. When the state becomes 2, the Airbags observer responds, deploying the airbags and printing "Airbags deployed!".

Above, I attached the project link, which demonstrates the usage of observer patterns in this scenario.


To view or add a comment, sign in

More articles by Jeena George

  • C vs C++ vs Rust — who manages memory best?

    Memory is the heartbeat of systems programming. But how we handle it — allocation, usage, and cleanup — differs across…

    19 Comments
  • Ever wondered how C programs manage memory—with and without an OS?

    Whether you're building embedded systems or writing high-level applications, understanding the memory layout of C…

    2 Comments
  • Inter-Process Communication (IPC) Using Signals in Linux

    In modern software systems, especially in embedded, automotive, and Linux-based platforms, multiple processes often…

    2 Comments
  • Exception Handling in Embedde C++

    Exception handling in C++ allows you to gracefully handle errors or exceptional conditions that may occur during…

    1 Comment
  • Testing FreeRTOS on Ubuntu

    Testing FreeRTOS APIs without a microcontroller typically involves running FreeRTOS on a host system (like your Ubuntu…

    12 Comments
  • Unwinding in Recursive function

    Recursive functions can be a powerful tool in programming, but it's important to ensure that the recursion has a base…

    3 Comments
  • Message Queue for inter process communication.

    This project is just to demonstrate how message queue can be used for inter process communication. Here producer…

Explore content categories