简体   繁体   English

uml到Java代码的转换约定

[英]uml to java code conversion convention

汽车商店的uml类图

Please neglect any other arrow excluding the single simple arrow on which 1..1 or anything like that is written at the end 请忽略其他任何箭头,但最后写有1..1或类似名称的单个简单箭头除外

I have refered to many websites and many books giving introductions to UML 2.0 and i came across two conventions used 我已经参考过许多网站和许多书籍,其中介绍了UML 2.0,并且遇到了两个常用的约定

when two classes are connected by a single line without arrow and either 1..1, 1..*, etc is written at the end of line, then 当两类通过一条没有箭头的单行连接并且在行尾写有1..1,.. *等时,则

Assuming that at the end of line joining purchasePlan and User the term is 1..* not 0..* 假设在加入purchasePlan和User的行的末尾,术语为1 .. *而不是0 .. *

1) according to one convention we create PurchasePlan object's array in User class according to diagram and one object of User in PurchasePlan 1)根据一种约定,我们根据图表和PurchasePlan中User的一个对象在User类中创建PurchasePlan对象的数组

2) according to second convention we create only array of objects of purchaseplan in User, but not user's object in PurchasePlan 2)根据第二种约定,我们仅在User中创建purchaseplan对象的数组,而不在PurchasePlan中创建用户的对象

So which convention is the most widely used or the standard one? 那么,哪个公约是使用最广泛的公约还是标准公约?

What you are looking at there is a bi-directional association . 您正在查看的是双向关联 This means convention (1) is appropriate for this relationship. 这意味着约定(1)适用于这种关系。 If the association had an arrow then the second approach would apply. 如果协会有箭头,则第二种方法适用。

The "0..*" is significant in that a User can have no referenced purchase plans (ie its PurchasePlan array can be empty). “ 0 .. *”很重要,因为用户不能具有引用的购买计划 (即,其PurchasePlan数组可以为空)。 The corresponding User field in every PurchasePlan would still need to be populated (not null) in order to adhere to the bi-directional relationship; 为了遵守双向关系,仍然需要填充每个PurchasePlan中的相应User字段(不为null); this rejects the possibility of "orphaned" purchase plans. 这拒绝了“孤立”购买计划的可能性。

The first option would create a cyclic dependency. 第一个选项将创建循环依赖性。 This is discouraged because you get a higher coupling and the maintainabilty of your code degenerates. 不建议这样做,因为您会获得更高的耦合度,并且代码的可维护性会退化。 Your two options are creating a collection of PurchasePlan in your User or a single User in each PurchasePlan. 您的两个选择是在您的用户中创建一个PurchasePlan的集合,或者在每个PurchasePlan中创建一个用户。 Which one you use depends on your domain. 您使用哪一个取决于您的域。 Ask yourself if you have to get all PurchasePlan for a User or the other way around more often. 问问自己,是否必须为某个用户获取所有PurchasePlan或以其他方式获取更多。

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

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