























When we learn the Observer Pattern, we will see the Figure like this:

It come from the << Head.First.Design.Patterns>>. Let us to learn this pattern together....
Observer is a abstract class or interface, it define a series of methods. these methods can be executed when the Subject have a notify to happen. Now, i give some code to analyse it:
namespace ObserverPattern2
{
class Program
{
static void Main(string[] args)
{
Observer1 o1 = new Observer1();
Observer2 o2 = new Observer2();
Subject subject
= new Subject();subject.Notify(
"billok");
Console.ReadLine();
}
}
Observer1 and Observer2 are the implement of abstract Observer. They really finish the Update(string userName) method. This method will be executed when Observer is notified.
How to notify these Observer? It is Subject's duty. ISubject define three methods(RegisterObserver, RemoveObserver, Notify). RegisterObserver is used to register a new Observer, this observer will be notified when subject's Notify method is executed.
RemoveObserver is used to remove existent Observer, then it will be not notified.
Notify is used to tigger an action, this action will be notify all of Observers of the storage of the Subject.
Now, Subject is a implementing class of the ISubject interface.
How to use this Observer Pattern to finish our job? First, we can create a Subject object, and create some Observer's implements. Next, you add the objects to the Subject object by RegisterObserver method. Finally, we execute the Notify method, and all of the Observer will be notified and their Update method will be executed.
Maybe you can take note of my codes is differ from the Figure. In the Figure, ConcreteObserver class have a reference of the ISubject. we can come true it. I think it's duty is simply register itseft to the subject. Let us to add a Isubject reference to the Observer, like this:
public abstract class Observer
{
private ISubject iSubject;private Observer() { }
public Observer(ISubject iSubject)
{
this.iSubject = iSubject;
this.iSubject.RegisterObserver(this);//register itseft
}public abstract void Update(string userName);
}
when we new a Observer object, reference of ISubject is added to the new object. Now, we may modify the using of the RegisterObserve. Please look this:
Subject subject = new Subject();
Observer1 o1 = new Observer1(subject);
Observer2 o2 = new Observer2(subject);
subject.Notify("billok");
Okay, i give another version:
another version
Okay, Now! Let us take attention to the implement of Observer Pattern in C#.
We should use the Delegate and Event to implement the Observer Pattern in the .NET Framework. I give the new edition for .net like it:
namespace ObserverPattern3
{
class Program
{
static void Main(string[] args)
{
Trigger trigger = new Trigger();
trigger.Changed += new Trigger.ChangedEventHandler(trigger_Changed);
trigger.Changed += delegate(object sender, ChangedEventArgs e)
{
Console.WriteLine("delegate Observer: "+e.UserName);
};
trigger.Changed += Observer1;
trigger.Changed += Observer2;
trigger.Update(
"billok");
Console.ReadLine();
}
You can see i define a Delegate variable and Event variable in the Trigger classs.
Please notice how to define the event frame. attention:
How to register and remove the observer? we can use "+=" and "-=" to do it. please look the source code.
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。