How to represent shared objects in UML?
Asked Answered
L

1

6

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?

Longitude answered 4/11, 2016 at 10:2 Comment(5)
Why does B need shared ownership of CSharedObj? The lifetime of B is the same as the lifetime of A so it could safely have a non-owning pointer to CSharedObj, no?Bookerbookie
What you say is theoretically correct, but the architecture is a bit more complicated than the example above. The idea is that A creates everything that is needed and distributes it to each sub-component (let's say we have B1, B2, B3, ..., Bn sharing some resources created by A). You don't want to create multiple resources since they have a 1-to-1 relationship with hardware modules, but it is not either needed to store them in A, since it does not do anything with them except initializing and passing to Bi.Longitude
Ok, if there are more than one B that makes more sense and I didn't spot that A doesn't store the shared object.Bookerbookie
Where do you see 3 classes? There are just two, namely A and B.Allina
The third class is CSharedObject. I did not write the definition because it would have been a trivial one (class CSharedObject {};) and it is already clear that it is a type and not an instance since it is the template argument of a shared_ptr.Longitude
M
7

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.

Mcintire answered 5/11, 2016 at 20:39 Comment(5)
Well, CSharedObject is what the name says: an object. Not a class. You happily mix classes and objects as if all were classes.Allina
@ThomasKilian; It's a long time ago I have developed in C++, but I think it's a class. See the example at en.cppreference.com/w/cpp/memory/shared_ptr. It's a good naming convention to name a class after what it's like when it is instantiated. For example, "Person" is a good name for a class, although every Person is an object.Mcintire
A class is defined after the class keyword. The OP is wrongly using the word class where instance is meant.Allina
@ThomasKilian, The declaration of class CSharedObject is not present in this code snippet, but I expect the complete working application has that declaration somewhere in the source code.Mcintire
I think I know what you mean :-/ Please make a dummy edit of your answer so I can alter my voting.Allina

© 2022 - 2024 — McMap. All rights reserved.