====== Observer 패턴 ====== ===== 타입 ===== Behavioral Pattern ===== 문제 ===== ===== 해결 ===== ===== 클래스 다이어그램 ===== {{:programming:design_pattern:observer.png|}} ===== 예제 ===== #include #include #include using namespace std;   // The Abstract Observer class ObserverBoardInterface { public:     virtual void update(float a,float b,float c) = 0; };   // Abstract Interface for Displays class DisplayBoardInterface { public:     virtual void show() = 0; };   // The Abstract Subject class WeatherDataInterface { public:     virtual void registerOb(ObserverBoardInterface* ob) = 0;     virtual void removeOb(ObserverBoardInterface* ob) = 0;     virtual void notifyOb() = 0; };   // The Concrete Subject class ParaWeatherData: public WeatherDataInterface { public:     void SensorDataChange(float a,float b,float c)     {         m_humidity = a;         m_temperature = b;         m_pressure = c;         notifyOb();     }       void registerOb(ObserverBoardInterface* ob)     {         m_obs.push_back(ob);     }       void removeOb(ObserverBoardInterface* ob)     {         m_obs.remove(ob);     } protected:     void notifyOb()     {         list::iterator pos = m_obs.begin();         while (pos != m_obs.end())         {             ((ObserverBoardInterface* )(*pos))->update(m_humidity,m_temperature,m_pressure);             (dynamic_cast(*pos))->show();             ++pos;         }     }   private:     float        m_humidity;     float        m_temperature;     float        m_pressure;     list m_obs; };   // A Concrete Observer class CurrentConditionBoard : public ObserverBoardInterface, public DisplayBoardInterface { public:     CurrentConditionBoard(ParaWeatherData& a):m_data(a)     {         m_data.registerOb(this);     }     void show()     {         cout<<"_____CurrentConditionBoard_____"<m_maxt)         {             m_maxt = t;         }         if (tSensorDataChange(10.2, 28.2, 1001);     wdata->SensorDataChange(12, 30.12, 1003);     wdata->SensorDataChange(10.2, 26, 806);     wdata->SensorDataChange(10.3, 35.9, 900);       wdata->removeOb(currentB);       wdata->SensorDataChange(100, 40, 1900);        delete statisticB;     delete currentB;     delete wdata;       return 0; } ===== 참고 ===== http://en.wikibooks.org/wiki/C%2B%2B_Programming/Code/Design_Patterns#Observer