Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

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

, 13 Jul 2009
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.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MB.Propagators;

namespace PropagatorDemo
{
    /// <summary>
    /// This form contains a user control for selecting a font (FontControl) 
    /// and a user control for selecting a color (ColorControl). Both controls 
    /// display an example text with the current font and color. The demo form
    /// itself also has example text that displays the current font
    /// and color selection.
    /// 
    /// There is a bi-directional propagator link from the DemoForm
    /// to the FontControl and from the DemoForm to the ColorControl.
    /// Any state change is communicated to all propagators in the network.
    /// This demo demonstrates the following routes:
    /// 
    ///  - Initialize/reset: from DemoForm to FontControl and ColorControl
    ///  - Change font: from FontControl to DemoForm and ColorControl
    ///  - Change color: from ColorControl to DemoForm and FontControl.
    /// 
    /// </summary>
    public partial class DemoForm : Form
    {
        private Propagator _propagator = new Propagator("DemoForm");

        /// <summary>
        /// Constructor.
        /// </summary>
        public DemoForm()
        {
            InitializeComponent();

            // Add font and color controls as dependents.
            _propagator.AddDependent(fontControl.Propagator, true);
            _propagator.AddDependent(colorControl.Propagator, true);

            // Add font and color change handlers.
            _propagator.AddHandler(FontChange.ID, HandleFontChange);
            _propagator.AddHandler(ColorChange.ID, HandleColorChange);

            // Initialize font and color.
            Initialize();
        }
                
        /// <summary>
        /// Reset font and color.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonReset_Click(object sender, EventArgs e)
        {
            Initialize();
        }

        /// <summary>
        /// Initialize font and color.
        /// </summary>
        private void Initialize()
        {
            ColorChange colorChange = new ColorChange(Color.Blue);
            _propagator.Process(colorChange);

            Font font = new Font("Arial", 14);
            FontChange fontChange = new FontChange(font);
            _propagator.Process(fontChange);
        }

        /// <summary>
        /// Handle color change event.
        /// </summary>
        /// <param name="stateChange"></param>
        public void HandleColorChange(StateChange stateChange)
        {
            ColorChange colorChange = stateChange as ColorChange;
            if (colorChange != null)
            {
                // Set color of example text.
                labelExample.ForeColor = colorChange.Color;
            }
        }

        /// <summary>
        /// Handle font change event.
        /// </summary>
        /// <param name="stateChange"></param>
        public void HandleFontChange(StateChange stateChange)
        {
            FontChange fontChange = stateChange as FontChange;
            if (fontChange != null)
            {
                // Set font of example text.
                labelExample.Font = fontChange.Font;
            }
        }
    }
}

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)

Share

About the Author

Martijn Boeker
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!

| Advertise | Privacy | Mobile
Web02 | 2.8.140814.1 | Last Updated 14 Jul 2009
Article Copyright 2009 by Martijn Boeker
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid