Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to represent shared objects in UML?

To design my code I am drawing some UML Class diagram. I have some shared objects and I am wondering how that should be drawn since the ownership of these objects is really shared. To be more specific here a C++ example of what is happening:

class A
{
public:
  A(){
    std::shared_ptr<CSharedObj> sharedObj = std::make_shared<CSharedObj>;
    mB = B(sharedObj);
  }
private:
  B mB;
};

class B
{
public:
  B(std::shared_ptr<CSharedObj>);
private:
  std::shared_ptr<CSharedObj> mSharedObj;
};

class CSharedObj
{
public:
  CSharedObj();
};

How do I represent in a class diagram the relationship between these 3 classes?

like image 758
San Mosy Avatar asked Nov 04 '16 10:11

San Mosy


1 Answers

UML doesn't define exactly how this piece of C++ code should be reflected in a diagram. There are several options and here is my suggestion:

enter image description here

I have used composition (filled diamond) for mB, because an instance of B is destructed when the enclosing instance of A is destructed.

Because the instance of CSharedObject can potentially be shared by multiple owning objects, I have used shared aggregation (open diamond) for mSharedObj. Throughout your project, you could set the convention that objects pointed to by shared_ptr are represented in class diagrams using the shared aggregation relationship.

Note, that this class diagram doesn't specify that the instance of CSharedObject created by A is the same instance pointed to by mSharedObj. If you want, you can add a note to describe this fact.

If you really want to show the shared_ptr in your diagram, you could do it like this:

enter image description here

See UML 2.5 specification section 9.3 for more information on templated classifiers.

like image 89
www.admiraalit.nl Avatar answered Oct 02 '22 08:10

www.admiraalit.nl