重载与泛型或多态性

发布于 2024-10-10 22:34:38 字数 634 浏览 3 评论 0原文

假设我们正在尝试编写一个辅助扩展方法来为 asp.net 添加单元格。 Asp.net TableCell 控件只有一个默认构造函数,在处理高度自定义的数据时可能会很麻烦(忽略与问题无关的该解决方案的基本缺陷)。

多态解决

public static void AddCell(this Table table, object cell) {
    TableCell tableCell;
    if (cell is TableCell) {
        tableCell = (TableCell)cell;
    } else if (cell is WebControl) {
        tableCell = new TableCell();
        tableCell.Controls.Add((WebControl)cell);
    } else {
        tableCell = new TableCell();
        tableCell.Text = cell.ToString();
    }
    table.Rows[table.Rows.Count - 1].Cells.Add(tableCell);
}

方案 问题

作为通用方法或多个方法重载来实现会更好吗?

Let's say we're trying to write a helper extension method to add cells for asp.net. Asp.net TableCell controls only have a default constructor and can be a hassle to work with when dealing with highly custom data (disregard the fundamental flaws of this solution not related to the question).

Polymorphic solution

public static void AddCell(this Table table, object cell) {
    TableCell tableCell;
    if (cell is TableCell) {
        tableCell = (TableCell)cell;
    } else if (cell is WebControl) {
        tableCell = new TableCell();
        tableCell.Controls.Add((WebControl)cell);
    } else {
        tableCell = new TableCell();
        tableCell.Text = cell.ToString();
    }
    table.Rows[table.Rows.Count - 1].Cells.Add(tableCell);
}

Question

Would this be better implemented as a generic method, or several method overloads?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三生一梦 2024-10-17 22:34:38

您应该使用三个重载方法,第三个方法应该采用字符串而不是对象

原因:

  1. 这三个重载使客户端开发人员清楚该方法接受哪种类型的数据。

  2. 通过添加更多重载而不破坏客户端代码中方法的现有调用,可以更轻松地扩展功能。

  3. 当客户端开发人员无意中传递了他们无意传递的内容时,您当前的实现很容易导致错误。由于参数是object,编译器不可能检查类型的正确性。最好在客户端方面要求显式 .ToString() 来明确表明这就是正在执行的操作。

You should use three overloaded methods, and the third one should take a string instead of an object.

Reasons:

  1. The three overloads make it obvious to the client developer which kinds of data the method accepts.

  2. It is easier to extend the functionality by adding more overloads without breaking existing calls to the methods in client code.

  3. Your current implementation is prone to causing bugs when client developers accidentally pass something they didn’t mean to. Since the parameter is object, the compiler cannot possibly check the correctness of the type. It is better to require an explicit .ToString() on the part of the client to make it explicit that this is what being done.

迟到的我 2024-10-17 22:34:38

这看起来更像是您想要提供重载方法的情况。我这么说是因为您处理的不是一个常量接口或可以轻松应用于泛型的东西。也就是说,您对 cell 对象执行的操作完全取决于它是什么。对我来说,重载 AddCell 方法以根据参数类型执行不同的功能会更有意义。另外,如果您正在处理在编译时了解的有限数量的类型,那么在我看来,使用泛型会使事情变得不必要的复杂化。

This looks more like a case where you'd want to provide overloaded methods. I say that because you're not dealing with a constant interface or something that could be easily applied to generics. That is, what you do with the cell object depends entirely on what it is. To me, it would make more sense to overload the AddCell method to perform different functionality based on the type of parameter. Also, if you're dealing with a finite number of types that you know about at compile time, it just seems to me that working with generics would unnecessarily complicate things.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文