StackStalk
  • Home
  • Java
    • Java Collection
    • Spring Boot Collection
  • Python
    • Python Collection
  • C++
    • C++ Collection
    • Progamming Problems
    • Algorithms
    • Data Structures
    • Design Patterns
  • General
    • Tips and Tricks

Wednesday, May 21, 2008

C++ Observer Design Pattern

 May 21, 2008     Design Patterns     No comments   

Observer design pattern is a behavioral design pattern and used to notify multiple objects of a change to keep them in sync like Model-View-Controller (MVC) concept. This article provides an introduction to observer design pattern with a sample implementation in C++.

Observer Design Pattern?

  • Observer pattern is a behavioral design pattern.
  • Observer pattern is used to solve the problem of notifying multiple objects of a change to keep them in sync like the Model-View-Controller (MVC) concept.
  • Useful for event management kind of scenarios.
  • Two classes are involved.
  • The Observable class is where the actual data change is occuring. It has information about all the interested objects that need to be notified.
  • The Observer is typically an abstract class providing the interface to which concrete classes interested in the events need to be compliant to.

Observer Design Pattern implementation

#include <iostream>
#include <set>

using namespace std;

// ---------------- Observer interface -----------------
class MyObserver {
    public:
        virtual void Notify() = 0;
};

// ---------------- Observable object -------------------
class MyObservable {
        static MyObservable* instance;
        set<MyObserver*> observers;
        MyObservable() { };
    public:
       static MyObservable* GetInstance();
       void AddObserver(MyObserver& o);
       void RemoveObserver(MyObserver& o);
       void NotifyObservers();
       void Trigger();
};

MyObservable* MyObservable::instance = NULL;

MyObservable* MyObservable::GetInstance()
{
    if ( instance == NULL ) {
       instance = new MyObservable();
    }

    return instance;
}

void MyObservable::AddObserver(MyObserver& o)
{
    observers.insert(&o);
}

void MyObservable::RemoveObserver(MyObserver& o)
{
    observers.erase(&o);
}

void MyObservable::NotifyObservers()
{
    set<MyObserver*>::iterator itr;
    for ( itr = observers.begin();
          itr != observers.end(); itr++ )
    (*itr)->Notify();
}

// TEST METHOD TO TRIGGER
// IN THE REAL SCENARIO THIS IS NOT REQUIRED
void MyObservable::Trigger()
{
    NotifyObservers();
}

// ------ Concrete class interested in notifications ---
class MyClass : public MyObserver {

        MyObservable* observable;

    public:
       MyClass() {
          observable = MyObservable::GetInstance();
          observable->AddObserver(*this);
       }

       ~MyClass() {
          observable->RemoveObserver(*this);
       }

       void Notify() {
            cout << "Received a change event" << endl;
       }
};

void main()
{
    MyObservable* observable = MyObservable::GetInstance();
    MyClass* obj = new MyClass();
    observable->Trigger();
}
OUTPUT:
Received a change event
  • Share This:  
Newer Post Older Post Home

0 comments:

Post a Comment

Follow @StackStalk
Get new posts by email:
Powered by follow.it

Popular Posts

  • Avro Producer and Consumer with Python using Confluent Kafka
    In this article, we will understand Avro a popular data serialization format in streaming data applications and develop a simple Avro Produc...
  • Monitor Spring Boot App with Micrometer and Prometheus
    Modern distributed applications typically have multiple microservices working together. Ability to monitor and manage aspects like health, m...
  • Server-Sent Events with Spring WebFlux
    In this article we will review the concepts of server-sent events and work on an example using WebFlux. Before getting into this article it ...
  • Implement caching in a Spring Boot microservice using Redis
    In this article we will explore how to use Redis as a data cache for a Spring Boot microservice using PostgreSQL as the database. Idea is to...
  • Python FastAPI microservice with Okta and OPA
    Authentication (AuthN) and Authorization (AuthZ) is a common challenge when developing microservices. In this article, we will explore how t...
  • Spring Boot with Okta and OPA
    Authentication (AuthN) and Authorization (AuthZ) is a common challenge when developing microservices. In this article, we will explore how t...
  • Getting started with Kafka in Python
    This article will provide an overview of Kafka and how to get started with Kafka in Python with a simple example. What is Kafka? ...
  • Getting started in GraphQL with Spring Boot
    In this article we will explore basic concepts on GraphQL and look at how to develop a microservice in Spring Boot with GraphQL support. ...

Copyright © StackStalk