Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Implementing a Subject/Observer pattern with templates

, 10 Mar 2003
Using C++ Templates to overcome some of the original Subject/Observer design pattern problems
subjectobserver_source.zip
#include <iostream>
#include <vector>

template <class T>
class Observer
   {
   public:
      Observer() {}
      virtual ~Observer() {}
      virtual void update(T *subject)= 0;
   };

template <class T>
class Subject
   {
   public:
      Subject() {}
      virtual ~Subject() {}
      void attach (Observer<T> &observer)
         {
         m_observers.push_back(&observer);
         }
      void notify ()
         {
         std::vector<Observer<T> *>::iterator it;
         for (it=m_observers.begin();it!=m_observers.end();it++) (*it)->update(static_cast<T *>(this));
         }
   private:
      std::vector<Observer<T> *> m_observers;
   };

class Temperature : public Subject<Temperature>
   {
   public:
      Temperature() {}
      ~Temperature() {}
      void temperatureChanged () {notify();}
      void getTemperature() {std::cout << "   Getting the temperature." << std::endl;}
   };

class Pressure : public Subject<Pressure>
   {
   public:
      Pressure() {}
      ~Pressure() {}
      void pressureChanged () {notify();}
      void getPressure() {std::cout << "   Getting the pressure." << std::endl;}
   };

class EnvironmentWindow : public Observer<Temperature>, public Observer<Pressure>
   {
   public:
      EnvironmentWindow() {}
      ~EnvironmentWindow() {}
      void update (Temperature *subject) {std::cout << "Temperature was changed" << std::endl; subject->getTemperature();}
      void update (Pressure    *subject) {std::cout << "Pressure was changed"    << std::endl; subject->getPressure   ();}
   };

class PanicSirene : public Observer<Temperature>
   {
   public:
      PanicSirene() {}
      ~PanicSirene() {}
      void update (Temperature *subject)
         {
         std::cout << "Temperature was changed, sound the sirene" << std::endl;
         subject->getTemperature();
         }
   };

int main()
{
Temperature       temp;
Pressure          press;
EnvironmentWindow win;
PanicSirene       panic;

temp .attach (win  );
temp .attach (panic);
press.attach (win  );

temp.temperatureChanged ();
press.pressureChanged ();

return (0);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author


| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 11 Mar 2003
Article Copyright 2002 by Patje
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid