识别类KLM和ABC之间的UML关系类型

识别类KLM和ABC之间的UML关系类型

本文介绍了识别类KLM和ABC之间的UML关系类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ABC类和KLM之间应该是什么关系?应该是组成,聚合,关联还是任何其他关系.

What should be the relationship between class ABC and KLM ? Whether it should be composition, aggregation or association or any other relationship.

#include<iostream>
using namespace std;
class ABC
{
    int x;
public:
    ABC() { cout<<"1\t"; }
    ~ABC() { cout<<"2\t"; }
};
class KLM
{
    int  y;
    ABC *O1;
public:
    KLM() { cout<<"3\t"; }
    ~KLM() { cout<<"4\t"; }
};
int main()
{
    KLM *O4=new KLM();
    ABC a1;
    delete(O4);

    return 0;
}

推荐答案

是的,至少存在关联:

  • KLM 中的 ABC 指针表明, KLM 的实例与 ABC .此外,该关联可从 KLM 导航到 ABC .
  • 在关联的 ABC 末端,多重性为 0..1 ,因为指针最多可以指向一个对象.但是,我们可以假设它可能是 nullptr ,即指向任何对象.
  • the ABC pointer in KLM shows that there can be a link between instances of KLM and instances of ABC. Moreover the association is navigable from KLM to ABC.
  • The multiplicity is 0..1 at the ABC end of the association, since the pointer can point to one object at most. We can however suppose it could be nullptr, i.e. pointing to no object.

现在代码中存在问题:指针 KLM :: O1 未初始化.如果未在构造函数中明确为其赋值,则应确保它为 nullptr .

Now there is an issue in the code: the pointer KLM::O1 is not initialized. You should make sure it is nullptr if you do not explicitly assign it a value in the constructor.

我们无法进一步了解这种关系,因为我们不知道该指针的分配方式是指向 ABC 对象,例如:

We cannot know more about this relation, since we do not know how this pointer is assigned to point to ABC objects, e.g.:

  • 两个不同的 KLM 对象能否指向相同的 ABC ?这将排除成分.
  • KLM 对象本身是否会创建 ABC 对象并防止其地址被泄露?在这种情况下,我们可能会怀疑UML组成.
  • Could two different KLM objects point to the same ABC? THis would exclude composition.
  • Does a KLM object itself create the ABC object and prevent its address from being disclosed? In this case we could suspect an UML composition.

提示1 :您有一个带有指针成员的类.您应实施 3规则(或5),以避免讨厌的内存管理错误.如果您显示该规则的执行情况,则可以更确定地确定组合是否正确.

Hint 1: You have a class with a pointer member. You should implement the rule of 3 (or 5) to avoid nasty memory management errors. If you show the implementation of the rule, we could tell with more certainty about composition or not.

提示2 :您使用的是c ++ 11标记.在现代C ++中,智能指针的使用优于原始指针.特别是, unique_ptr 建议用于合成的专有所有权. shared_ptr 建议使用聚合来实现共享所有权(但是我们不能排除简单的关联). weak_ptr 表明没有所有权,因此必然是简单的关联.

Hint 2: You use c++11 tag. In modern C++, the use of smart pointers is preferred to raw pointers. In particular, unique_ptr suggests an exclusive ownership typical for composition. A shared_ptr suggests a shared ownership quite common with aggregation (but we could not rule out a simple association). A weak_ptr suggests absence of ownership and hence necessarily a simple association.

备注:代码和模型之间没有一对一的关系.相同的代码可用于实现不同的模型.相同的模型可以用不同的代码实现.设计意图并不总是会陷入代码片段中.因此,与其说是找到一个独特的现实,不如说是或多或少强烈地建议或排除了一些可能性.

Remark: There is no one-to-one relation between code and model. The same code can be used to implement different models. And the same model can be implemented with different code. Design intent is not always caught in a code snippet. So, it's more about suggesting or excluding more or less strongly some possibilities rather than finding a unique reality.

这篇关于识别类KLM和ABC之间的UML关系类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!