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?
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:
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:
See UML 2.5 specification section 9.3 for more information on templated classifiers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With