API设计问题

发布于 2024-12-02 23:54:25 字数 423 浏览 1 评论 0原文

在设计我的 api 时,我正在考虑如何对以下行为进行建模。

选项 1 似乎更合乎逻辑,但随之而来的是强制执行不变量,例如检查电子表格是否确实属于工作簿。

选项 2 似乎很奇怪,电子表格知道如何删除自己,但实际上电子表格有对其父工作簿的引用,并且可以将调用直接委托给他。

或者这真的不是一个有效的案例,因为无论如何工作簿都需要验证它的电子表格?想法?

Workbook wb = new Workbook("Finances");
Spreadsheet ss = wb.CreateSpreadsheet("Bob's");

// option 1:
wb.RemoveSheet(ss);

// option 2:
ss.RemoveFromWorkbook();

谢谢大家

while designing my api, i am thinking about how i would like to model the following behavior.

option 1 seems more logical, but with that comes enforcing invariants such as checking if the spreadsheet actually belongs to the workbook.

option 2 seems strange a spreadsheet knows how to remove himself, but in fact the spreadsheet has a reference to its parent workbook and can delegate the call directly to him.

or is this really not a valid case since the workbook would need to validate it spreadsheet no matter what?? thoughts?

Workbook wb = new Workbook("Finances");
Spreadsheet ss = wb.CreateSpreadsheet("Bob's");

// option 1:
wb.RemoveSheet(ss);

// option 2:
ss.RemoveFromWorkbook();

Thank You everyone

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

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

发布评论

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

评论(6

梦在深巷 2024-12-09 23:54:25

我会使用 wb.Sheets.Remove(ss)。这允许职责分离,因为 Sheets 对象是电子表格的集合。这也允许稍后出现一个工作表可能位于多个工作簿中的概念。

I would use wb.Sheets.Remove(ss). This allows separation of responsibilities as the Sheets object is a collection of Spreadsheets. This also allows the concept later on that a sheet might be in multiple workbooks.

不必了 2024-12-09 23:54:25

我认为选项 1 更好用,因为每次需要从某个容器中删除某个子实体时,您首先会引用该容器,然后才在该容器中搜索某些成员实体本身。

I think option 1 is better to use because every time when you need to remove some child entity from the some container first of all you'll reference to the container and only then you will search some members in the entity itself.

花开柳相依 2024-12-09 23:54:25

我会选择选项 1。将工作簿视为一个集合或可枚举。与任何集合一样,工作簿的部分工作是管理其中的项目。在工作簿中添加和删除工作表是工作簿的责任,而不是工作表的责任。

I would go with Option 1. Think of the Workbook as a collection or enumerable. As with any collection, part of the Workbook's job is to manage the items within it. Adding and removing sheets from a workbook is the workbook's responsibility, not the sheet's.

与他有关 2024-12-09 23:54:25

我个人会选择选项1,因为工作簿是包含工作表的东西,所以它是某种容器。因此,要从中删除工作表,恕我直言,使用工作簿对象是合乎逻辑且更自然的。

I would personally stay for option 1, as workbook is something that contains a worksheets, so it's some kind of container. So to remove a worksheet from it, it's logical and more natural IMHO to use workbook object.

岁月苍老的讽刺 2024-12-09 23:54:25

对我来说绝对是1。

你有 wb.CreateSpreadsheet,所以如果我已经使用过它,我肯定会在 wb 对象中寻找删除函数。

Definitely 1 for me.

You have wb.CreateSpreadsheet, so if I'd already used that, I'd defnitely be looking for the remove function in the wb object.

沦落红尘 2024-12-09 23:54:25

选项 1:

工作表根本不应该了解工作簿,您应该查找“解耦”!。

Option 1:

The sheet should not know about the workbook at all, you should look up 'decoupling'!.

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