2010-03-03 85 views

回答

1

退房DOFactory

他們有:

  • UML圖的優點缺點
  • 總結主要項目涉及C#
  • 樣本問題實現C#實現。
5

維基百科最好的總結是:

觀察者模式(在 的一個子集發佈/訂閱模式)是一種 設計模式,其中 對象,叫做主題,保持 一其家屬名單,稱爲 觀察員,並自動通知他們 任何狀態更改, 通常通過調用他們的 方法之一。它主要用於實現分佈式事件處理 系統的 系統。

觀察者(有時也被稱爲publish-subscribe圖案)最好在GUI接口用於更新變化對GUI對象的狀態,例如所有其他對象可以更新本身(例如調整大小的窗口,那麼所有的GUI對象,例如按鈕可以根據窗口大小重新對齊自己)。這通常是通過引入EventListeners(這是一個觀察者模式)完成的。

希望這有助於。

6

Observer模式在C#中非常具體化爲事件。

+0

這是唯一正確的答案。觀察者是用C#語言實現的。 – 2010-03-03 11:23:27

+2

@Vasiliy ....他問什麼是Observer模式。告訴OP,C#中的事件是觀察者模式並不能解釋觀察者是什麼。 – 2010-03-03 11:45:49

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... 
}