Thursday, January 24, 2008

Design Patterns - Observer Pattern

Introduction

This article explains the Observer patterns which is one of the C# Design Patterns, the article provides a very simple implementation so its complexity can be easily understood.

The observer pattern makes an object that if its state changed somehow, other instances will be notified (or updated) automatically, In other words, it is used to keep track of many objects.
observer
Implementation

Imagine if we have cards game, some tables, and some players..
On a table.., in the game context the suit of cards is changed time by time and we want to notify the players each time the
suit is changed.

Note* this is not a complete logic for a cards game, its 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 reasons..)
So in the table object you'll have:
  • An array list of the players objects List<>

  • Current Suit string

  • Table Number int

  • Add Player Function AddPlayer()

  • Remove Player Function RemovePlayer()

  • Notify Function Notify()



The notify function is sending the updated object (table) to all players objects..


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

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)
{ }
}



And the Main()

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 players = new List(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();
}
}

4 comments:

Anonymous said...

Great one Islam , but i have two comments :
First , It is not specifc to c#, Design Patterns , is a science used in any object , so it is not one of the c# as you mentioned

Second : and i know that you already know it , but to add more simple defintion : it is used to keep track of more than object , it is the extension of singelton

but really great blog , i am interstred and you give me the motive to start writting in my blog

Islam Eldemery said...

First: This is right design patterns are not related to C# only, they are patterns for Object Oriented, what meant is that the implementation is in C#, check this.

Second: article has been modified, Thank you so much.

Islam Eldemery said...

You should start your blog, So we can learn from you.

Unknown said...

Cool,,Explanation...