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

Tagged as

Design Patterns - Observer Pattern

, 25 Jan 2008 CPOL
Explains the observer pattern and simple implementation
observer.zip
observer
observer.suo
observer
bin
Debug
observer.exe
observer.vshost.exe
observer.vshost.exe.manifest
Properties
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)

Share

About the Author

Islam ElDemery
Web Developer Business Development Gate
Egypt Egypt
Blog
Follow on   Twitter

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150326.1 | Last Updated 25 Jan 2008
Article Copyright 2008 by Islam ElDemery
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid