Click here to Skip to main content
15,886,873 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 55.7K   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.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using MB.Propagators;

namespace PropagatorDemo
{
    /// <summary>
    /// Control for choosing color.
    /// </summary>
    public partial class ColorControl : UserControl
    {
        private Color _currentColor = Color.Black;
        private Propagator _propagator = new Propagator("ColorControl");

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

            _propagator.AddHandler(ColorChange.ID, HandleColorChange);
            _propagator.AddHandler(FontChange.ID, HandleFontChange);
        }

        /// <summary>
        /// Access to propagator.
        /// </summary>
        public IPropagator Propagator
        {
            get
            {
                return _propagator;
            }
        }

        /// <summary>
        /// Select new color.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonSelectColor_Click(object sender, EventArgs e)
        {
            ColorDialog colorDialog = new ColorDialog();
            colorDialog.Color = _currentColor;

            DialogResult result = colorDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                ColorChange colorChange = new ColorChange(colorDialog.Color);
                _propagator.Process(colorChange);
            }
        }

        /// <summary>
        /// Handle color change event.
        /// </summary>
        /// <param name="stateChange"></param>
        private void HandleColorChange(StateChange stateChange)
        {
            ColorChange colorChange = stateChange as ColorChange;
            if (colorChange != null)
            {
                Color newColor = colorChange.Color;

                // Set color of example text.
                labelExample.ForeColor = newColor;

                // Set background of color panel.
                panelColor.BackColor = newColor;

                // Remember the current color.
                _currentColor = newColor;
            }
        }

        /// <summary>
        /// Handle font change event.
        /// </summary>
        /// <param name="stateChange"></param>
        private 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)


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