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

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;

namespace www.askbargains.com.ObserverDesignPatternLib
{
    public delegate void NotifyObserver(string key);


    public abstract class ObservableObject
    {
        public event NotifyObserver NotifyObserverEvent;

        public void AddObserver(NotifyObserver ob)
        {
            NotifyObserverEvent += ob;
        }

        public void RemoveObserver(NotifyObserver ob)
        {
            NotifyObserverEvent -= ob;
        }

        public void Notify(string kidName)
        {
            if (NotifyObserverEvent != null)
            {
                NotifyObserverEvent(kidName);
            }
        }
    }
}

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
Web02 | 2.8.140905.1 | Last Updated 9 Aug 2009
Article Copyright 2009 by Allen _ Wang
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid