简体   繁体   English

用服务类绘制UML类图

[英]draw UML class diagram with Service classes

this is a small scenario of a big system. 这是大型系统的一个小场景。 i want to model this scenario in class diagram and then implement it in code 我想在类图中对这种情况进行建模,然后在代码中实现

this is a scenario in book store.there is customer and books customer can buy books,search books by name,and read books 这是书店中的一种情况。有客户,客户可以购买书籍,按名称搜索书籍以及阅读书籍

class diagram implemented in following ways 通过以下方式实现的类图

1.put all operations to the customer class because customer is the one who perform these actions but buyBooks(),searchBook(),readBooks() methods are not responsible for changing the state of customer class (these methods are not working on data inside of customer class ) there for it violate basic oop principle encapsulation. 1.将所有操作归类到客户类,因为客户是执行这些操作的人,但是buyBooks(),searchBook(),readBooks()方法不负责更改客户类的状态(这些方法不适用于内部数据)客户类)的存在,因为它违反了基本的oop原则封装。

customer can buy,search,read 1 or more books therefore association marked as shown in image. 客户可以购买,搜索,阅读1本书或更多本书,因此其关联关系如图所示。

i have found lots of class diagrams draw this way when referring the INTERNET 我发现很多类图在引用INTERNET时都以此方式绘制

在此处输入图片说明

2. put all operations to the customer class same as above. 2.将所有操作归入与上述相同的客户类别。 and mark each associations seperately, with three separate lines 并分别用三个单独的行标记每个关联

associations 协会

  • customer can buy one or more books 客户可以购买一本或多本书
  • customer can search one or more books 客户可以搜索一本或多本图书
  • customer can read one or more books 客户可以阅读一本或多本书籍

在此处输入图片说明

3.in here put all operations to the service class because those methods are not responsible for changing state of neither customer,book class 3.在这里把所有操作都放到服务类上,因为那些方法不负责改变客户,书本类的状态

and then mark association. 然后标记关联。

在此处输入图片说明 - among theses which one is correct class diagram ? -其中哪一个是正确的班级图?
- is it acceptable to draw service classes in uml class diagrams ? -在uml类图中绘制服务类是否可以接受?

Either the first or the last diagram can work in my opinion (once corrected), but I lean towards the first implementation. 我认为第一个或最后一个图都可以工作(一旦更正),但是我倾向于第一个实现。 The second one is just plain wrong. 第二个是完全错误的。

I would use the first because a "Customer" implies a person whose exclusive purpose in the system is to interact with your Book class, especially in light of the fact that your multiplicity values indicate that a customer must associate with at least one book class. 我之所以使用第一个,是因为“客户”是指其系统中的唯一目的是与您的Book类进行交互的人,尤其是鉴于您的多重性值表明客户必须与至少一个书类相关联这一事实。 If a customer did more things than interact with books, it might be useful to abstract the things that a customer does with respect to books into a service, but I don't see the need here. 如果客户要做的事情多于与书本互动,那么将客户就书本所做的事情抽象为服务可能会很有用,但我认为这里没有必要。

Now, your class diagram syntax is off. 现在,您的类图语法已关闭。 The arrows should be open (like > ) rather than solid. 箭头应该是打开的(如> ),而不是实心的。 Also, your multiplicity value on the Book class seems wrong: a book instance will presumably have zero customers associated with it if it hasn't been bought yet, and one customer associated with it if it has. 同样,您在Book类上的多重性值似乎是错误的:如果尚未购买,则该实例与该实例相关联的客户为零,而与该实例相关联的客户则为一个。 If so, your multiplicity value on the book end should be 0..1 . 如果是这样,则书末的多重性值应为0..1

Finally, your second diagram is wrong because associations are by class rather than by operation; 最后,您的第二张图是错误的,因为关联是按类而不是按操作进行的。 you don't put an association for each operation, but one per class. 您不必为每个操作都关联一个关联,而是为每个类关联一个关联。

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

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