这种设计是个好主意吗 - 接口和抽象类

发布于 2024-07-11 00:19:19 字数 795 浏览 6 评论 0 原文

我希望能够做如下的事情:

//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 制作的:)

I would like to be able to do somthing like the following:

//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;

Would something like this be to much:

http://img81.imageshack.us/img81/427/diagramcm3.jpg

or would this be better:

http://img301.imageshack.us/img301/4136/presentation1nh9.jpg

Sorry if this hard to understand what I am trying to get at, basically I have two objects will share comman properties except for there row collections which will be generic. I would like to do this in cleanest way.

Sorry for my crappy diagrams, made in powerpoint :)

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

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

发布评论

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

评论(3

So要识趣 2024-07-18 00:19:19

我想说第二种设计更好。 更少的物品和更容易的继承路径。

第一个设计具有不必要的接口,除非您要实现实现该接口但不从基类继承的其他东西,否则您实际上并不需要这些接口。

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.

不交电费瞎发啥光 2024-07-18 00:19:19

TableTable 之间有什么区别? 换句话说,您不能只使用 Table 作为您的非泛型表单吗?

如果您确实选择第二个选项,我建议重命名您的一个Rows属性 - 您可以通过隐藏来避免拥有两个不同类型的属性等等,但这不会令人愉快。

您的 Table 类型实际上有多少行为? 如果它实际上只是一个容器,您可能不需要接口 - 但如果它背后有重要的逻辑,您可能需要一个接口,以便在测试使用它的类时可以模拟该表。

What's the difference betweeen a Table and a Table<string>? In other words, can you not just use Table<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.

浅浅 2024-07-18 00:19:19

我将在行中使用泛型,而不涉及基类中的字符串,并让非泛型继承 Table 类。 考虑不使用抽象类。

Table<T> -> Table:Table<string>

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.

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