在.NET架构中,有很多设计模式。我想知道观察者模式的含义及其实现方式我们如何在.NET中实现观察者模式?
3
A
回答
1
1
5
维基百科最好的总结是:
观察者模式(在 的一个子集发布/订阅模式)是一种 设计模式,其中 对象,叫做主题,保持 一其家属名单,称为 观察员,并自动通知他们 任何状态更改, 通常通过调用他们的 方法之一。它主要用于实现分布式事件处理 系统的 系统。
观察者(有时也被称为publish-subscribe
图案)最好在GUI接口用于更新变化对GUI对象的状态,例如所有其他对象可以更新本身(例如调整大小的窗口,那么所有的GUI对象,例如按钮可以根据窗口大小重新对齐自己)。这通常是通过引入EventListeners
(这是一个观察者模式)完成的。
- MSDN:
对于实现,您可以从查看教程http://msdn.microsoft.com/en-us/library/ms998543.aspx
- 百科:http://en.wikipedia.org/wiki/Observer_pattern
希望这有助于。
6
Observer模式在C#中非常具体化为事件。
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. 实现观察者模式
- 2. 在.NET中实现观察者模式的类的名称?
- 3. 实现C++ -to-lua观察者模式?
- 4. 实现观察者模式的片段
- 5. 使用winforms实现观察者模式
- 6. 通过RMI实现观察者模式
- 7. 如何在JNI中实现观察者模式
- 8. 如何在C++中实现观察者模式
- 9. 如何在javascript中实现观察者模式?
- 10. 观察者模式 - 观察者创建
- 11. 如何在PHP + Javascript/jQuery上实现观察者模式?
- 12. 在Delphi中实现观察者模式的最佳方式
- 13. 观察者模式
- 14. 观察者模式:如何区分观察者
- 15. 如何离子实现观察者模式2
- 16. 如何使用Java RMI实现观察者模式?
- 17. 如何安全地实现观察者模式?
- 18. 如何用greenDAO实现观察者模式?
- 19. 如何用CDI Events实现观察者模式?
- 20. 如何使用boost :: signals来实现观察者模式?
- 21. 如何实现这个观察者模式?
- 22. 在C#中使用委托实现的观察者模式?
- 23. 是否值得在PHP中实现观察者模式?
- 24. 如何在C++中实现观察者模式,在JNI中调用Java方法
- 25. 在Magento中实现事件观察者
- 26. 实施了“静态观察者模式”
- 27. 当观察者希望观察不同的项目时,实现观察者模式
- 28. 在Java中实现观察者模式时避免暴露实现细节?
- 29. 重命名Java中观察者模式的实现方法
- 30. 实现观察者模式在Delphi与接口
这是唯一正确的答案。观察者是用C#语言实现的。 – 2010-03-03 11:23:27
@Vasiliy ....他问什么是Observer模式。告诉OP,C#中的事件是观察者模式并不能解释观察者是什么。 – 2010-03-03 11:45:49