0
votes

I need to create an UML Class Diagram and I am not sure if I've done it correctly.

enter image description here I will show you on example. So ClassA has an instance of ClassB and ClassA calls methods from ClassC which take as attributes variables from ClassA and ClassB. Should I connect also ClassC with ClassB? Does the association between ClassA and ClassB should be directed? The implementation of the code below:

ClassB {
    constructor() {
        this.str1 = "Hello";
    }
}

ClassA {
    constructor() {
        this.str2 = "World";
        this.objB = new ClassB();
    }
    funA() {
        let objC = new ClassC();
        objC.function1(this.objB.str1);
        objC.function2(this.str2);
    }
}

ClassC {
    constructor() {}
    function1(stringFromB) {
        console.log(stringFromB);
    }
    function2(stringFromA) {
        console.log(stringFromA);
    }
}

let objA = new ClassA();
objA.funA();

Thanks for any help :)

1

1 Answers

-1
votes

Composite aggregation is about life-time of objects. There's noting in your code that shows a need for that.

enter image description here

(N.B. I don't know the Java scoping rules so I took this.objB as being private.) There's one association which is an owned property of ClassA (shown by the dot) named objB. The dependency to ClassC comes from the (temporarily used) objC which is not on class level. It could be discussed whether an association would match (honestly I'd had to dig deeper into the specs), but a dependency isn't wrong in any case (because it's weaker than an association and definitely A depends on B).

Another N.B. re. your CD: The functions in the code take strings, not objects and one of them misses the trailing 1.