Monday, October 18, 2010

Propagator Design Pattern


I've encountered this design pattern while I was looking for an existing solution to the problem of propagating an object property to the dependant objects.
Actually the "Propagator" is based on well-known "Observer" design pattern
in which an object known as the Subject holds a list of Observers and notifies them automatically of any state changes.
The difference is that "Propagator" makes it possible to construct each object to be "Subject" and "Observer" interchangeably.

The Propagator

The idea behind the pattern is to establish a network of dependent objects and when a change occurs, push it through propagators to all descendants.

Let's give a brief explanation of each actor in the diagram above:
  1. IPropagator interface contains methods for adding/removing dependent objects and processing state changes.
  2. Propagator class, implements the IPropagator interface and contains
    the "AddHandler" method used for managing a list of delegates to be invoked when a change takes place.
  3. StateChange class represents the change itself.
  4. StateChangeOptions enum allows specifying a change propagation options.
The final usage is quite simple - we just need to call "Process" method of corresponding propagator to have all objects synchronized with the recent change.

The detailed description of Propagator design pattern along with a nice code example could be found in this post by Martijn Boeker.

That's it,

1 comment: