在.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. 在.NET中實現觀察者模式的類的名稱?
- 2. 實現C++ -to-lua觀察者模式?
- 3. 使用winforms實現觀察者模式
- 4. 觀察者模式 - 觀察者創建
- 5. 如何在PHP + Javascript/jQuery上實現觀察者模式?
- 6. 在Delphi中實現觀察者模式的最佳方式
- 7. 觀察者模式
- 8. 如何安全地實現觀察者模式?
- 9. 如何使用Java RMI實現觀察者模式?
- 10. 在C#中使用委託實現的觀察者模式?
- 11. 是否值得在PHP中實現觀察者模式?
- 12. 實施了「靜態觀察者模式」
- 13. 在Java中實現觀察者模式時避免暴露實現細節?
- 14. 重命名Java中觀察者模式的實現方法
- 15. 如何實現觀察者模式以在asp.net中使用用戶控件
- 16. 與觀察者模式
- 17. asp.net觀察者模式
- 18. 觀察者模式vs MVC
- 19. 觀察者模式類(JAVA)
- 20. 觀察者模式混淆
- 21. python觀察者模式
- 22. MVC和觀察者模式
- 23. 觀察者模式過載
- 24. 異步觀察者模式
- 25. Vue.js和觀察者模式
- 26. 爲什麼我們需要觀察者模式?
- 27. 觀察者模式 - 將信息傳遞給觀察者
- 28. 觀察者模式Java - 使用線程的多個觀察者
- 29. 使用Spring依賴注入的觀察者模式實現
- 30. 支持Ajax的WCF服務 - 實現觀察者設計模式
這是唯一正確的答案。觀察者是用C#語言實現的。 – 2010-03-03 11:23:27
@Vasiliy ....他問什麼是Observer模式。告訴OP,C#中的事件是觀察者模式並不能解釋觀察者是什麼。 – 2010-03-03 11:45:49