在.NET中,我们如何实现观察者模式?

3

.NET架构中有很多设计模式。我想了解观察者模式的含义以及如何实现它。

5个回答

6
观察者模式在C#中被称为事件。

这是唯一正确的答案。Observer模式是由C#语言实现的。 - Vasyl Boroviak
2
@Vasiliy....他问什么是观察者模式。告诉OP C#中的事件是观察者模式并不能解释观察者是什么。 - Buhake Sindi

5
维基百科最好地总结了它:
观察者模式(发布/订阅模式的子集)是一种软件设计模式,其中一个名为主题的对象维护其依赖项列表,称为观察者,并通过调用它们的方法自动通知它们任何状态更改。它主要用于实现分布式事件处理系统。
观察者模式(有时称为“发布-订阅”模式)最适用于GUI界面,以更新GUI对象的更改状态,例如所有其他对象都可以更新自身(例如调整窗口大小,然后所有GUI对象(如按钮)都可以根据窗口的大小重新对齐)。这通常通过引入EventListeners(这是观察者模式)来完成。
有关实现,请查看以下教程:
MSDN:http://msdn.microsoft.com/en-us/library/ms998543.aspx 维基百科:http://en.wikipedia.org/wiki/Observer_pattern 希望这可以帮助您。

1
这是一个在C#中简单实现观察者模式的例子,我添加了很多注释,希望能让你清楚地理解它到底是什么 :)
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...
}

1

1

看看DOFactory吧。

他们有:

  • UML图的优点和缺点
  • C#涉及的主要项目摘要
  • C#实现
  • 示例问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接