开发者

API Design Question

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("开发者_Python百科Finances");
Spreadsheet ss = wb.CreateSpreadsheet("Bob's");

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

// option 2:
ss.RemoveFromWorkbook();

Thank You everyone


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.


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.


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.


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.


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.


Option 1:

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜