Observer Pattern In Cee Plus Plus

Andrei Alexandrescu has written two articles which discuss different ways of implementing the Observer Pattern in Cee Plus Plus.

In this model all the observers inherit from an observer class and use this to connect to the subject which needs to be observed.

In the paper Functional Tools For Object Oriented Tasks Yannis Smaragdakis and Brian McNamara propose a different way of implementing the Observer Pattern using Functoids In Cpp.

Functional Pattern System For Object Oriented Design gives a framework for the same ground and is referred to by Smaragdakis and McNamara.

I want to look at the implications of the interesting paper Deprecating The Observer Pattern where the coding is in Scala Language.

I now have a version of this code which uses Functoids In Cpp with an extra layer of the Pimpl Idiom to make it work with CUDA C. See Programming Cuda Cee for explanation.


Example

Simulating a chemical plant where Feed is a feed of material into a Stream. The Unit mixer has two streams going into it (using overloaded < ).

Feed feed1(1000.); Feed feed2(2000.); Stream stream1(feed1); Stream stream2(feed2); Unit mixer; mixer < stream1; mixer < stream2;

A later call to change the mass of the feed causes a cascade of updates to all the other plant items which depend on feed1. This can be generalised to any change of parameter.

feed1.Set''''''Mass(2000.);


These techniques could be used to implement updates for Functional Reactive Programming. In fact the way the example is put together appears to fit the definition of Reactive Programming on that page.


See original on c2.com