Csharp/CSharp Tutorial/Design Patterns/Observer Pattern — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 12:17, 26 мая 2010

Observer Design Pattern

using System;
public class ValueChangedEventArgs : EventArgs
{
    public readonly int OldValue, NewValue;
    public ValueChangedEventArgs(int oldTemp, int newTemp)
    {
        OldValue = oldTemp;
        NewValue = newTemp;
    }
}
public delegate void ValueChangedEventHandler(Object sender, ValueChangedEventArgs args);
public class ValueChangeObserver
{
    public ValueChangeObserver(MyValue t)
    {
        t.ValueChanged += this.ValueChange;
    }
    public void ValueChange(Object sender, ValueChangedEventArgs temp)
    {
        Console.WriteLine ("ChangeObserver: Old={0}, New={1}, Change={2}",
            temp.OldValue, temp.NewValue,
            temp.NewValue - temp.OldValue);
    }
}
public class ValueAverageObserver
{
    private int sum = 0, count = 0;
    public ValueAverageObserver(MyValue t)
    {
        t.ValueChanged += this.ValueChange;
    }
    public void ValueChange(Object sender, ValueChangedEventArgs temp)
    {
        count++;
        sum += temp.NewValue;
        Console.WriteLine("AverageObserver: Average={0:F}", (double)sum / (double)count);
    }
}
public class MyValue
{
    private int myvalue = 0;
    public event ValueChangedEventHandler ValueChanged;
    virtual protected void OnValueChanged(ValueChangedEventArgs args)
    {
        if (ValueChanged != null)
        {
            ValueChanged(this, args);
        }
    }
    public int Value
    {
        get { return myvalue; }
        set
        {
            ValueChangedEventArgs args = new ValueChangedEventArgs(myvalue, value);
            myvalue = value;
            OnValueChanged(args);
        }
    }
}
public class MainClass
{
    public static void Main()
    {
        MyValue t = new MyValue();
        new ValueChangeObserver(t);
        new ValueAverageObserver(t);
        t.Value = 100;
        t.Value = 99;
        t.Value = 88;
        t.Value = 77;        
    }
}
ChangeObserver: Old=0, New=100, Change=100
AverageObserver: Average=100.00
ChangeObserver: Old=100, New=99, Change=-1
AverageObserver: Average=99.50
ChangeObserver: Old=99, New=88, Change=-11
AverageObserver: Average=95.67
ChangeObserver: Old=88, New=77, Change=-11
AverageObserver: Average=91.00

Observer Patterns

using System;
  public delegate void ChangedEventHandler(object sender, EventArgs e);
  public class Subject
  {
    private string data;
    public event ChangedEventHandler Changed;
    public string InterestingData
    {
      get{return data;}
      set{
        data = value;
        this.OnChanged(EventArgs.Empty);
      }
    }
    protected virtual void OnChanged (EventArgs e)
    {
      if (Changed !=null)
        this.Changed(this,e);
    }
    public Subject(){;}
  }
  public class Observer
  {
    private Subject data;
    private string observerName;
    public Subject ObservedObject
    {
      get {return data;}
      set {data = value;}
    }
    private void DataChanged (object sender, EventArgs e)
    {
      Console.WriteLine(observerName);
      Console.WriteLine(data.InterestingData);
    }
    public void Attach()
    {
      data.Changed += new ChangedEventHandler(DataChanged);
    }
    public void Detach()
    {
      data.Changed -= new ChangedEventHandler(DataChanged);
    }
    public Observer (string name)
    {
      observerName = name;
    }
  }
  class Client{
    static void Main(string[] args){
      Subject observedAssemblyLine = new Subject();
      Observer observerA = new Observer("A Observer");
      Observer observerB = new Observer("B Observer");
      
      observerA.ObservedObject = observedAssemblyLine;
      observerB.ObservedObject = observedAssemblyLine;
      observerA.Attach();
      observerB.Attach();
      observedAssemblyLine.InterestingData = "# 001 is now finished!";
      observerA.Detach();
      observedAssemblyLine.InterestingData = "# 002 is now finished!";
    }
  }