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

Observer Design Pattern (Delegates/Events)

, 9 Aug 2009
This article uses Delegates/Events to implement an Observer Design Pattern.
ObserverDP_src.zip
www.askbargains.com
www.askbargains.com.ObserverDesignPartternClient
bin
Debug
www.askbargains.com.ObserverDesignPartternClient.exe
www.askbargains.com.ObserverDesignPartternClient.pdb
www.askbargains.com.ObserverDesignPartternClient.vshost.exe
www.askbargains.com.ObserverDesignPartternClient.vshost.exe.manifest
www.askbargains.com.ObserverDesignPartternLib.dll
www.askbargains.com.ObserverDesignPartternLib.pdb
obj
Debug
ResolveAssemblyReference.cache
TempPE
www.askbargains.com.ObserverDesignPartternClient.exe
www.askbargains.com.ObserverDesignPartternClient.pdb
Properties
www.askbargains.com.ObserverDesignPartternLib
bin
Debug
www.askbargains.com.ObserverDesignPartternLib.dll
www.askbargains.com.ObserverDesignPartternLib.pdb
obj
Debug
TempPE
www.askbargains.com.ObserverDesignPartternLib.dll
www.askbargains.com.ObserverDesignPartternLib.pdb
ObserverClassDiagram.cd
Properties
www.askbargains.com.suo
Observer_src.zip
www.askbargains.com.ObserverDesignPartternClient.exe
www.askbargains.com.ObserverDesignPartternClient.vshost.exe
www.askbargains.com.ObserverDesignPartternClient.vshost.exe.manifest
www.askbargains.com.ObserverDesignPartternLib.dll
www.askbargains.com.ObserverDesignPartternLib.dll
www.askbargains.com.ObserverDesignPartternLib.pdb
Refactor
www.askbargains.com.ObserverDesignPartternLib.dll
www.askbargains.com.ObserverDesignPartternLib.dll
www.askbargains.com.ObserverDesignPartternLib.pdb
ObserverClassDiagram.cd
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using www.askbargains.com.ObserverDesignPartternLib;

namespace www.askbargains.com.ObserverDesignPartternClient
{
    class Program
    {
        static void Main(string[] args)
        {
            //Two kids been created
            Kid kid1 = new Kid();
            Kid kid2 = new Kid();

            kid1.Name = "Ammie";
            kid2.Name = "Elizabeth";

            //one parent object created. 
            //this parent has two kids in this case. 
            Parent parent1 = new Parent();
            parent1.Kids.Add(kid1.Name, kid1);
            parent1.Kids.Add(kid2.Name, kid2);

            //one family doctor object created
            //this doctor is kid2's family doctor, 
            //and I am going to send the note when kid2 isn't well
            FamilyDoctor doc1 = new FamilyDoctor();
            doc1.Paitients.Add(kid2.Name, kid2);

            //I want to send notes to the parents.
            kid1.AddObserver(new NotifyObserver(parent1.DailyStatusUpdate));
            kid2.AddObserver(new NotifyObserver(parent1.DailyStatusUpdate));

            //Update status for both Kids. Parents will receive the notes at the same time
            kid1.DailyStatus = new Status(String.Format("{0} is happy", kid1.Name));
            kid2.DailyStatus = new Status(String.Format("{0} is fessy", kid2.Name));

            //Updates the status after 5 secs
            Thread.Sleep(5000);

            //after 5 secs, kid2 doesn't feel well. need to get doctor involved
            kid2.AddObserver(new NotifyObserver(doc1.ReciveNotes));

            //update two kids' status. 
            //Parent will recive two kids status
            //Doc1 start reciving kid2 's status
            kid1.DailyStatus = new Status(String.Format("{0} is happy", kid1.Name));
            kid2.DailyStatus = new Status(String.Format("{0} is sick. Tempture : 39.7", kid2.Name));

            //Updates the status after 5 secs
            Thread.Sleep(5000);

            //update two kids' status
            kid1.DailyStatus = new Status(String.Format("{0} is happy", kid1.Name));
            kid2.DailyStatus = new Status(String.Format("{0} is back to normal. she is happy now", kid2.Name));


            //Updates the status after 5 secs
            Thread.Sleep(5000);


            //since kid2 is fine. I am going to deattach the doc1's observation
            kid2.RemoveObserver(new NotifyObserver(doc1.ReciveNotes));

            //update two kids' status
            kid1.DailyStatus = new Status(String.Format("{0} is happy. Just had a big lunch", kid1.Name));
            kid2.DailyStatus = new Status(String.Format("{0} is happy. Palying with her best friend Kevin", kid2.Name));

            Console.WriteLine("Daily Report End!");
            Console.Read();
        }
    }
}

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

Allen _ Wang
Architect
United States United States
Sr Software Architect.Microsoft Certified Solutions Developer (MCSD). owner of www.askbargains.com.

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 9 Aug 2009
Article Copyright 2009 by Allen _ Wang
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid