user4081625
user4081625

Reputation:

UML - Chained Class References

Class Man

public Box box;
private Gun gun;

public Man()
{
    gun = new Gun(this);
}

Class Box

public Bullets bullets;

public Box() 
{
    bullets = new Bullets();
}

Class Bullets

public Bullets() {}

Class Gun

private Man owner;
private Bullets bullets;

public Gun(Man owner)
{
    this.owner = owner;
    bullets = this.owner.box.bullets; 
}

What would the UML for class Gun be?

Here is what I am thinking:

Gun has an aggregation with Man and Bullets
Gun has a dependency with Box

Would this be correct?

Upvotes: 0

Views: 67

Answers (2)

granier
granier

Reputation: 1789

That's right that using aggregation could be confusing, but a composite is a strong association and helps to understand the model and associations.

In the specification document, it is described section 9.5.3

As I understood, you can use a composition when :

- the composite object has responsibility for the existence and storage of the composed objects
- part object be included in at most one composite
object at a time. If a composite object is deleted, all of its part instances that are objects are deleted with it.
- please, note also that cardinality of the diamond side, by default is 0..1

If I can help to improve the proposed model, I always try to avoid circular association (maybe due to my old C/C++ coding without a garbage collector), so I try to avoid class A associated to class B associated to class C associated to class A.

Hope this helps

Upvotes: 0

qwerty_so
qwerty_so

Reputation: 36313

Basically yes. But specifically the dependency with Box is already given with Man having an association with Box. So you don't need that.

enter image description here

Don't care too much about aggregation since it adds only very little extra semantics to the model. The fact that you have an association is usually enough. You can easily start arguing with "can X live without Y?" but that's pointless. Use composite aggregation if ever in context of foreign key constraints for databases and/or for showing memory management constraints.

You could augment the model with constraints that tell { bullets in box are never bullets in gun} and such. Not to forget multiplicities.

Upvotes: 1

Related Questions