Click here to Skip to main content
13,589,266 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


14 bookmarked
Posted 24 Jan 2008
Licenced CPOL

Design Patterns - Observer Pattern

, 25 Jan 2008
Rate this:
Please Sign up or sign in to vote.
Explains the observer pattern and simple implementation


This article explains the Observer pattern which is one of C# Design Patterns. The article provides a very simple implementation so its complexity can be easily understood. The observer pattern makes an object such that if its state changed somehow, other instances will be notified (or updated) automatically.


This is taken from


Imagine if we have a cards game, some tables, and some players. On a table, in the game context the suit of cards is changed periodically and we want to notify the players each time the suit is changed. Note* this is not a complete logic for a cards game, it's an example. The player object is going to have a function Update() which will be called by the notifier, and data:

  • Player Number (Player Identity) int
  • Current Suit string
  • Current Table Object Table

How can the player be notified? We pass the (always changes) object of the table as a parameter in the Update() function, So the player (object) can see the new differences in the table (object). This is the Player object..

interface IPlayer
    void Update(Table _table);

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

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

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

    public Table CurrentTable 
        get { return _table; } 

In our table object, you want to add players or remove players (or count them for some reason). So in the table object, you'll have:

  • An array list of the players objects List<Player>
  • Current Suit string
  • Table Number int
  • Add Player Function AddPlayer()
  • Remove Player Function RemovePlayer()
  • Notify Function Notify()

The notify function sends the updated object (table) to all players objects:

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)

    public void RemovePlayer(Player _player)

    public string currentSuit
        get { return _currentSuit; }
            _currentSuit = value;

    public void Notify()
        foreach (Player _player in players)

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

And the Main():

class Program
    static void Main(string[] args)
        //Create Table
        //specify a table id and a starting suit
        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);//specify number of players
        for (int i = 0; i < 4; i++)
            players.Add(new Player(i));
            //add players to the table

        //Change the suit and all players will be notified
        table.currentSuit = "Hearts";

        table.currentSuit = "Diamonds";



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Islam ElDemery
Web Developer Business Development Gate
Egypt Egypt

You may also be interested in...

Comments and Discussions

GeneralEvents Pin
Steve Hansen24-Jan-08 19:44
memberSteve Hansen24-Jan-08 19:44 
GeneralRe: Events Pin
Islam ElDemery24-Jan-08 23:13
memberIslam ElDemery24-Jan-08 23:13 
GeneralA class diagram is needed.. Pin
Nirosh24-Jan-08 15:48
memberNirosh24-Jan-08 15:48 
GeneralRe: A class diagram is needed.. Pin
Islam ElDemery24-Jan-08 23:08
memberIslam ElDemery24-Jan-08 23:08 
GeneralI am sorry to say this... Pin
Nirosh24-Jan-08 23:48
memberNirosh24-Jan-08 23:48 
GeneralRe: I am sorry to say this... Pin
Islam ElDemery25-Jan-08 0:13
memberIslam ElDemery25-Jan-08 0:13 
GeneralRe: I am sorry to say this... Pin
Nirosh25-Jan-08 3:32
memberNirosh25-Jan-08 3:32 
Just couple of more corrections.. we have to be extra careful with the way we use terminologies when write for beginner.. If you direct a beginner at a wrong point he will go wrong for ever ..

Object - is an instance of a class
Class - is a representation of a type of object. It is the blueprint/ plan/ template that describe the details of an object.

Islam ElDemery wrote:

The Table is abstract class so I can implement as many tables as I want in my game.

I think the reasoning is fine. But the sentence is having some wrong terminology. You can implement an Interface, but not a class, abstract class is of type class, so you derived from it..

L.W.C. Nirosh.
Sri Lanka.

GeneralRe: I am sorry to say this... Pin
Islam ElDemery25-Jan-08 3:38
memberIslam ElDemery25-Jan-08 3:38 
GeneralI know the place... Pin
Nirosh25-Jan-08 3:38
memberNirosh25-Jan-08 3:38 
GeneralRe: I know the place... Pin
Islam ElDemery25-Jan-08 3:42
memberIslam ElDemery25-Jan-08 3:42 
GeneralRe: I know the place... Pin
Nirosh25-Jan-08 3:43
memberNirosh25-Jan-08 3:43 
GeneralRe: I know the place... Pin
Islam ElDemery25-Jan-08 3:49
memberIslam ElDemery25-Jan-08 3:49 
GeneralRe: I am sorry to say this... Pin
Ravi Bhavnani25-Jan-08 1:33
memberRavi Bhavnani25-Jan-08 1:33 
GeneralRe: I am sorry to say this... Pin
Nirosh25-Jan-08 3:11
memberNirosh25-Jan-08 3:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.180615.1 | Last Updated 25 Jan 2008
Article Copyright 2008 by Islam ElDemery
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid