类与聚合之间的关联 [英] Associations between the classes with aggregation

查看:89
本文介绍了类与聚合之间的关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个UML类图,并且不确定是否正确完成了操作.

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

我将在示例中向您展示. 因此,ClassA具有ClassC的实例,并且ClassC的ClassA调用方法将ClassA和ClassB的属性变量作为属性.我也应该将ClassC与ClassB连接吗?是否应指导ClassA和ClassB之间的关联?下面的代码的实现:

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();

感谢您的帮助:)

推荐答案

复合聚集与对象的生命周期有关.您的代码中没有显示出对此的需求.

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

(N.B.我不知道Java范围规则,所以我将this.objB视为私有.)有一个关联,它是ClassA的拥有属性(由点表示),名为objB.对ClassC的依赖来自(暂时使用的)objC,它不在类级别上.可以讨论某个关联是否匹配(老实说,我不得不深入研究规格),但是在任何情况下依赖都没有错(因为它比关联弱,并且A肯定取决于B ).

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

另一个N.B.回覆.您的CD:代码中的函数采用字符串,而不是对象,并且其中之一缺少结尾的1.

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

这篇关于类与聚合之间的关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆