简体   繁体   English

一个简单应用程序的类图

[英]Class diagram for a simple application

Suppose we have 3 entities: Library, Section & Book. 假设我们有3个实体:Library,Section和Book。

A Library consists of several Sections . Library由几个Sections组成。 A Section has several Books . 一个Section有几Books

A Book may belong to only 1 Section . Book可能只属于一个Section And finally a Section may belong to only 1 Library . 最后,一Section可能仅属于1个Library

The way I see it is that class Library aggregates a collection of Sections and class Section aggregates a collection of Books . 我看到的方式是类Library 聚合Sections的集合,而类Section 聚合Books的集合。

Now I'm required to upload all Books to a server. 现在,我需要将所有电子书上传到服务器。 I build a class BookUploader that takes a Book object in its constructor. 我构建了一个BookUploader类, BookUploader在其构造函数中使用Book对象。 On the server I created a folder for each Library and inside each library I'll create a Section folder and put the Book in it. 在服务器上,我为每个库创建了一个文件夹,在每个库中,我将创建一个Section文件夹并将Book放入其中。

The problem is since I passed a Book object to the BookUploader I have no idea what it's Section is. 问题是,因为我将Book对象传递给BookUploader,所以我不知道它的Section是什么。 In addition I don't know which Section belongs to which Library. 另外,我不知道哪个部分属于哪个库。

So I thought I'll just pass the Library object to the BookUploader then loop all Section then loop all Books in each Section, but someone told me that now BookUploader depends on 3 classes to upload a Book which is a bad design. 所以我想我只是将Library对象传递给BookUploader,然后循环所有Section,然后循环每个Section中的所有Books,但是有人告诉我,现在BookUploader依靠3个类来上载Book,这是一个糟糕的设计。

He suggested the each Book object should hold it's Section and each Section should hold it's Library which is the total inverse of my original design. 他建议每个Book对象都应该包含它的Section,每个Section应该包含它的Library,这是我原始设计的总逆。

Can anybody share his thoughts on which design is better and why? 谁能分享他对哪种设计更好的想法,为什么?

Thanks in advance. 提前致谢。

There's nothing wrong with defining aggregation as one end of a bi-directional association. 将聚合定义为双向关联的一端没有任何问题。 (Look here or here for examples.) (请查看此处此处的示例。)

If you're looking for implementation details, about that, look eg at ecore's EOpposite feature in EReferences. 如果要查找实现细节,请查看EReferences中的ecoreEOpposite功能。

After reading @Chad's comment I realized that the design itself needed some modifications to the entities in question. 阅读@Chad的评论后,我意识到设计本身需要对相关实体进行一些修改。 what was missing is a function in each child class that would return a reference to its parent. 缺少的是每个子类中的一个函数,该函数将返回对其父级的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM