开发者

How can we implement observer pattern in .NET?

In .NET architecture,there are lot of design patte开发者_开发问答rns.I want to know about what is meant with observer pattern and how it is implemented


The Observer pattern is pretty much reified in C# as events.


Wikipedia summarises it best:

The observer pattern (a subset of the publish/subscribe pattern) is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. It is mainly used to implement distributed event handling systems.

An Observer (sometimes known as publish-subscribe pattern) is best used on GUI interfaces to update states of change on GUI objects such as all other objects can update itself (e.g. resizing a window, then all gui objects such as buttons can re-align itself based on window's size). This is usually done by introducing EventListeners (which is an Observer pattern).

For implementation, you can view the tutorial from either:

  • MSDN: http://msdn.microsoft.com/en-us/library/ms998543.aspx
  • Wikipedia: http://en.wikipedia.org/wiki/Observer_pattern

Hope this helps.


A good example of observer pattern is shown on MSDN.

You can find it here :

http://msdn.microsoft.com/en-us/library/ms998543.aspx


Check out DOFactory.

They have :

  • UML diagrams Advantages Disadvantages
  • Summary of main items involved C#
  • implementation C# implementation for
  • sample problem.


this is a simple implementation of the observer pattern in c#, i added a lot of comments so hopefully it will be clear to you what it actually is :)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SimpleObserver
{
    class Program
    {
        static void Main(string[] args)
        {
            Newspaper newspaper = new Newspaper(); //create a Newspaper, which is a realization of Subject
            Grandma grandma = new Grandma(newspaper); //create a new Grandma, which is a realization of Observer
            newspaper.ChangeNews("no news today..."); //when the news get changed, grandma will automatically get it
            newspaper.ChangeNews("still no news..."); //and again
        }
    }

    //the subject which the observers are 'observing'
    //whenever something changes in the subject, the 
    //observers that are registered should be updated
    public interface Subject 
    {
        void RegisterObserver(Observer o); 
        void RemoveObserver(Observer o);
        void NotifyObservers(); 
    }

    //the actual subject, a newspaper which implements
    //the methods declared in the interface and it's own method
    //the goal is that whenever the news property changes 'by 
    //calling ChangeNews(string newNews); all the registered observers will
    //get that new news
    public class Newspaper : Subject
    {
        private List<Observer> observers = new List<Observer>(); //list with observers
        private string news = "initial news, nothing new!"; //the news

        public void RegisterObserver(Observer o)
        {
            this.observers.Add(o);
        }

        public void RemoveObserver(Observer o)
        {
            this.observers.Remove(o);
        }

        public void NotifyObservers()
        {
            foreach (Observer o in this.observers)
                o.Update(news); //update all the observers with the news
        }

        public void ChangeNews(string newNews) //method to manually change the news
        {
            this.news = newNews;
            this.NotifyObservers(); //automatically calls the NotifyObservers() method
        }
    }

    //the actual observer, has a method Update that will be
    //called by the Subject when something changes
    public interface Observer
    {
        void Update(string news);
    }

    //grandma is a observer, whenever the news changes she will be 
    //notified. she also has a reference to the subject instance, so
    //that she can cancel her subscription whenever needed
    public class Grandma : Observer
    {
        private Subject subject;

        public Grandma(Subject subject)
        {
            this.subject = subject; //set reference to the subject
            this.subject.RegisterObserver(this); //register to the subject
        }

        public void Update(string news)
        {
            Console.WriteLine("Granny reads the news, very slowly...");
            Console.WriteLine("The news today is... " + news);
        }
    }

    //possibly other observers go here...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜