Effective C# Item 22: Define Outgoing Interface With Events
2006-12-22 09:09
471 查看
Effective C# Item 22: Define Outgoing Interface With Events
事件为我们的类型定义了对外的接口。事件是通过委托来提供类型安全的函数签名。由于大部分情况下我们使用委托的例子都是事件,这使得我们容易将二者混为一谈。在Item 21中,我们举了不定义事件的委托的例子。当我们的类型在系统中必须与多个客户程序进行信息交互时,我们应考虑当使用事件。
我们来考虑一个简单的例子。我们创建一个日志类来调度应用程序中的所有消息。它会接受应用程序资源的所有消息并将其调度给感兴趣的监听者。这个监听者可能是控制台,数据库,系统日志或者其它的什么机制。我们可以像下面这样定义这个类,创建一个事件来处理消息。
public class LoggerEventArgs : EventArgs
public delegate void AddMessageEventHandler(object sender, LoggerEventArgs msg);
public class Logger
public class Logger
class ConsoleLogger
class EventLogger
public class Logger
{
private static EventHandlerList handlers = new EventHandlerList();
public static void AddLogger(string system, AddMessageEventHandler ev)
{
handlers[system] = ev;
}
public static void RemoveLogger(string system)
{
handlers[system] = null;
}
public static void AddMsg(string system, int priority, string msg)
{
if (!string.IsNullOrEmpty(system))
{
AddMessageEventHandler l = handlers[system] as AddMessageEventHandler;
LoggerEventArgs args = new LoggerEventArgs(priority, msg);
if (l != null)
{
l(null, args);
}
l = handlers[""] as AddMessageEventHandler;
if (l != null)
{
l(null, args);
}
}
}
}
这个新的例子将所有的事件句柄存储在EventHandlerList集合中。当一个新的事件对象被创建后,对于来自于同一个子系统的后续请求将返回同一个事件对象。如果我们开发一个包含大量事件的类,你应当考虑使用事件句柄集合来处理。在.Net Framework中,System.Windows.Forms.Control类使用一种更加复杂的实现方法来对应复杂的事件字段。每个事件字段内部包含一个对象集合来添加和移除特定的句柄。
当我们在接口中为类定义事件时,我们应当明白任何客户端程序都可以将句柄关联到上面来。那些客户端程序并不需要了解编译时的状态。使用C#中的事件可以消除发送者和可能接收者之间的耦合关系。发送者可以被开发成与接收者无关的模式。事件是消息传递的一种标准的模式。
译自 Effective C#:50 Specific Ways to Improve Your C# Bill Wagner著
回到目录
事件为我们的类型定义了对外的接口。事件是通过委托来提供类型安全的函数签名。由于大部分情况下我们使用委托的例子都是事件,这使得我们容易将二者混为一谈。在Item 21中,我们举了不定义事件的委托的例子。当我们的类型在系统中必须与多个客户程序进行信息交互时,我们应考虑当使用事件。
我们来考虑一个简单的例子。我们创建一个日志类来调度应用程序中的所有消息。它会接受应用程序资源的所有消息并将其调度给感兴趣的监听者。这个监听者可能是控制台,数据库,系统日志或者其它的什么机制。我们可以像下面这样定义这个类,创建一个事件来处理消息。
public class LoggerEventArgs : EventArgs
public delegate void AddMessageEventHandler(object sender, LoggerEventArgs msg);
public class Logger
public class Logger
class ConsoleLogger
class EventLogger
public class Logger
{
private static EventHandlerList handlers = new EventHandlerList();
public static void AddLogger(string system, AddMessageEventHandler ev)
{
handlers[system] = ev;
}
public static void RemoveLogger(string system)
{
handlers[system] = null;
}
public static void AddMsg(string system, int priority, string msg)
{
if (!string.IsNullOrEmpty(system))
{
AddMessageEventHandler l = handlers[system] as AddMessageEventHandler;
LoggerEventArgs args = new LoggerEventArgs(priority, msg);
if (l != null)
{
l(null, args);
}
l = handlers[""] as AddMessageEventHandler;
if (l != null)
{
l(null, args);
}
}
}
}
这个新的例子将所有的事件句柄存储在EventHandlerList集合中。当一个新的事件对象被创建后,对于来自于同一个子系统的后续请求将返回同一个事件对象。如果我们开发一个包含大量事件的类,你应当考虑使用事件句柄集合来处理。在.Net Framework中,System.Windows.Forms.Control类使用一种更加复杂的实现方法来对应复杂的事件字段。每个事件字段内部包含一个对象集合来添加和移除特定的句柄。
当我们在接口中为类定义事件时,我们应当明白任何客户端程序都可以将句柄关联到上面来。那些客户端程序并不需要了解编译时的状态。使用C#中的事件可以消除发送者和可能接收者之间的耦合关系。发送者可以被开发成与接收者无关的模式。事件是消息传递的一种标准的模式。
译自 Effective C#:50 Specific Ways to Improve Your C# Bill Wagner著
回到目录
相关文章推荐
- Effective C#之22:Define Outgoing Interfaces with Events
- Effective C# Item 21: Express Callbacks with Delegates
- Effective C# Item 26: Implement Ordering Relations with IComparable and IComparer
- Effective C# Item13: Initialize Static Class Members with Static Constructiors
- Item22 When using the Pimpl, define specific member functions in the implementation file
- [翻译] Effective C++, 3rd Edition, Item 22: 将 data members(数据成员)声明为 private
- Effective C# Item 16: Minimize Garbage
- Effective Java Item3:Enforce the singleton property with a private constructor or an enum type
- How to: Explicitly Implement Interface Members with Inheritance (C# Programming Guide)
- Effective c# Item3:操作符is或as优于强制转型
- Effective C# Item15:利用using和try/finally语句来清理资源
- Effective C# Item3:操作符as或is优于强制转换
- Effective C# Item 27: Avoid ICloneable
- Item 34: Differentiate between inheritance of interface and inheritance of implementation(Effective C++)
- Effective C# Item15:利用using和try/finally语句来清理资源
- Effective C# Item27:避免ICloneable接口
- How to: Explicitly Implement Interface Members with Inheritance (C# Programming Guide)
- Effective C# Item 9: Understand the Relationships Among ReferenceEquals(),static Equals(),instance Equals,and operator==
- More Effective C# Item2 : 恰到好处的定义约束
- Effective C# Item20:明辨接口实现和虚方法重写