Click here to Skip to main content
15,867,141 members
Articles / Programming Languages / C#

Design Patterns - Observer Pattern

Rate me:
Please Sign up or sign in to vote.
2.75/5 (9 votes)
25 Jan 2008CPOL1 min read 32.7K   1   14  
Explains the observer pattern and simple implementation
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace observer
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create Table
            ReadyTable table = new ReadyTable(1, "Spade");
            Console.WriteLine("Starting table 1 with suit spades, suit will be changing in 3 seconds");

            //Create Players
            List<Player> players = new List<Player>(4);
            for (int i = 0; i < 4; i++)
            {
                players.Add(new Player(i));
                table.AddPlayer(players[i]);
            }

            Thread.Sleep(3000);
            //Change the suit and all players will be notified
            table.currentSuit = "Hearts";

            Thread.Sleep(6000);
            table.currentSuit = "Diamonds";

            Console.ReadLine();
        }
    }

    abstract class Table
    {
        private string _currentSuit;
        private int _tableNo;
        private List<Player> players = new List<Player>();

        public Table(int _tableNo, string _currentSuit)
        {
            this._tableNo = _tableNo;
            this._currentSuit = _currentSuit;
        }
        public void AddPlayer(Player _player)
        {
            players.Add(_player);
        }

        public void RemovePlayer(Player _player)
        {
            players.Remove(_player);
        }

        public string currentSuit
        {
            get { return _currentSuit; }
            set
            {
                _currentSuit = value;
                Notify();
            }
        }

        public void Notify()
        {
            foreach (Player _player in players)
            {
                _player.Update(this);
            }
        }
    }

    class ReadyTable : Table
    {
        public ReadyTable(int _tableNo, string _currentSuit) : base(_tableNo, _currentSuit)
        { }
    }

    interface IPlayer
    {
        void Update(Table _table);
    }

    class Player : IPlayer
    {
        #region IPlayer Members

        public void Update(Table _table)
        {
            this._table = _table;
            _currentSuit = _table.currentSuit;
            Console.WriteLine("Player '" + _PlayerNo + "' notified that current suit is " + _currentSuit);
        }

        #endregion

        private int _PlayerNo;
        private Table _table;
        private string _currentSuit;

        public Player(int _PlayerNo)
        {
            this._PlayerNo = _PlayerNo; 
        }

        public Table CurrentTable 
        { 
            get { return _table; } 
        }

    }



}

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 Business Development Gate
Egypt Egypt

Comments and Discussions