假设我的 Facade 类有两个子系统类。每个子系统都有不同的事件。
FacadeClass 是
public class FacadeClass
{
private SubsystemClass1 _s1;
private SubsystemClass2 _s2;
public FacadeClass()
{
_s1 = new SubsystemClass1();
_s2 = new SubsystemClass2();
}
}
和子系统类 1
public class SubsystemClass1
{
public event EventHandler FetchComplete;
public void OnFetchComplete(EventArgs e)
{
if (FetchComplete != null)
{
FetchComplete(this, e);
}
}
}
然后是 SubsystemClass2
public class SubsystemClass2
{
public event EventHandler SendComplete;
public void OnSendComplete(EventArgs e)
{
if (SendComplete != null)
{
SendComplete(this, e);
}
}
}
假设我有另一个使用外观类的类,我想附加来自 SubsystemClass1 和 SubsystemClass2 的事件。问题是如何附加事件而不在外观类中重新定义它,也不使用子系统类(如果有的话)?
在外观类中重新定义它的示例
public class FacadeClass
{
private SubsystemClass1 _s1;
private SubsystemClass2 _s2;
public FacadeClass()
{
_s1 = new SubsystemClass1();
_s2 = new SubsystemClass2();
}
// Redifine the event
public event EventHandler FetchComplete
{
add { _s1.FetchComplete += value; }
remove { _s1.FetchComplete -= value; }
}
public event EventHandler SendComplete
{
add { _s2.SendComplete += value; }
remove { _s2.SendComplete -= value; }
}
}
使用子系统类的示例,将它们都设为公共
public class FacadeClass
{
// Make both class to public
public SubsystemClass1 _s1;
public SubsystemClass2 _s2;
public FacadeClass()
{
_s1 = new SubsystemClass1();
_s2 = new SubsystemClass2();
}
}
public class AnotherClass
{
FacadeClass _fd;
public AnotherClass()
{
_fd = new FacadeClass();
// Little bit ugly
_fd._s1.FetchComplete += new EventHandler(_s1_FetchComplete);
}
void _s1_FetchComplete(object sender, EventArgs e)
{
// Do Something Here
}
}
提前致谢,
问候布赖恩...