3 回答
TA贡献1804条经验 获得超2个赞
观察者模式通常通过事件来实现。
这是一个例子:
using System;
class Observable
{
public event EventHandler SomethingHappened;
public void DoSomething() =>
SomethingHappened?.Invoke(this, EventArgs.Empty);
}
class Observer
{
public void HandleEvent(object sender, EventArgs args)
{
Console.WriteLine("Something happened to " + sender);
}
}
class Test
{
static void Main()
{
Observable observable = new Observable();
Observer observer = new Observer();
observable.SomethingHappened += observer.HandleEvent;
observable.DoSomething();
}
}
有关更多详细信息,请参见链接的文章。
请注意,上面的示例使用C#6 空条件运算符来DoSomething安全地实现以处理SomethingHappened尚未订阅的情况,因此为null。如果您使用的是旧版本的C#,则需要这样的代码:
public void DoSomething()
{
var handler = SomethingHappened;
if (handler != null)
{
handler(this, EventArgs.Empty);
}
}
TA贡献1877条经验 获得超1个赞
这是一个简单的示例:
public class ObservableClass
{
private Int32 _Value;
public Int32 Value
{
get { return _Value; }
set
{
if (_Value != value)
{
_Value = value;
OnValueChanged();
}
}
}
public event EventHandler ValueChanged;
protected void OnValueChanged()
{
if (ValueChanged != null)
ValueChanged(this, EventArgs.Empty);
}
}
public class ObserverClass
{
public ObserverClass(ObservableClass observable)
{
observable.ValueChanged += TheValueChanged;
}
private void TheValueChanged(Object sender, EventArgs e)
{
Console.Out.WriteLine("Value changed to " +
((ObservableClass)sender).Value);
}
}
public class Program
{
public static void Main()
{
ObservableClass observable = new ObservableClass();
ObserverClass observer = new ObserverClass(observable);
observable.Value = 10;
}
}
注意:
这违反了一个规则,即我不将观察者从可观察对象中解脱出来,对于这个简单的示例来说,这也许就足够了,但是请确保您不要让观察者脱离这样的事件。解决此问题的一种方法是使ObserverClass IDisposable,并让.Dispose方法执行与构造函数中的代码相反的操作
不执行错误检查,至少应在ObserverClass的构造函数中进行空检查
TA贡献1808条经验 获得超4个赞
在此模型中,您有一些发布者,他们将做一些逻辑并发布一个“事件”。
然后,发布者将仅向已订阅接收特定事件的订阅者发送事件。
在C#中,任何对象都可以发布其他应用程序可以订阅的一组事件。
当发布类引发事件时,将通知所有订阅的应用程序。
下图显示了这种机制。
在此处输入图片说明
关于C#中事件和委托的最简单示例:
代码是不言自明的,此外,我还添加了注释以清除代码。
using System;
public class Publisher //main publisher class which will invoke methods of all subscriber classes
{
public delegate void TickHandler(Publisher m, EventArgs e); //declaring a delegate
public TickHandler Tick; //creating an object of delegate
public EventArgs e = null; //set 2nd paramter empty
public void Start() //starting point of thread
{
while (true)
{
System.Threading.Thread.Sleep(300);
if (Tick != null) //check if delegate object points to any listener classes method
{
Tick(this, e); //if it points i.e. not null then invoke that method!
}
}
}
}
public class Subscriber1 //1st subscriber class
{
public void Subscribe(Publisher m) //get the object of pubisher class
{
m.Tick += HeardIt; //attach listener class method to publisher class delegate object
}
private void HeardIt(Publisher m, EventArgs e) //subscriber class method
{
System.Console.WriteLine("Heard It by Listener");
}
}
public class Subscriber2 //2nd subscriber class
{
public void Subscribe2(Publisher m) //get the object of pubisher class
{
m.Tick += HeardIt; //attach listener class method to publisher class delegate object
}
private void HeardIt(Publisher m, EventArgs e) //subscriber class method
{
System.Console.WriteLine("Heard It by Listener2");
}
}
class Test
{
static void Main()
{
Publisher m = new Publisher(); //create an object of publisher class which will later be passed on subscriber classes
Subscriber1 l = new Subscriber1(); //create object of 1st subscriber class
Subscriber2 l2 = new Subscriber2(); //create object of 2nd subscriber class
l.Subscribe(m); //we pass object of publisher class to access delegate of publisher class
l2.Subscribe2(m); //we pass object of publisher class to access delegate of publisher class
m.Start(); //starting point of publisher class
}
}
输出:
听众听到
Listener2听到了
听众听到
Listener2听到了
听众听到。。。(无限次)
- 3 回答
- 0 关注
- 632 浏览
添加回答
举报