Associations between the classes with aggregation

故事扮演 提交于 2019-12-11 18:45:16

问题


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

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:


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

(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.



来源:https://stackoverflow.com/questions/54056721/associations-between-the-classes-with-aggregation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!