C# 基于类型的观察者?
假设我想根据类型创建一组观察者。也就是说,当他们收到事件通知时,他们会被告知其中一个参数的类型,然后根据它是否可以对该类型进行操作来决定是否采取行动 有什么简单的方法可以做到这一点吗?我认为这对于泛型来说是相当简单的,但这似乎比我想象的要难。如果可以避免的话,我更愿意不必处理对对象的大量引用 我被困的地方是这样做:C# 基于类型的观察者?,c#,design-patterns,generics,observer-pattern,C#,Design Patterns,Generics,Observer Pattern,假设我想根据类型创建一组观察者。也就是说,当他们收到事件通知时,他们会被告知其中一个参数的类型,然后根据它是否可以对该类型进行操作来决定是否采取行动 有什么简单的方法可以做到这一点吗?我认为这对于泛型来说是相当简单的,但这似乎比我想象的要难。如果可以避免的话,我更愿意不必处理对对象的大量引用 我被困的地方是这样做: public delegate void NotifyDelegate<T>(IEnumerator<T> loadable, NotifyArgs na);
public delegate void NotifyDelegate<T>(IEnumerator<T> loadable, NotifyArgs na);
interface IObserver
{
void Notify<T>(IEnumerator<T> loadable, NotifyArgs na);
}
class Subject
{
NotifyDelegate notifier; //won't compile: needs type args
void Register(IObserver o)
{
notifier += o.Notify;
}
}
公共委托void NotifyDelegate(IEnumerator可加载,NotifyArgs na);
接口IObserver
{
无效通知(IEnumerator可加载,NotifyArgs na);
}
班级科目
{
NotifyDelegate通知程序;//将不编译:需要类型args
无效寄存器(IObserver o)
{
通知者+=o.通知;
}
}
当然,我也可以将主题设置为泛型,但是我必须为每种类型设置一个单独的主题。这里有人有什么建议吗?是否有一些功能是我在某个地方遗漏的,或者是我将其过度复杂化了
更新:我确实过度简化了Notify和NotifyDelegate的参数。与此相反:
public delegate void NotifyDelegate<T>(NotifyArgs na);
public delegate void NotifyDelegate(NotifyArgs na);
我其实想做这样的事情:
public delegate void NotifyDelegate<T>(IEnumerator<T> loadable, NotifyArgs na);
class Observer : IObserver
{
public Observer (Subject s)
{
s.Register (this);
}
void Notify (float value)
{
System.Diagnostics.Trace.WriteLine ("float value = " + value);
}
void Notify (int value)
{
System.Diagnostics.Trace.WriteLine ("int value = " + value);
}
}
static void Main (string [] args)
{
Subject
s = new Subject ();
Observer
o = new Observer (s);
float
v1 = 3.14f;
int
v2 = 42;
System.Diagnostics.Trace.WriteLine ("sending float");
s.NotifyObservers (v1);
System.Diagnostics.Trace.WriteLine ("sending int");
s.NotifyObservers (v2);
}
公共委托void NotifyDelegate(IEnumerator可加载,NotifyArgs na);
我基本上是想来回传递数据库中的数据。很抱歉,如果前面的代码示例使任何人感到困惑。首先,将您的代码更改为以下内容:
interface IObserver
{
}
class Subject
{
public Subject ()
{
m_observers = new List<IObserver> ();
}
public void Register (IObserver o)
{
m_observers.Add (o);
}
List<IObserver>
m_observers;
}
然后像这样使用它:
public delegate void NotifyDelegate<T>(IEnumerator<T> loadable, NotifyArgs na);
class Observer : IObserver
{
public Observer (Subject s)
{
s.Register (this);
}
void Notify (float value)
{
System.Diagnostics.Trace.WriteLine ("float value = " + value);
}
void Notify (int value)
{
System.Diagnostics.Trace.WriteLine ("int value = " + value);
}
}
static void Main (string [] args)
{
Subject
s = new Subject ();
Observer
o = new Observer (s);
float
v1 = 3.14f;
int
v2 = 42;
System.Diagnostics.Trace.WriteLine ("sending float");
s.NotifyObservers (v1);
System.Diagnostics.Trace.WriteLine ("sending int");
s.NotifyObservers (v2);
}
接口IObserver
{
无效通知(NotifyArgs na);
bool支架类型(t型);
}
班级科目
{
观察员名单;
无效寄存器(IObserver o)
{观察员。加(o);
}
void OnNotify(类型t,NotifyArgs args)
{
foreach(观察者中的IObserver o)
{
如果(o.SupportsType(t))o.Notify(args));
}
}
}
有趣的想法。这比我想要实现的要复杂一点,但如果我不能想出一个更简单的方法,至少它看起来可以工作。