Click here to Skip to main content
15,895,667 members
Articles / Programming Languages / C# 4.0

Propagator in C# - An Alternative to the Observer Design Pattern

Rate me:
Please Sign up or sign in to vote.
4.94/5 (19 votes)
13 Jul 2009CPOL9 min read 56K   368   53  
Re-usable implementation of the Propagator Design Pattern in C#, a potentially more powerful alternative to the well-known Observer Design Pattern.
// Martijn Boeker, July 14, 2009
// License: The Code Project Open License (CPOL) 1.02

using System;
using System.Collections.Generic;
using System.Text;

namespace MB.Propagators
{
    /// <summary>
    /// Object that propagates state changes in a network of dependent objects.
    /// </summary>
    public class Propagator : IPropagator
    {
        #region Delegates and Events

        /// <summary>
        /// Handle state change.
        /// </summary>
        /// <param name="stateChange">Object representing state change.</param>
        public delegate void StateChangeHandler(StateChange stateChange);

        #endregion

        #region Data Members

        /// <summary>
        /// List of propagators to be notified of state changes.
        /// </summary>
        private List<IPropagator> _dependents = new List<IPropagator>();

        /// <summary>
        /// Dictionary of state change type id to state change handler methods.
        /// </summary>
        private Dictionary<int, StateChangeHandler> _handlers = new Dictionary<int, StateChangeHandler>();
        
        /// <summary>
        /// Name of propagator, for debugging purposes.
        /// </summary>
        private string _name;

        #endregion

        #region Constructors

        /// <summary>
        /// Constructor.
        /// </summary>
        public Propagator()
            : this("Unknown")
        {
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="name">Name for debugging purposes.</param>
        public Propagator(string name)
        {
            _name = name;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Get name of propagator for debugging purposes.
        /// </summary>
        public string Name
        {
            get
            {
                return _name;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Process state change: update this propagator to the given state and notify dependents.
        /// </summary>
        /// <param name="message">Message containing state information.</param>
        public void Process(StateChange stateChange)
        {
            Process(stateChange, StateChangeOptions.UpdateAndNotify, this);
        }

        /// <summary>
        /// Process state change.
        /// </summary>
        /// <param name="stateChange">Object containing information about state change.</param>
        /// <param name="options">Indicates how state message should be processed.</param>
        public void Process(StateChange stateChange, StateChangeOptions options)
        {
            Process(stateChange, options, this);
        }

        /// <summary>
        /// Process state change according to state change options.
        /// </summary>
        /// <param name="message">Message containing state information.</param>
        /// <param name="options">Indicates how state change should be processed.</param>
        /// <param name="sender">Propagator who propagated the command, may be null. The command will not be sent in the direction of the propagator.</param>
        public void Process(StateChange stateChange, StateChangeOptions options, IPropagator sender)
        {
            // Make sure this state change has not yet been observed by this propagator.
            if (!stateChange.HasBeenObservedBy(this))
            {
                // Let state change know this propagator has observed it.
                stateChange.AddObservedBy(this);

                // Handle state change if needed.
                if ((options & StateChangeOptions.Update) == StateChangeOptions.Update)
                {
                    // Get message handler delegate.
                    StateChangeHandler handler;
                    if (_handlers.TryGetValue(stateChange.StateChangeTypeID, out handler))
                    {
                        // Invoke message handler delegate.
                        handler(stateChange);
                    }
                }

                // Notify state change if needed.
                if ((options & StateChangeOptions.Notify) == StateChangeOptions.Notify)
                {
                    // Send to dependents.
                    foreach (IPropagator dependent in _dependents)
                    {
                        // For more efficient processing, don't send message in direction of sender.
                        if (dependent != sender)
                        {
                            // Send message to dependent.
                            dependent.Process(stateChange, StateChangeOptions.Notify | StateChangeOptions.Update, this);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Add propagator as dependent.
        /// </summary>
        /// <param name="dependent">Dependent propagator.</param>
        public void AddDependent(IPropagator dependent, bool biDirectional)
        {
            // Add dependent to this propagator.
            if (!_dependents.Contains(dependent))
            {
                _dependents.Add(dependent);
            }

            // If bi-directional, add this propagator to the dependent.
            if (biDirectional)
            {
                dependent.AddDependent(this, false);
            }
        }

        /// <summary>
        /// Remove propagator as dependent.
        /// </summary>
        /// <param name="dependent">Dependent propagator.</param>
        public void RemoveDependent(IPropagator dependent, bool biDirectional)
        {
            // If bi-directional, remove this propagator from dependent.
            if (biDirectional)
            {
                dependent.RemoveDependent(this, false);
            }

            _dependents.Remove(dependent);
        }

        /// <summary>
        /// Remove all child controllers.
        /// </summary>
        /// <param name="biDirectional">If true, dependencies will be removed in both directions.</param>
        public void RemoveAllDependents(bool biDirectional)
        {
            // Create copy of dependents list because items will be removed during iteration.
            List<IPropagator> dependentsCopy = new List<IPropagator>(_dependents);

            // Remove each dependent.
            foreach (IPropagator dependent in dependentsCopy)
            {
                RemoveDependent(dependent, biDirectional);
            }
        }

        /// <summary>
        /// Add state change handler.
        /// </summary>
        /// <param name="stateChangeTypeId"></param>
        /// <param name="handler"></param>
        public void AddHandler(int stateChangeTypeId, StateChangeHandler handler)
        {
            if (!_handlers.ContainsKey(stateChangeTypeId))
            {
                _handlers[stateChangeTypeId] = handler;
            }
        }

        #endregion
    }
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Phi International
Canada Canada
Grew up in Amsterdam, now living in downtown Vancouver. There are definitely more mountains here.

My first internship was with the first company in the Netherlands to teach C++ (www.datasim.nl). During this internship I got to know Object Oriented Design, which kept my interest until this day. In the mean time, I have worked for different companies in the Netherlands and Canada. I have done most of my recent work in C#, developing Database/Web/Desktop applications.

I am currently working as a freelance Software Developer for PHI International in Amsterdam.

The CodeProject rocks!

Comments and Discussions