0

I have a question. If ClassA aggregates ClassB and ClassC and ClassA calls a method of Class C in which it uses attributes from object of ClassB (passed by arguments) should I connect ClassB and ClassC or aggregation is enough?

Image for example: Diagram UML

Implementation:

class ClassA {
    constructor() {
        this.stringA = "Hello"
        this.objB = new ClassB();
        this.objC = new ClassC();
    }
    functionA() {
        this.objB.functionB(this.stringA);
        this.objC.functionC(this.objB.intB);
    }
}

class ClassC {
    constructor() {}
    functionC(intB) {
        console.log(intB);
    }
}

class ClassB {
    constructor() {
        this.intB = 10;
    }
    functionB(stringA) {
        console.log(stringA);
    }
}

Thanks for your help

Zar
  • 5
  • 1
  • 7
  • So what's the difference to https://stackoverflow.com/questions/54056721/associations-between-the-classes-with-aggregation except that you use another UML notation not matching the code? – qwerty_so Jan 06 '19 at 22:55

1 Answers1

0

In your diagram you use compositions, not 'just' aggregations, that means the instances of ClassB and ClassC will disappear when the instance of ClassA will disappear.

The goal of the compositions and aggregations is not to say a class calls a method of an other class or use one of its attribute.

If ClassB uses an attribute of ClassC (already strange, is it public ?) you can use a dependency, but if you add a dependency each time you take the risk to have a dependency between a lot of classes, and the goal of a class diagram is not really to indicate that

bruno
  • 32,421
  • 7
  • 25
  • 37
  • so if I create a getter for intB in ClassB and in functionA I will call that getter (this.objC.functionC(this.objB.getIntB()) I don't need to use a dependency between ClassB and ClassC? – Zar Jan 06 '19 at 18:18
  • _functionC receives the value of _this.objB.getIntB()_ in argument, but ClassB don't know that, nor may be ClassC, and this is detail no ? If really the fact a class uses an other one is really important you can indicate that through a dependency, but may be it is is better to define interfaces and components to show require/provide ? A diagram must not be complicated with relations between all as a spider web – bruno Jan 06 '19 at 18:33