Many people like watching box fights. But besides of this, someone have to pay for all of this, and most of the money come from people who put some money on boxers and then lose it. Or… maybe I’m wrong, but for this example, let it be so. Imagine that we have box fight tonight and there are two persons interested in game, so they OBSERVE fight and UPDATE their rates. One of them likes risks, so he always put money on boxer who has less chance to win, and other is very conservative and likes small bird in hand instead of big in the sky.

OBSERVER

Observer is design pattern, that allows automatically update many observers if there was change in state of some subject object.

So, each gambler (Player, Observer) likes to adjust his rates, that is why he has Update() method.

    public interface IObserver
{
void Update(ISubject subject);
}

public class RiskyPlayer : IObserver
{
public string BoxerToPutMoneyOn { get; set; }

public void Update(ISubject subject)
{
var boxFight = (BoxFight)subject;

BoxerToPutMoneyOn = (boxFight.BoxerAScore > boxFight.BoxerBScore) ? "I put on boxer B, if he win I get more!" : "I put on boxer A, if he win I get more!";


Console.WriteLine("RISKYPLAYER:{0}", BoxerToPutMoneyOn);
}
}

public class ConservativePlayer : IObserver
{
public string BoxerToPutMoneyOn { get; set; }

public void Update(ISubject subject)
{
var boxFight = (BoxFight)subject;

BoxerToPutMoneyOn = (boxFight.BoxerAScore < boxFight.BoxerBScore) ? "I put on boxer B, better be safe!" : "I put on boxer A, better be safe!";

Console.WriteLine("CONSERVATIVEPLAYER:{0}", BoxerToPutMoneyOn);
}
}

Here below we have subject which is observed by players:

    public interface ISubject
{
void AttachObserver(IObserver observer);
void DetachObserver(IObserver observer);
void Notify();
}

public class BoxFight : ISubject
{
public List<IObserver> Observers { get; private set; }
public int RoundNumber { get; private set; }

private Random Random = new Random();

public int BoxerAScore { get; set; }
public int BoxerBScore { get; set; }

public BoxFight()
{
Observers = new List<IObserver>();
}

public void AttachObserver(IObserver observer)
{
Observers.Add(observer);
}

public void DetachObserver(IObserver observer)
{
Observers.Remove(observer);
}

public void NextRound()
{
RoundNumber++;

BoxerAScore += Random.Next(0, 5);
BoxerBScore += Random.Next(0, 5);

Notify();
}

public void Notify()
{
foreach (var observer in Observers)
{
observer.Update(this);
}
}
}

Usage, or how we build one-to-many relationship by attaching observer-s:

            var boxFight = new BoxFight();

var riskyPlayer = new RiskyPlayer();
var conservativePlayer = new ConservativePlayer();

boxFight.AttachObserver(riskyPlayer);
boxFight.AttachObserver(conservativePlayer);


boxFight.NextRound();
boxFight.NextRound();
boxFight.NextRound();
boxFight.NextRound();

Output:

RISKYPLAYER:I put on boxer A, if he win I get more!
CONSERVATIVEPLAYER:I put on boxer B, better be safe!

RISKYPLAYER:I put on boxer B, if he win I get more!
CONSERVATIVEPLAYER:I put on boxer A, better be safe!

RISKYPLAYER:I put on boxer B, if he win I get more!
CONSERVATIVEPLAYER:I put on boxer A, better be safe!

RISKYPLAYER:I put on boxer B, if he win I get more!
CONSERVATIVEPLAYER:I put on boxer A, better be safe!

 

Been very short explanation. Just take closer look on interfaces in examples, this should be enough to understand everything.

My design patterns table