Click here to Skip to main content
15,897,360 members
Articles / Desktop Programming / Win32

Implement Observer Pattern in an Absolutely Easy Example

Rate me:
Please Sign up or sign in to vote.
2.84/5 (21 votes)
16 Sep 2008CPOL3 min read 73.7K   1.8K   31  
Observer pattern in C# using Event and Delegate
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Windows.Forms;
using System.Drawing;

namespace EventAndDelegate
{
    public class Observer
    {
        private delegate void ColorEventHandler(string _color);
        private event ColorEventHandler ColorChangedEvent;
        private TextBox txt;

        public Observer(TextBox _txt)
        {
            this.ColorChangedEvent += new ColorEventHandler(Observer_ColorChangedEvent);
            this.txt = _txt;
        }

        private void OnChange(string _color)
        {
            if (ColorChangedEvent != null)
            {
                ColorChangedEvent(_color);
            }
        }

        public void Update(string _color)
        {
            OnChange(_color);
        }

        private void Observer_ColorChangedEvent(string _color)
        {
            switch (_color)
            { 
                case "RED":
                    txt.BackColor = Color.Red;
                    break;
                case "BLUE":
                    txt.BackColor = Color.Blue;
                    break;
                case "GREEN":
                    txt.BackColor = Color.Green;
                    break;
                default:
                    txt.BackColor = Color.Gray;
                    break;
            }
        }
    }

    public class Subject
    {
        ArrayList arrObs = new ArrayList();
        private delegate void NotifyHandler(string _color);
        private event NotifyHandler NotifyEvent;

        public Subject()
        {
            this.NotifyEvent += new NotifyHandler(Notify);
        }

        public void UpdateClient(string _color)
        {
            OnNotify(_color);
        }

        private void OnNotify(string _color)
        {
            if (NotifyEvent != null)
            {
                NotifyEvent(_color);
            }
        }

        private void Notify(string _color)
        {
            for (int i = 0; i < arrObs.Count; i++)
            {
                Observer obs = (Observer)arrObs[i];
                obs.Update(_color);
            }
        }

        public void RegisterClient(Observer obs)
        {
            arrObs.Add(obs);
        }
    }
}

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
Web Developer
Vietnam Vietnam
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions