英文:
Give specific value to some attributes in uml
问题
在经典的聚合示例中,涉及到汽车和车轮。一辆汽车有4个车轮,一个车轮属于1辆汽车:
1 4
汽车 <>------ 车轮
现在,车轮可以是标准轮胎或雪地轮胎。
在保持一个车轮类别的约束条件下(例如,不派生'snowWheel'等),如何建模汽车必须有2个雪地轮胎和2个标准轮胎?
英文:
On the classic aggregation example of car and wheel. A car has 4 wheels, wheel belongs to 1 car :
1 4
Car <>------ Wheel
Now, wheel can be standard tyre, or snow tyre.
With the constraint of keeping one wheel class (no derived snowWheel
, for example), how do I model that a car must have 2 snow tyres and 2 standard tyre?
Edit: edited to remove the "mandatory" boolean attribute
答案1
得分: 3
你必须为关联添加一个约束。这可以简单地写成一个带有花括号中文本的附加注释,比如{必须有这种和那种类型的轮胎}
。这也可以用OCL来编写,但我对此并不太熟悉,仍然认为这更多是学术而不是实际水平。
当然,你可以扩展你的模型以包括不同的轮胎专业化,并使用一般化子集。这取决于你的需求。
英文:
You have to attach a contraint to the association. That can simply be witten as an attached note with text in curly brackets like {must have this and that sort of tyres}
. This could as well be written in OCL, but I'm not very firm in that and still think it's still more on an academic than practical level.
Of course you could extend your model with diffents tyre specializations and use generalization subsets. Depends on your needs.
答案2
得分: 3
除了类图之外,您还可以使用复合结构图来实现此目的:
如果您想继续使用常规的类图,您可以将一个附注附加到组合关系上:两个带有标准轮胎的车轮和两个带有雪地轮胎的车轮
。
或者,您可以将组合替换为两个组合,如下所示:
请注意,汽车一侧的多重性是0..1
,因为车轮要么是标准的,要么是雪地的,不能同时兼有。这一事实,以及每个车轮都属于一辆汽车,必须在约束中指定。
您还可以将与车轮的关系指定为类属性:
这样,您就不再拥有组合,但优点是在您的汽车被销毁时仍然拥有车轮
英文:
In addition to the class diagram, you could use a composite structure diagram for this purpose:
If you want to stick to a regular class diagram, you could attach a note to the composition relationship: two Wheels with standard tyres and two Wheels with snow tyres
.
Alternatively, you could replace the composition by two compositions, as follows:
Note that the multiplicities on the Car side are 0..1
, because a Wheel is either standard or snow, not both. This fact, and the fact that every Wheel belongs to a Car, must be specified in a constraint.
You can also specify the relationship with the Wheels as class attributes:
You don't have composition anymore then, but the advantage is that you still have the wheels when your car is destroyed
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论