To Bruce, 我认为从代码的角度理解聚合、关联关系完全是错误的。给你一段代码: public class A { private B ref; public A( B ref ) { this.ref = ref; } ... } 你怎么知道class A和class B是什么关系? 既可以是关联关系,也可以是合成关系,也可以是聚合关系。我觉得这三者要从语义上去理解。
jiangxi
2003-06-18 16:43
同意wwlhp@jdon.com ,我觉得3种关系都能产生 public class A { private B ref; public A( B ref ) { this.ref = ref; } ... } public class B{ ... } 应该从语意,内涵上去理解。比如: 关联:人->习惯,可有可无的东西 聚合:多边形<>->边,角 关系不是很紧密 组合:圆《》->圆心,圆弧 圆由圆心和圆弧组成
====== 组合 ====== public class A { //Constructor function must include B and C, all of them have the same life cycle. public A() { B b = new B(); C c = new C(); } ... } public class B{ } public class C{ } ====== 聚合 ====== public class A { public A() { } /* This method is a must method in class A, but this method is called after the constructor function is finished. So A and B,C have the different life cycle. Class A could exist without B,C*/
public void MethodOfA(){ B b = new B(); C c = new C();
} public class B{ } public class C{ } ======== 关联 ======== public class A { public A() { }
/* This method is not a must method in class A Class A only have association with B C when this method is called */
public void Method-need-BC(){ B b = new B(); C c = new C();
} public class B{ } public class C{ }
jiangxi
2003-06-19 08:23
楼上哥们写的我觉得是:依赖关系,符号------>
SUPERMY
2003-06-19 11:05
我认为逻辑上,越来越强。但是,实现上是一样的,没有区别。
ripper
2003-06-23 16:00
我不懂,我查书
Association: Uses the services of another class
Aggregation: A class “owns” another class
Composition: A class is composed of another class; refers to an aggregation within which the component parts and the larger encompassing whole share a lifetime