对象的 class 可以在 UML 中改变吗?

Can the class of an object change in UML?

简而言之

主流class-based OO languages不允许对象的class在创建后改变。我在 UML 中假设了相同的约束,特别是基于 UML 2.5 规范中的以下条款:

7.5.1: Types and multiplicity are used in the declaration of Elements that contain values, in order to constrain the kind and number of values that may be contained.

9.2.3.2: An instance of a Classifier is also an (indirect) instance of each of its generalizations.

然而,在对 的评论中,几位技术娴熟的 UML 专家对这一假设提出了质疑。因此我的问题是:实例可以在 UML 中更改 class 吗? UML 规范中的哪些条款允许进行此类更改?

附加参数

虽然我在 UML 规范中找不到关于实例和 classes 之间关系的任何更明确的条款,但我解释了以下提示以支持固定的 class:

11.4.3.1: (...) An active object is an object that, as a direct consequence of its creation, commences to execute its classifierBehavior, and does not cease until either the complete Behavior is executed or the object is terminated by some external object.

(它是特定于活动对象的。但是如果对象在创建后可以改变 class,它就不能再活动了,或者它会保持旧的行为与新的 class,这看起来很奇怪):

此外,如果实例的 class 可以动态更改,则有多个子句会导致歧义。例如在 6.5.1 中:

A class may also have invariant conditions that must be true before and after the execution of the operation but may be violated during the course of the execution of the operation method.

如果在对 classifier 实例的操作期间不变量不再为真,会发生什么情况:该实例是否只是失去了 class 实例的质量?会不会是与任何 class 完全不相关的实例?

The semantics of properties specify that, when a property with a default value is instantiated, in the absence of some specific setting for the property, the default value is evaluated to provide the initial values of the property

如果一个实例不再是一个具有默认值的 class 的实例,它会突然获得另一个 class 的默认值吗?或者,如果一个实例可以匹配两个相似的 classes:它会得到两者的默认值吗?

我可能已经找到了困扰您的部分,基本上这就是多重​​继承发挥作用的地方(这肯定是邪恶的)。

9.9.9 InstanceSpecification [Class]

9.9.9.1 Description

An InstanceSpecification is a model element that represents an instance in a modeled system. An InstanceSpecification can act as a DeploymentTarget in a Deployment relationship, in the case that it represents an instance of a Node. It can also act as a DeployedArtifact, if it represents an instance of an Artifact.

[...]

9.9.9.5 Association Ends

  • classifier : Classifier [0..*] (opposite A_classifier_instanceSpecification::instanceSpecification) The Classifier or Classifiers of the represented instance. If multiple Classifiers are specified, the instance is classified by all of them.

给你:多重分类。所以 InstanceSpecification(对象)确实有点像变色龙,可以有不止一种形式。

另外规范说:

9.8 Instances

9.8.3. Semantics

[...]

An InstanceSpecification may represent an instance at a point in time (a snapshot). Changes to the instance may be modeled using multiple InstanceSpecification, one for each snapshot.

It is important to keep in mind that InstanceSpecification is a model element and should not be confused with the instance that it is modeling. [...]

使用 InstanceSpecification 将及时显示对象的表示。如上所示,这可以改变其分类的限制。

对象可以改变它们的 class。至少在现实世界中,这是显而易见的。 Student 可能变成 ResearchAssistent,甚至可能变成 ProfessorCar 可能会在一段时间后变成 ClassicCar。 UML 确实支持这一点:

16.4.3.7 Reclassify Object Actions A ReclassifyObjectAction is an Action that changes which Classifiers classify the object given on its object InputPin. It may both add and remove Classifiers from the object. Multiple Classifiers may be added and removed at one time. [...] The identity of the input object is preserved, no behaviors are executed, and no default value expressions are evaluated. The newClassifiers replace existing classifiers in an atomic step, so that structural feature values and links are not lost during the reclassification when the oldClassifiers and newClassifiers have structural features and associations in common.

重新class化是否有意义,这不是问题。在对您有意义的时候使用它。当然,你可以模型废话。

不用说,所有新的 Class 不变量必须在更改 class 后得到满足。如果新不变量涉及旧 class 的特征,则可能会出现新不变量无法用旧值实现的问题。它们必须通过更改 class.

的行为同时更新

默认值在这里不是问题。 class 的行为负责确保设置默认值,它不是自动的。如果您更改 class,新 class 中仍然存在的特征值将被原封不动地接管。

当你从主动 class 重新class 到被动 class 时,那么 classifier 行为就会停止。不过,我怀疑这种情况是否有用。

PS:上面已经说过,InstanceSpecifications 指定对象作为一些classifier的实例,它们不是对象。因此,我认为无论规范如何描述它们,都与我们的讨论无关。