将Event合并成一个Handler,效率高吗?
Combining Events into one Handler, efficient?
这个问题分为两部分。
- 每个对象都有一个特定于事件的函数还是组合对象事件更好,如果是这样,
- 使用散列码来定义发件人对象并进行比较以找出正在更新的内容是否有效?
示例代码如下。
我的事件函数:
private void GeneralEventHandler(object sender, EventArgs e){
var senderHash = sender.GetHashCode();
if (senderHash == tbDatabase.GetHashCode())
Settings.DB.Default.Database = tbDatabase.Text;
else if (senderHash == tbSchema.GetHashCode())
Settings.DB.Default.Schema = tbSchema.Text;
}
我的对象定义:
// tbDatabase
/
this.tbDatabase.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
this.tbDatabase.Location = new System.Drawing.Point(62, 3);
this.tbDatabase.Name = "tbDatabase";
this.tbDatabase.Size = new System.Drawing.Size(210, 20);
this.tbDatabase.TabIndex = 0;
this.tbDatabase.LostFocus += new System.EventHandler(this.GeneralEventHandler);
//
// tbSchema
//
this.tbSchema.Location = new System.Drawing.Point(55, 3);
this.tbSchema.Name = "tbSchema";
this.tbSchema.Size = new System.Drawing.Size(217, 20);
this.tbSchema.TabIndex = 1;
this.tbSchema.LostFocus += new System.EventHandler(this.GeneralEventHandler);
我主要只是用它来即时更新用户设置。我在退出时保存设置文件。
我更喜欢让每个对象都有自己的事件处理程序。每个处理程序依次调用 "does the deed" 的方法,但不知道是谁调用的。如果多个对象 "do the same thing" 让它们调用相同的 "do-it" 方法。
你永远不知道未来会发生什么 - 具有组合事件处理程序的对象现在可能会在未来需要做的事情上有所不同。
这个问题分为两部分。
- 每个对象都有一个特定于事件的函数还是组合对象事件更好,如果是这样,
- 使用散列码来定义发件人对象并进行比较以找出正在更新的内容是否有效?
示例代码如下。
我的事件函数:
private void GeneralEventHandler(object sender, EventArgs e){
var senderHash = sender.GetHashCode();
if (senderHash == tbDatabase.GetHashCode())
Settings.DB.Default.Database = tbDatabase.Text;
else if (senderHash == tbSchema.GetHashCode())
Settings.DB.Default.Schema = tbSchema.Text;
}
我的对象定义:
// tbDatabase
/
this.tbDatabase.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
this.tbDatabase.Location = new System.Drawing.Point(62, 3);
this.tbDatabase.Name = "tbDatabase";
this.tbDatabase.Size = new System.Drawing.Size(210, 20);
this.tbDatabase.TabIndex = 0;
this.tbDatabase.LostFocus += new System.EventHandler(this.GeneralEventHandler);
//
// tbSchema
//
this.tbSchema.Location = new System.Drawing.Point(55, 3);
this.tbSchema.Name = "tbSchema";
this.tbSchema.Size = new System.Drawing.Size(217, 20);
this.tbSchema.TabIndex = 1;
this.tbSchema.LostFocus += new System.EventHandler(this.GeneralEventHandler);
我主要只是用它来即时更新用户设置。我在退出时保存设置文件。
我更喜欢让每个对象都有自己的事件处理程序。每个处理程序依次调用 "does the deed" 的方法,但不知道是谁调用的。如果多个对象 "do the same thing" 让它们调用相同的 "do-it" 方法。
你永远不知道未来会发生什么 - 具有组合事件处理程序的对象现在可能会在未来需要做的事情上有所不同。