这种设计是个好主意吗 - 接口和抽象类
我希望能够做如下的事情:
//non-generic
var MyTable = new Table();
string name = MyTable.Name;
IEnumerable<String> rows = MyTable.Rows;
//generic
var MyTableGeneric = new Table<MyType>();
string name = MyTableGeneric.Name;
IEnumerable<MyType> rows = MyTableGeneric .Rows;
这样的事情会太多吗:
http://img81.imageshack.us/img81/427/diagramcm3.jpg
或者这样会更好:
http://img301.imageshack.us/img301/4136/presentation1nh9.jpg
抱歉,如果这很难理解我想要表达的意思,基本上我有两个对象将分享通用属性,但行集合除外,它们是通用的。 我想以最干净的方式做到这一点。
抱歉我的图表很糟糕,是用 powerpoint 制作的:)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想说第二种设计更好。 更少的物品和更容易的继承路径。
第一个设计具有不必要的接口,除非您要实现实现该接口但不从基类继承的其他东西,否则您实际上并不需要这些接口。
I'd say the second design is better. Less items and easier inheritance path.
The first design has unnecessary interfaces, which you don't really need unless you're implementing something else which implements the interface, but doesn't inherit from the base class.
Table
和Table
之间有什么区别? 换句话说,您不能只使用Table
作为您的非泛型表单吗?如果您确实选择第二个选项,我建议重命名您的一个
Rows
属性 - 您可以通过隐藏来避免拥有两个不同类型的属性等等,但这不会令人愉快。您的
Table
类型实际上有多少行为? 如果它实际上只是一个容器,您可能不需要接口 - 但如果它背后有重要的逻辑,您可能需要一个接口,以便在测试使用它的类时可以模拟该表。What's the difference betweeen a
Table
and aTable<string>
? In other words, can you not just useTable<string>
as your nongeneric form?If you do go for the second option, I'd suggest renaming one of your
Rows
properties - you may be able to get away with having two properties of different types through hiding etc, but it's not going to be pleasant.How much behaviour will your
Table
type actually have? If it's really just a container, you may not need an interface - but if it's got significant logic behind it, you may want to have an interface so that you can mock out the table when testing a class which uses it.我将在行中使用泛型,而不涉及基类中的字符串,并让非泛型继承 Table 类。 考虑不使用抽象类。
I would use generics in the rows, without involving string in the base class, and have the non generic inherit the Table class. Consider not using the abstract class.