具有特定 "observable/subscriber" 关系的观察者模式
Observer pattern that has specific "observable/subscriber" relationship
我一直在尝试创建一个基本的观察者模式,该模式使用泛型类型在可观察对象与其订阅者之间创建紧密耦合的关系。以 YouTube 为例;频道将是可观察的对象,订阅者将是 YouTube 用户。 YouTube 频道只能由 YouTube 用户订阅,YouTube 用户只能订阅频道。因此,举例来说,您不能使用 YouTube 帐户订阅报纸或月刊果冻俱乐部。
我的第一个想法就是做这样的事情:
abstract class Observable<T> where T : Subscriber
abstract class Subscriber<T> where T : Observable
但这不起作用,因为 T 在这两种情况下都需要扩展包含特定泛型类型的 class,从而使泛型类型无用。
我的第二个想法是做这样的事情:
abstract class Observable<T> : IObservable where T : ISubscriber
abstract class Subscriber<T> : ISubscriber where T : IObservable
这可行,但并不完全安全。例如,IObservable
有一个方法 Subscribe(ISubscriber subscriber)
而 Observable 有方法 Subscribe(T t)
接口方法 Subscribe
的实现只是将给定的 ISubscriber
转换为类型 T 和将其传递给同名的另一个方法。
由于接口方法必须是 public,从技术上讲,您可以尝试订阅一个非 T 类型的对象,只要它是一个 ISubscriber。有没有一种方法可以在没有松散线程或有问题的代码的情况下完成这种观察者模式?
添加观察者时检查实例类型
// in you observer class
public void subscribe(ISubscriber s) {
if( s instanceof YouTubeSubscriber) {
System.out.println("Subscriber added");
} else {
// ignore the subscriber
System.out.println("Subscriber rejected");
}
}
好主意!这种模式非常有用,IObservable 是一个内置的 .Net 接口。 MSDN documentation provides more details. Your subscriber idea is represented by IObserver, also documented on MSDN.
The IObserver and IObservable interfaces provide a generalized mechanism for push-based notification, also known as the observer design pattern. The IObservable interface represents the class that sends notifications (the provider); the IObserver interface represents the class that receives them (the observer). T represents the class that provides the notification information [the data transfer object]...
我一直在尝试创建一个基本的观察者模式,该模式使用泛型类型在可观察对象与其订阅者之间创建紧密耦合的关系。以 YouTube 为例;频道将是可观察的对象,订阅者将是 YouTube 用户。 YouTube 频道只能由 YouTube 用户订阅,YouTube 用户只能订阅频道。因此,举例来说,您不能使用 YouTube 帐户订阅报纸或月刊果冻俱乐部。
我的第一个想法就是做这样的事情:
abstract class Observable<T> where T : Subscriber
abstract class Subscriber<T> where T : Observable
但这不起作用,因为 T 在这两种情况下都需要扩展包含特定泛型类型的 class,从而使泛型类型无用。
我的第二个想法是做这样的事情:
abstract class Observable<T> : IObservable where T : ISubscriber
abstract class Subscriber<T> : ISubscriber where T : IObservable
这可行,但并不完全安全。例如,IObservable
有一个方法 Subscribe(ISubscriber subscriber)
而 Observable 有方法 Subscribe(T t)
接口方法 Subscribe
的实现只是将给定的 ISubscriber
转换为类型 T 和将其传递给同名的另一个方法。
由于接口方法必须是 public,从技术上讲,您可以尝试订阅一个非 T 类型的对象,只要它是一个 ISubscriber。有没有一种方法可以在没有松散线程或有问题的代码的情况下完成这种观察者模式?
添加观察者时检查实例类型
// in you observer class
public void subscribe(ISubscriber s) {
if( s instanceof YouTubeSubscriber) {
System.out.println("Subscriber added");
} else {
// ignore the subscriber
System.out.println("Subscriber rejected");
}
}
好主意!这种模式非常有用,IObservable 是一个内置的 .Net 接口。 MSDN documentation provides more details. Your subscriber idea is represented by IObserver, also documented on MSDN.
The IObserver and IObservable interfaces provide a generalized mechanism for push-based notification, also known as the observer design pattern. The IObservable interface represents the class that sends notifications (the provider); the IObserver interface represents the class that receives them (the observer). T represents the class that provides the notification information [the data transfer object]...