API设计问题
在设计我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我会使用 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.
我认为选项 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.
我会选择选项 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.
我个人会选择
选项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.对我来说绝对是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.
选项 1:
工作表根本不应该了解工作簿,您应该查找“解耦”!。
Option 1:
The sheet should not know about the workbook at all, you should look up 'decoupling'!.