[英]Class diagram composition with generalisation
I am trying to show here that Admin is a sub-class of User which is shown through the open triangle relationship connection.我想在这里展示 Admin 是 User 的一个子类,它通过开放的三角形关系连接显示。 Is this enough to show that the sub-class relies on the main parent class to exist or would I then need to further apply composition?
这是否足以表明子类依赖于主要父 class 存在,还是我需要进一步应用组合? I have shown my example below.
我在下面展示了我的示例。 I am super confused regarding this.
我对此感到非常困惑。 I feel it's enough to show it through the specialization arrow but I am not sure.
我觉得通过专业化箭头显示就足够了,但我不确定。
Is this enough to show that the sub-class relies on the main parent class to exist or would I then need to further apply composition?
这是否足以表明子类依赖于主要父 class 存在,还是我需要进一步应用组合?
Because of the generalization an Admin is a User , so if there is no User at all ( User.allInstances()->size() = 0
) then there is no Admin too.由于泛化Admin是User ,所以如果根本没有User (
User.allInstances()->size() = 0
),那么也没有Admin 。 But of course you can have User (s) without having Admin .但是当然,您可以拥有User (s) 而无需Admin 。
If you just want to say that nothing more than the generalization is necessary.如果你只想说,没有什么比泛化是必要的了。
But if you want to indicates an Admin is a User and there is 1 and only 1 admin you can do:但是,如果您想指示管理员是用户并且只有 1 个管理员,您可以执行以下操作:
As you can see admin is a property of the class User ( isStatic is true) because it is written underlined, no need to have it for all instances (including the admin(s)).如您所见, admin是 class用户的属性( isStatic为真),因为它是带下划线的,所有实例(包括管理员)都不需要它。
An other way to say the same without using an additional relation is:不使用附加关系的另一种说法是:
Of course if you want to say there is at least one admin and not exactly one just use the multiplicity 1..*
in the first solution or modify the constraint to have {allInstances()->size() >= 1}
in the second solution.当然,如果您想说至少有一个管理员而不是一个管理员,只需在第一个解决方案中使用多重性
1..*
或修改约束以使{allInstances()->size() >= 1}
在第二个解决方案。
I'd like to add another view to Bruno 's excellent and accurate answer regarding generalization.我想为Bruno关于泛化的出色而准确的答案添加另一种观点。
Yes, it is indeed sufficient to make Admin
a specializatrion of User
: this means that Admin
is a User
.是的,使
Admin
成为User
的专业化确实就足够了:这意味着Admin
是User
。 It also means that Admin
inherits User
's non-private properties and operations (ie AuthenticateUser()
).这也意味着
Admin
继承了User
的非私有属性和操作(即AuthenticateUser()
)。 Finally it means that an instance a
of Admin
is also an instance of User
and has hence a username
and a password, although these are visible only to User
instances.最后,这意味着
Admin
的实例a
也是User
的实例,因此具有username
和密码,尽管这些仅对User
实例可见。 Unfortunately, once an object created as Admin
, it will always stay an Admin
.不幸的是,一旦 object 创建为
Admin
,它将始终保持为Admin
。
Your hesitation about composition is therefore perfectly understandable: it is often advised to prefer composition over inheritance .因此,您对构图的犹豫是完全可以理解的:通常建议您更喜欢构图而不是 inheritance 。 You could perfectly imagine to have a
User
that has an Admin
role, without Admin
inheriting from User
.您可以完美地想象拥有一个具有
Admin
角色的User
,而Admin
没有从User
继承。 But this is a radically different design.但这是一个完全不同的设计。 And in this specific case, it would be strange, if there wouldn't be other roles as well.
在这种特殊情况下,如果没有其他角色,那就太奇怪了。
Last but not the least, it's worth to mention de decorator pattern that may combine inheritance with aggregation (instead of composition): In this design, you would make Admin
a decorator of User
: it extends the reponsibilities of the User with Admin
responsibilities (ie with additional operations).最后但并非最不重要的一点是,值得一提的 de装饰器模式可以将 inheritance 与聚合(而不是组合)结合起来:在这个设计中,您可以让
Admin
成为User
的装饰器:它扩展了具有Admin
职责的 User 的职责(即附加操作)。 But this can be done at runtime: you can create a User
, add an Admin
responsibility, or remove the Admin
responsibility.但这可以在运行时完成:您可以创建
User
、添加Admin
职责或删除Admin
职责。 THis is much more dynamic.这更有活力。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.