ASCE1885 Design Pattern --- Observer Pattern

Source: Internet
Author: User

The observer mode defines a one-to-many dependency between objects. When the State of an object changes, all objects dependent on it are notified and updated automatically. In this mode, the key objects are the target (subject) and The observer (observer ). A target can have any number of observer dependent on it. Once the status of the target changes, all observers are notified. As a response to this notification, each observer queries the target to synchronize its status with that of the target. This interaction is also called the publish-subscribe mode. The target is the publisher of the notification. It does not need to know who is its observer when sending the notification. It can have any number of observers to subscribe to and receive the notification.

 

The observer mode is applicable to the following situations:

1) When an abstract model has two aspects, one of which depends on the other, the two are encapsulated in an independent object so that they can be changed and reused independently;

2) When an object needs to change other objects at the same time, it does not know how many objects need to be changed;

3) when an object must notify other objects, it cannot assume who the other objects are. That is, we do not want these objects to be tightly coupled.

 

The structure of the observer mode is as follows:

 


 

The observer mode involves:

1) target Subject:

The observer who knows the object can have multiple observers to observe the same object. It provides interfaces for registering and deleting the object;

2) Observer:

Defines an update interface for objects that need to be notified when the target changes;

3) Target ConcreteSubject:

Stores related states into various ConcreteObserver objects. When its status changes, it sends a notification to its various observers;

4) Specific observer ConcreteObserver:

Maintain a reference pointing to the ConcreteSubject object. Store the relevant States, which should be consistent with the target State. Implement the Observer update interface to keep the status consistent with the target State;

 

Implementation:

Observer interface:

Class Subject;

Class Observer

{

Public:

Virtual ~ Observer ();

Virtual void Update (Subject * m_subject) = 0;

Protected:

Observer ();

};

This implementation method supports multiple goals for one observer. When the observer observes multiple goals, the target passed as a parameter to the Update operation allows the observer to determine which target has changed.

 

Target Interface:

Class Subject

{

Public:

Virtual ~ Subject ();

Virtual void Attach (Observer *);

Virtual void Detach (Observer *);

Virtual void Notify ();

Protected:

Subject ();

Private:

Std: list <Observer *> * _ observers;

};

 

Subject: Subject ()

{

// Remember to initialize before use

_ Observers = new std: list <Observer *>;

}

 

Void Subject: Attach (Observer * observer)

{

_ Observers-> push_back (observer );

}

 

Void Subject: Detach (Observer * observer)

{

If (observer! = NULL)

_ Observers-> remove (observer );

}

 

Void Subject: Policy ()

{

Std: list <Observer * >:: iterator it = _ observers-> begin ();

While (it! = _ Observers-> end ())

{

(* It)-> Update (this );

++ It;

}

}

 

The following example is from Wikipedia:

# Include <list>

# Include <vector>

# Include <algorithm>

# Include <iostream>

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 required yob () = 0;

};

 

// The Concrete Subject

Class ParaWeatherData: public WeatherDataInterface

{

Public:

Void SensorDataChange (float a, float B, float c)

{

M_humidity =;

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 required yob ()

{

List <ObserverBoardInterface *>: iterator pos = m_obs.begin ();

While (pos! = M_obs.end ())

{

(ObserverBoardInterface *) (* pos)-> update (m_humidity, m_temperature, m_pressure );

(Dynamic_cast <DisplayBoardInterface *> (* pos)-> show ();

++ Pos;

}

}

 

Private:

Float m_humidity;

Float m_temperature;

Float m_pressure;

List <ObserverBoardInterface *> m_obs;

};

 

// A Concrete Observer

Class CurrentConditionBoard: public ObserverBoardInterface, public DisplayBoardInterface

{

Public:

CurrentConditionBoard (WeatherDataInterface & a): m_data ()

{

M_data.registerob (this );

}

Void show ()

{

Cout <"_____ CurrentConditionBoard _____" <endl;

Cout <"humidity:" <m_h <endl;

Cout <"temperature:" <m_t <endl;

Cout <"pressure:" <m_p <endl;

Cout <"___________________________" <endl;

}

 

Void update (float h, float t, float p)

{

M_h = h;

M_t = t;

M_p = p;

}

 

Private:

Float m_h;

Float m_t;

Float m_p;

WeatherDataInterface & m_data;

};

 

// A Concrete Observer

Class StatisticBoard: public ObserverBoardInterface, public DisplayBoardInterface

{

Public:

StatisticBoard (WeatherDataInterface & a): m_maxt (-1000), m_mint (1000), m_avet (0), m_count (0), m_data ()

{

M_data.registerob (this );

}

 

Void show ()

{

Cout <"________ StatisticBoard _________" <endl;

Cout <"lowest temperature:" <m_mint <endl;

Cout <"highest temperature:" <m_maxt <endl;

Cout <"average temperature:" <m_avet <endl;

Cout <"___________________________" <endl;

}

 

Void update (float h, float t, float p)

{

+ M_count;

If (t> m_maxt)

{

M_maxt = t;

}

If (t <m_mint)

{

M_mint = t;

}

M_avet = (m_avet * (m_count-1) + t)/m_count;

}

 

Private:

Float m_maxt;

Float m_mint;

Float m_avet;

Int m_count;

WeatherDataInterface & m_data;

};

 

 

Int main (int argc, char * argv [])

{

 

ParaWeatherData * wdata = new ParaWeatherData;

CurrentConditionBoard * currentB = new CurrentConditionBoard (* wdata );

StatisticBoard * statisticB = new StatisticBoard (* wdata );

 

Wdata-> SensorDataChange (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,190 0 );

 

Delete statisticB;

Delete currentB;

Delete wdata;

 

Return 0;

}

 

From ASCE1885

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.