用事件实现观察者模式 [英] Implementing observer pattern with events

查看:73
本文介绍了用事件实现观察者模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Silverlight应用程序,在该应用程序中我过度使用了观察者模式.在我的实现中,我创建了两个接口IObservable<T>IObserver<T>.前者包含将观察者附加到可观察对象上的方法.后者具有方法Notify(IObservable<T> observable, ...),当可观察对象已更改其状态时,可观察对象将调用该方法并通过observer.Notify(this, ...)将其自身作为参数传递.

现在,我偶然发现了事件",对我来说,这似乎是观察者模式的一种实现,只是使用委托而不是上述的Notify方法. 是不是?

我对委托人了解不多,也不想花费数小时来重写我的代码,只是为了获得与已经完成的功能相同的代码.另一方面,事件可能优于基于接口的观察者模式.我想念什么吗?

解决方案

事件是观察者模式的实现.

事件被实现为发出通风口时要调用的方法的列表.

代表是方法引用:与Java相比,C#提供了一种引用方法的方法.

使用事件比自己实现观察者模式更好.但是事件提供了一种非常通用的方式来执行此操作,并且在许多情况下事件已针对该任务进行了高度优化,因此为组织此事件提供了一种更有效,更便捷的方式.

委托由预期方法的签名定义.例如:

public delegate void FooMethod (Bar x, Baz y);

您为给定BarBazvoid方法定义了一个委托.因此,如果您具有以下类的实例:

public class Qux {

    public void Method (Bar x, Baz y) {
        //do something
        return null;
    }

}

然后您可以参考该方法:

Qux q = new Qux();
FooMethod fm = q.Method;

因此event是具有相同签名的delegate的列表:

您将事件定义为:

private event FooMethod SomeEvent;

您可以使用+=运算符添加delegate(侦听器):

SomeEvent += q.Method;

通过-=运算符删除委托,并使用以下命令调用事件:

SomeEvent(new Bar(), new Baz());

就像您调用一个执行分派的方法一样.

通过调用event,将按照注册顺序调用所有已注册的delegate.

注意:通过调用SomeEvent(new Bar(), new Baz());并不意味着每个委托都接收new实例:首先构造实例,然后在所有委托调用中 shared .

结论:我认为C#设计师在介绍观察者模式方面做得很好,因为程序员不再负责自己进行正确的编程.此外,事件易于理解并使用方便的语法.但是,在特殊情况下,可能需要程序员自己实施 observer .但这是非常罕见的情况.

I am working on a Silverlight application where I made excessive use of the observer pattern. In my implementation I have created two interfaces IObservable<T> and IObserver<T>. The former contains methods to attach and detach observers to the observable. The latter has a method Notify(IObservable<T> observable, ...) that is called by the observable passing itself as parameter via observer.Notify(this, ...) when the observable has changed its state.

Now I have stumbled upon "events" and for me it seems pretty much as if this were an implementation of the observer pattern, just with delegates instead of the aforementioned Notify-method. Is that right?

I do not know much about delegates and do not want to spend hours on rewriting my code just to end up with code that does the same thing as it already does. On the other hand events might be superior to the interface-based observer pattern. Am I missing something?

解决方案

Events are an implementation of the observer pattern.

An event is implemented as a list of methods to be called when the vent is raised.

Delegates are method references: in contrast to Java, C# offers a way to refer to a method.

It's not a priori better to use events than implement the observer pattern oneself. But events offer a quite generic way to do it, and are in many cases highly optimized for the task, thus give a more efficient and convenient way organize this.

A delegate is defined by the signature of the expected method. For instance with:

public delegate void FooMethod (Bar x, Baz y);

You define a delegate for void methods given a Bar and a Baz. So if you have an instance of the following class:

public class Qux {

    public void Method (Bar x, Baz y) {
        //do something
        return null;
    }

}

Then you can refer to the method:

Qux q = new Qux();
FooMethod fm = q.Method;

An event is thus a list of delegates with the same signature:

You define an event as:

private event FooMethod SomeEvent;

You can add delegates (listeners) by using the += operator:

SomeEvent += q.Method;

remove the delegate by the -= operator and call the event with:

SomeEvent(new Bar(), new Baz());

As if you call a single method that does the dispatching.

By calling the event, all the registered delegates will be called, in the order of registration.

Note: By calling SomeEvent(new Bar(), new Baz()); this does not mean every delegate receives new instances: the instances are constructed first and shared over all delegate calls.

Conclusion: I think the C# designers did a good job introducing observer patterns since the programmer is no longer responsible to program it correctly his/herself. Furthermore the events are easy to understand and use convenient syntax. In special situations however programmers can be required to implement an observer him/herself. But these are very rare occasions.

这篇关于用事件实现观察者模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆