重载与泛型或多态性
假设我们正在尝试编写一个辅助扩展方法来为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用三个重载方法,第三个方法应该采用
字符串
而不是对象
。原因:
这三个重载使客户端开发人员清楚该方法接受哪种类型的数据。
通过添加更多重载而不破坏客户端代码中方法的现有调用,可以更轻松地扩展功能。
当客户端开发人员无意中传递了他们无意传递的内容时,您当前的实现很容易导致错误。由于参数是
object
,编译器不可能检查类型的正确性。最好在客户端方面要求显式.ToString()
来明确表明这就是正在执行的操作。You should use three overloaded methods, and the third one should take a
string
instead of anobject
.Reasons:
The three overloads make it obvious to the client developer which kinds of data the method accepts.
It is easier to extend the functionality by adding more overloads without breaking existing calls to the methods in client code.
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.这看起来更像是您想要提供重载方法的情况。我这么说是因为您处理的不是一个常量接口或可以轻松应用于泛型的东西。也就是说,您对
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 theAddCell
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.