C#:调用事件处理程序显然真的“一件好事”。 [英] C#: is calling an event handler explicitly really "a good thing to do"?

查看:160
本文介绍了C#:调用事件处理程序显然真的“一件好事”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题与C#相关,但也可能适用于其他语言。我有使用如下代码的预订:

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?

有没有被接受的风格?我觉得像任何表现力和灵活性,事件处理在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.

写的人这个,我会说:如果你坚持有一个EventHandler方法来处理你的计时器滴答,你的按钮点击,然后调用它除了button_Click之外 - 也许 handleUserEvent(object sender,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.

因此,回到原始代码:

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的一个优点是控制器从视图。现在,控制器可以轻松地在多个视图类型中使用,并且退出的GUI更容易维护,因为它们只包含很少的应用程序逻辑。

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

软件工程的基本设计原则讨论耦合和内聚。重要的是,我们努力减少组件之间的耦合,同时最大化内聚,因为这将导致更加模块化和可维护的系统。像MVC和像Open / Closed Principal这样的模板基于这些基本原理,为开发人员提供了更具体的实现模式。

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.

因此,任何编写原始帖子中所示代码的人都不了解软件设计的基础知识,需要大量开发他们的技能。

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_(computer_science)
  • http://en.wikipedia.org/wiki/Cohesion_(computer_science)
  • http://en.wikipedia.org/wiki/Loose_coupling
  • http://en.wikipedia.org/wiki/Model–view–controller
  • http://en.wikipedia.org/wiki/Design_patterns
  • http://en.wikipedia.org/wiki/Open/closed_principle
  • http://en.wikipedia.org/wiki/Design_Patterns_(book)

这篇关于C#:调用事件处理程序显然真的“一件好事”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆