问题描述
这个问题是涉及到C#,但可以适用于其他语言。我对使用code,如以下预约:
This question is related to C#, but may be applicable to other languages as well. I have a reservation against using code such as the following:
using System.Windows.Forms;
class MyForm : Form
{
private Timer myTimer;
private Button myButton;
public MyForm()
{
// Initialize the components, etc.
myTimer.Tick += new EventHandler( myTimer_Tick );
myButton.Click += new EventHandler( myButton_Click );
myTimer.Start();
}
private void myTimer_Tick( object sender, EventArgs eventArgs )
{
myTimer.Stop();
// also, I see a lot of usage of
// Timer.Enabled = true/false instead of -^
myButton_Click( this, ea /* or event EventArgs.Empty, or null */ );
return;
}
private void myButton_Click( object sender, EventArgs eventArgs )
{
// do a lot of stuff, with lots of logic that doesn't even use the
// state of the eventArgs
return;
}
}
我是独自一人,在那上面的风格是我的忌讳?是否有其他人谁喜欢分离的事件在功能工作量处理的清晰度,甚至可以分离出复杂的程序到单独的功能?
Am I alone, in that the above style is a pet peeve of mine? Are there others who enjoy the clarity of separating event handling from the workload of functions, or even separating out complex routines into separate functions?
时甚至有一个公认的风格?我觉得任何EX pressiveness和灵活性,事件处理在C#中已经可以使用样式这样会丢失。我觉得,如果你有一个方法,它的意思是一个按钮被点击,则单击按钮时只应调用。
Is there even an accepted style? I feel like any expressiveness and flexibility that event handling in C# has can be lost with styles like this. I feel like if you have a method that means "a button has been clicked", then it should only be called when a button is clicked.
对于那些谁这样写,我会说:如果你坚持有一个事件处理程序的方法来处理你的计时器滴答,和你按一下按钮,然后把它以外的其他button_Click - 也许 handleUserEvent(对象发件人,EventArgs EventArgs的)
To those who write like this, I would say: if you insist on having an EventHandler method to handle your timer tick, and your button click, then call it something other than button_Click -- perhaps "handleUserEvent( object sender, EventArgs eventArgs )
".
真的,不过,问题是,有没有被广泛采用哪些支持或不鼓励使用如上述任何风格指南?
Really, though, the question is, are there any style guidelines that are widely used which either support or discourage usage such as the above?
推荐答案
我同意Rex M的答案,但我迈出了这一步。如果您使用的是MVC模式(或类似的东西),视图将委派按钮点击控制器。当然,控制器的方法可以从你的类在其他地方被称为 - 说,从你的计时器回调
I agree with Rex M's answer, but I'd take it one step further. If you are using the MVC pattern (or something similar), the view would delegate the button click to the controller. The controllers methods can of course be called from elsewhere in your class - say, from your timer callback.
所以,回到你原来的code:
So, back to your original code:
using System.Windows.Forms;
class MyForm : Form
{
private Timer myTimer;
private Button myButton;
private MyController myController;
public MyForm()
{
// ...
// Initialize the components, etc.
// ...
myTimer.Tick += new EventHandler( myTimer_Tick );
myButton.Click += new EventHandler( myButton_Click );
myTimer.Start();
}
private void myTimer_Tick( object sender, EventArgs eventArgs )
{
myTimer.Stop();
myController.SomeMethod()
}
private void myButton_Click( object sender, EventArgs eventArgs )
{
// All the stuff done here will likely be moved
// into MyController.SomeMethod()
myController.SomeMethod();
}
}
使用MVC的一个优点是从视图控制器的脱钩。控制器现在可以跨多个视图类型很容易使用和退出图形用户界面更易于维护,因为它们含有很少的应用程序逻辑。
One advantage of using MVC is the decoupling of the controller from the view. The controller can now be used across multiple view types easily and exiting GUIs are easier to maintain as they contain very little application logic.
编辑:添加响应意见从OP
Added in response to comments from the OP
约耦合和内聚软件工程通话的基本设计原则。重要的是我们努力的同时最大限度地凝聚,因为这将导致一个更加模块化和可维护的系统组件之间的最小耦合。模式,如MVC和校长如打开/关闭主要建立在这些基础,为开发者提供遵循的实行更切实的模式。
The fundamental design principals of software engineering talk about coupling and cohesion. Importantly we strive to minimise coupling between components while maximising cohesion as this leads to a more modular and maintainable system. Patterns like MVC and principals like the Open/Closed Principal build on these fundamentals, providing more tangible patterns of implemenation for the developer to follow.
那么,谁写code看到在原来的职位还没有理解软件设计的基本原则,需要大大发展他们的技能。该任择议定书应该赞扬识别这种code闻,并试图了解为什么它是不完全正确。
So, anyone who writes code as seen in the original post has not understood the fundamentals of software design and needs to develop their skills considerably. The OP should be commended for identifying this "code smell" and trying to understand why it's not quite right.
一些相关参考资料:
- http://en.wikipedia.org/wiki/Coupling%5F% 28computer%5Fscience )
- http://en.wikipedia.org/wiki/Cohesion%5F% 28computer%5Fscience )
- http://en.wikipedia.org/wiki/Loose%5Fcoupling
- http://en.wikipedia.org/wiki/Model -view控制器
- http://en.wikipedia.org/wiki/Design%5Fpatterns
- http://en.wikipedia.org/wiki/Open/closed%5Fprinciple
- http://en.wikipedia.org/wiki/Design%5FPatterns% 5F%28book )
- http://en.wikipedia.org/wiki/Coupling%5F%28computer%5Fscience)
- http://en.wikipedia.org/wiki/Cohesion%5F%28computer%5Fscience)
- http://en.wikipedia.org/wiki/Loose%5Fcoupling
- http://en.wikipedia.org/wiki/Model–view–controller
- http://en.wikipedia.org/wiki/Design%5Fpatterns
- http://en.wikipedia.org/wiki/Open/closed%5Fprinciple
- http://en.wikipedia.org/wiki/Design%5FPatterns%5F%28book)
这篇关于C#:是调用的事件处理程序明确真的"好事做"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!