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?

SCFrench
  • 8,244
  • 2
  • 31
  • 61
San Mosy
  • 115
  • 2
  • 6
  • 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? – Chris Drew Nov 04 '16 at 10:21
  • 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. – San Mosy Nov 04 '16 at 10:34
  • 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. – Chris Drew Nov 04 '16 at 11:35
  • Where do you see 3 classes? There are just two, namely `A` and `B`. – qwerty_so Nov 06 '16 at 15:34
  • 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. – San Mosy Nov 09 '16 at 12:42

1 Answers1

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.

www.admiraalit.nl
  • 5,768
  • 1
  • 17
  • 32
  • Well, `CSharedObject` is what the name says: an object. Not a class. You happily mix classes and objects as if all were classes. – qwerty_so Nov 05 '16 at 22:18
  • @ThomasKilian; It's a long time ago I have developed in C++, but I think it's a class. See the example at http://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. – www.admiraalit.nl Nov 06 '16 at 13:02
  • A class is defined after the `class` keyword. The OP is wrongly using the word class where instance is meant. – qwerty_so Nov 06 '16 at 15:33
  • @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. – www.admiraalit.nl Nov 06 '16 at 18:40
  • I think I know what you mean :-/ Please make a dummy edit of your answer so I can alter my voting. – qwerty_so Nov 06 '16 at 19:43