The observer pattern is already implemented for you in the form of events.
The advantage of events is that they can have multiple subscribers, while with a delegate, you can only have one. Events are better for public interfaces, and scenarios where you don't have complete control over who wants to get notified that something happens. In reality, events are just automatically managed lists of delegates. You'll have to see what makes more sense in your scenario.
Edit:As commenter Rabbi mentions, the above isn't entirely true, as any delegate can become a multicast delegate. The purpose of the event modifier is to make a delegate that can only be invoked inside the class that defines it. This is most useful for ensuring encapsulation in public interfaces.
传统观察者模式的另一个好处是,它可以更好地处理因某种原因需要询问订阅者的情况。我遇到过这种需要,即通过 Web 服务边界的对象,其中委托存在问题,而观察者模式只是对另一个对象的引用,因此只要序列化保持对象内引用的完整性,它就可以正常工作像 NetDataContractSerializer 那样的图表。在这些情况下,可以根据引用的订阅者是否也在同一对象图中来区分在建立服务边界之前应删除的订阅者。
One advantage of the observer pattern is that if you have a large number of events that are generally always subscribed to by an interested party, then passing a single object into a method to subscribe to the events is much easier than subscribing to each event individually. With C#'s lack of specifying interfaces and methods for anonymous classes as can be done with Java, implementing the observer pattern becomes a bit more laborious so most opt for using events anyway.
Another benefit of the traditional observer pattern is that it handles better in cases where you need to interrogate the subscriber for some reason. I've come across this need with objects that pass a web-service boundary where there are problems with delegates whereas the observer pattern is just simply a reference to another object so it works fine as long as your serialization keeps integrity of references within the object graph like the NetDataContractSerializer does. In these cases it's possible to discriminate between subscribers that should be removed before making the service boundary based on whether the referenced subscriber is also within the same object graph.
发布评论
评论(3)
观察者模式已经以 事件的形式为您实现。
事件的优点是它们可以有多个订阅者,而使用委托时,您只能有一个订阅者。事件更适合公共接口,以及您无法完全控制谁想要订阅者的场景得到发生某事的通知。实际上,事件只是自动管理的代表列表。您必须看看什么在您的场景中更有意义。编辑: 正如评论者拉比提到的,上述内容并不完全正确,因为任何委托都可以成为多播委托。事件修饰符的目的是创建一个只能在定义它的类内部调用的委托。这对于确保公共接口中的封装最有用。
The observer pattern is already implemented for you in the form of events.
The advantage of events is that they can have multiple subscribers, while with a delegate, you can only have one.Events are better for public interfaces, and scenarios where you don't have complete control over who wants to get notified that something happens. In reality, events are just automatically managed lists of delegates. You'll have to see what makes more sense in your scenario.Edit: As commenter Rabbi mentions, the above isn't entirely true, as any delegate can become a multicast delegate. The purpose of the event modifier is to make a delegate that can only be invoked inside the class that defines it. This is most useful for ensuring encapsulation in public interfaces.
观察者模式的优点之一是,如果您有大量通常总是由相关方订阅的事件,那么将单个对象传递到方法中来订阅事件比单独订阅每个事件要容易得多。由于 C# 缺乏为匿名类指定接口和方法 可以使用Java,实现观察者模式变得有点费力,所以大多数人还是选择使用事件。
传统观察者模式的另一个好处是,它可以更好地处理因某种原因需要询问订阅者的情况。我遇到过这种需要,即通过 Web 服务边界的对象,其中委托存在问题,而观察者模式只是对另一个对象的引用,因此只要序列化保持对象内引用的完整性,它就可以正常工作像 NetDataContractSerializer 那样的图表。在这些情况下,可以根据引用的订阅者是否也在同一对象图中来区分在建立服务边界之前应删除的订阅者。
One advantage of the observer pattern is that if you have a large number of events that are generally always subscribed to by an interested party, then passing a single object into a method to subscribe to the events is much easier than subscribing to each event individually. With C#'s lack of specifying interfaces and methods for anonymous classes as can be done with Java, implementing the observer pattern becomes a bit more laborious so most opt for using events anyway.
Another benefit of the traditional observer pattern is that it handles better in cases where you need to interrogate the subscriber for some reason. I've come across this need with objects that pass a web-service boundary where there are problems with delegates whereas the observer pattern is just simply a reference to another object so it works fine as long as your serialization keeps integrity of references within the object graph like the NetDataContractSerializer does. In these cases it's possible to discriminate between subscribers that should be removed before making the service boundary based on whether the referenced subscriber is also within the same object graph.
委托可用于实现观察者模式 - 想想事件。
要在没有事件的情况下执行此操作,请查看此处: http://www.dofactory.com/Patterns/PatternObserver .aspx
如果您愿意,将其重构为使用委托并不需要太多。
我能想到的实现接口的唯一优点是所有实现中成员名称的一致性。
Delegates can be used to implement the observer pattern - think of events.
To do it without events take a look here: http://www.dofactory.com/Patterns/PatternObserver.aspx
It wouldn't take much to refactor that into using delegates if you preferred.
The only advantage I can think of with implementing an interface is member name consistency across all implementations.