3

enter image description here

I can see many UML example like above diagram. I can understand that

  1. Class A has navigability to B with multiplicity of 10. But I can't understand that
  2. Class B has no navigability to A but has multiplicity of 5..7. Is it possible has multiplicity wihtout navigability in UML?

I can implement case 1 like below.

class A {
public:
    B b[10];
};

But how can I implement case 2? No navigability but has multiplicity? I am totally confused of case 2. What am I missing?

Christophe
  • 68,716
  • 7
  • 72
  • 138
myoldgrandpa
  • 871
  • 7
  • 21

1 Answers1

2

Yes, it is possible to have a multiplicity without navigability.

Navigability only makes a promise about the ease at runtime to find instances at the other end. The fact the there is no promise on navigability (or even an explicit absence of navigability with a X at the opposite end) does not change the existence of an association and hence the multiplicity.

The implementation of 2) could for example be done with a vector of pointers to A in B to navigate back (the navigability would exist, but is simply not indicated). Another implementation coukd be to use in A a vector of pointers to B, so that B can be shared, and some other mechanism make sure it is shared by 3 to 7 A's (no navigability, but multiplicity enforced)

Christophe
  • 68,716
  • 7
  • 72
  • 138
  • Let's assume the condition of that A has navigability to B but B has no navigability to A. Still can you implement above diagram? – myoldgrandpa Mar 01 '23 at 07:36
  • Yes. Make attribute `b` private. Add a public attribute `countA : int` to `B`. Add operation `relateB(B b)` to `A`. This operator adds its argument to `b` and increments `countA`, but it will raise an error if `countA` is 7. Let `B` have a constructor that enforces the minimum of 5 A's related to B. – www.admiraalit.nl Mar 01 '23 at 08:16
  • @www.admiraalit.nl My point is not how to limit the number of instances but how does class B has multiplicity without navigability to A. Is it possible to implement? – myoldgrandpa Mar 01 '23 at 08:31
  • 2
    Multiplicity is nothing more than a constraint on the number of instances. Implementing a multiplicity means enforcing this constraint. Maybe you have a different definition of multiplicity? – www.admiraalit.nl Mar 01 '23 at 08:50
  • @myoldgrandpa you may have an external validation, where A sends the information about every update of assignment of B and that checks if the validation is met. I can imagine it could be some static attribute of A tracking all Bs and their assignments or this could be an association class, just not shown explicitly on the diagram or... Possibilities are virtually endless. – Ister Mar 01 '23 at 11:58
  • @myoldgrandpa the indication of multiplicity can give some hints about the context in which B is used. The multiplicity is not necessarily be enforced by the objects involved. You could for example have another object that creates the A and B and establishes the links between them. This intermediary could know both sides, even if B has no easy way to find its As – Christophe Mar 01 '23 at 12:28
  • @www.admiraalit.nl If there is no way to implement multiplicity without navigability, that means if there is multiplicity it must have navigability. So, event though there is no navigability from B to A in the diagram, B must have navigability to A implicitly. Am I correct? – myoldgrandpa Mar 01 '23 at 14:44
  • There is always a multiplicity. Just sometimes, it's not specified. Multiplicity is a statement about structure. Navigability is a statement about performance. Let's take a simpler example, with simpler multiplicity: a Car uniquely , identified by its [VIN number](https://en.m.wikipedia.org/wiki/Vehicle_identification_number) and RegistrationDocument: the document refers to the car. There is an association between Car and the RegistrationDocument. It is navigable from the document to the car, with a multiplicity of 1. There are 0..* registration documents for a car .../... – Christophe Mar 01 '23 at 20:01
  • .../... (can be registered in several countries, some documents might be obsolete,...). You can decide to implement the association in a navigable way, letting each car find back the related registration document. But you don't have to (no navigation back) and some could even argue it would infringe the Single Responsibility Principle. But navigable or non-navigable don't change the fact that there is an association (semantic, structural relationship) and the 0..* multiplicity. – Christophe Mar 01 '23 at 20:08