您的位置:首页 > 编程语言 > C语言/C++

UML 之 C++类图关系全面剖析

2008-10-16 08:52 260 查看
UML的类图关系分为: 关联、聚合/组合、依赖、泛化(继承)。而其中关联又分为双向关联、单向关联、自身关联;下面就让我们一起来看看这些关系究竟是什么,以及它们的区别在哪里。

1、关联
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://p.blog.csdn.net/images/p_blog_csdn_net/dylgsy/doubleAssoc.JPG" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>

双向关联:
C1-C2:指双方都知道对方的存在,都可以调用对方的公共属性和方法。

在GOF的设计模式书上是这样描述的:虽然在分析阶段这种关系是适用的,但我们觉得它对于描述设计模式内的类关系来说显得太抽象了,因为在设计阶段关联关系必须被映射为对象引用或指针。对象引用本身就是有向的,更适合表达我们所讨论的那种关系。所以这种关系在设计的时候比较少用到,关联一般都是有向的。

使用ROSE 生成的代码是这样的:
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>class C1
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>...{
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>public:
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0> C2* theC2;
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>};
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>class C2
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>...{
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>public:
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0> C1* theC1;
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>};

双向关联在代码的表现为双方都拥有对方的一个指针,当然也可以是引用或者是值。

screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://p.blog.csdn.net/images/p_blog_csdn_net/dylgsy/uniAssoc.JPG" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>

单向关联:
C3->C4:表示相识关系,指C3知道C4,C3可以调用C4的公共属性和方法。没有生命期的依赖。一般是表示为一种引用。

生成代码如下:
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>class C3
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>...{
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>public:
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0> C4* theC4;
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>};
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>class C4
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>...{
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>};

单向关联的代码就表现为C3有C4的指针,而C4对C3一无所知。

screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://p.blog.csdn.net/images/p_blog_csdn_net/dylgsy/selfAssoc.JPG" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>

自身关联(反身关联):
自己引用自己,带着一个自己的引用。

代码如下:
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>class C14
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>...{
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>public:
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0> C14* theC14;
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>};

就是在自己的内部有着一个自身的引用。

2、聚合/组合

当类之间有整体-部分关系的时候,我们就可以使用组合或者聚合。

screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://p.blog.csdn.net/images/p_blog_csdn_net/dylgsy/Aggregation.JPG" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>

聚合:表示C9聚合C10,但是C10可以离开C9而独立存在(独立存在的意思是在某个应用的问题域中这个类的存在有意义。这句话怎么解,请看下面组合里的解释)。

代码如下:
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>class C9
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>...{
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>public:
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0> C10 theC10;
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>};
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>class C10
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>...{
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>};
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>

screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://p.blog.csdn.net/images/p_blog_csdn_net/dylgsy/Composition.JPG" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>

组合(也有人称为包容):一般是实心菱形加实线箭头表示,如上图所示,表示的是C8被C7包容,而且C8不能离开C7而独立存在。但这是视问题域而定的,例如在关心汽车的领域里,轮胎是一定要组合在汽车类中的,因为它离开了汽车就没有意义了。但是在卖轮胎的店铺业务里,就算轮胎离开了汽车,它也是有意义的,这就可以用聚合了。在《敏捷开发》中还说到,A组合B,则A需要知道B的生存周期,即可能A负责生成或者释放B,或者A通过某种途径知道B的生成和释放。

他们的代码如下:
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>class C7
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>...{
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>public:
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0> C8 theC8;
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>};
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>class C8
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>...{
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>};

可以看到,代码和聚合是一样的。具体如何区别,可能就只能用语义来区分了。

3、依赖

screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://p.blog.csdn.net/images/p_blog_csdn_net/dylgsy/Dependancy.JPG" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>

依赖:
指C5可能要用到C6的一些方法,也可以这样说,要完成C5里的所有功能,一定要有C6的方法协助才行。C5依赖于C6的定义,一般是在C5类的头文件中包含了C6的头文件。ROSE对依赖关系不产生属性。

注意,要避免双向依赖。一般来说,不应该存在双向依赖。

ROSE生成的代码如下:

screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>// C5.h
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>#include "C6.h"
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>class C5
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>...{
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>};
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>// C6.h
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>#include "C5.h"
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>class C6
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>...{
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>};
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>

虽然ROSE不生成属性,但在形式上一般是A中的某个方法把B的对象作为参数使用(假设A依赖于B)。如下:
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>#include "B.h"
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>class A
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>...{
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0> void Func(B &b);
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>}

那依赖和聚合/组合、关联等有什么不同呢?

关联是类之间的一种关系,例如老师教学生,老公和老婆,水壶装水等就是一种关系。这种关系是非常明显的,在问题领域中通过分析直接就能得出。

依赖是一种弱关联,只要一个类用到另一个类,但是和另一个类的关系不是太明显的时候(可以说是“uses”了那个类),就可以把这种关系看成是依赖,依赖也可说是一种偶然的关系,而不是必然的关系,就是“我在某个方法中偶然用到了它,但在现实中我和它并没多大关系”。例如我和锤子,我和锤子本来是没关系的,但在有一次要钉钉子的时候,我用到了它,这就是一种依赖,依赖锤子完成钉钉子这件事情。
组合是一种整体-部分的关系,在问题域中这种关系很明显,直接分析就可以得出的。例如轮胎是车的一部分,树叶是树的一部分,手脚是身体的一部分这种的关系,非常明显的整体-部分关系。

上述的几种关系(关联、聚合/组合、依赖)在代码中可能以指针、引用、值等的方式在另一个类中出现,不拘于形式,但在逻辑上他们就有以上的区别。

这里还要说明一下,所谓的这些关系只是在某个问题域才有效,离开了这个问题域,可能这些关系就不成立了,例如可能在某个问题域中,我是一个木匠,需要拿着锤子去干活,可能整个问题的描述就是我拿着锤子怎么钉桌子,钉椅子,钉柜子;既然整个问题就是描述这个,我和锤子就不仅是偶然的依赖关系了,我和锤子的关系变得非常的紧密,可能就上升为组合关系(让我突然想起武侠小说的剑不离身,剑亡人亡...)。这个例子可能有点荒谬,但也是为了说明一个道理,就是关系和类一样,它们都是在一个问题领域中才成立的,离开了这个问题域,他们可能就不复存在了。

4、泛化(继承)

screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://p.blog.csdn.net/images/p_blog_csdn_net/dylgsy/inheri.jpg" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>

泛化关系:如果两个类存在泛化的关系时就使用,例如父和子,动物和老虎,植物和花等。
ROSE生成的代码很简单,如下:
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>#include "C11.h"
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>class C12 : public C11
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>...{
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>};

5、这里顺便提一下模板

screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://p.blog.csdn.net/images/p_blog_csdn_net/dylgsy/Template.jpg" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>

上面的图对应的代码如下:
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>template<int>
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>class C13
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>...{
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>};
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='点击在新窗口浏览图片/nCTRL+Mouse 滚轮可放大/缩小';}" border=0>

这里再说一下重复度,其实看完了上面的描述之后,我们应该清楚了各个关系间的关系以及具体对应到代码是怎么样的,所谓的重复度,也只不过是上面的扩展,例如A和B有着“1对多”的重复度,那在A中就有一个列表,保存着B对象的N个引用,就是这样而已。

好了,到这里,已经把上面的类图关系说完了,希望你能有所收获了,我也费了不少工夫啊(画图、生成代码、截图、写上,唉,一头大汗)。不过如果能让你彻底理解UML类图的这些关系,也值得了。


——————————————————————————

评论

#index 发表于2006-08-17 11:42:00 IP: 218.81.82.*
对象(或类)间的关系就两种:引用和继承。 其他关系都是在这两种基础上演化而成。
#jackezou 发表于2006-08-17 22:04:00 IP: 221.216.173.*
聚合和组合那部分好象有些问题,两者生成的代码怎么会是一样的呢?
记得vckase.com有一篇文章《C++编程杂谈之四:漫谈UML
》,里面说聚合是一种相对松散的关系,因此C9应该含有C10的指针而不是C10的对象。

希望作者能继续就这问题解释一下,谢谢!!
#一雨田 发表于2006-08-17 22:33:00 IP: 219.136.188.*
对,聚合是一种相对松散的关系,文章里也说了,在ROSE里面生成的代码确实和组合是一样的。他们的松散应该不在于是指针、值或者引用,你可以尝试用ROSE画两个图——聚合和组合,你可能发现ROSE不提供“组合关系”这种图形(实心菱形,我用的是ROSE2002),然后你打开关系(Aggregation)的Specification->Role B Detail,你会发现有三项选(By Value, By Reference, Unspecified),在你选上By Value的时候,菱形就变成实心的了,其他两个都是空心菱形,这三种我都试过了,生成的代码都是一样的,所以我想组合和聚合的区别应该是一种语义上的区别,语义上的松散。当然在我们的印象中可能指针是比对象要松散一点,因为如果是对象的话,就要管它的生命期了,所以实际应用时,我觉得可以象你说的那样,聚合用指针,组合用对象。:)
#onestation 发表于2006-08-23 12:49:00 IP: 61.144.175.*
实心菱形我们这里叫强聚合,比如说建一个房子类,里面一定包括门,窗等,而桌子板凳可有可无,门窗这些就用强聚合(实心菱形)表示,桌子板凳就是空心地。所以空心菱形应该用指针,值为实心。
#hehe 发表于2006-08-25 09:12:00 IP: 61.187.54.*
嗯,实心的是强聚合,有人也叫“构成”。整体对象的存在也依赖于部分对象。当它的局部对象析构以后,整体对象也应该析构。
#caodixy 发表于2006-12-22 13:11:20 IP:
“我觉得可以象你说的那样,聚合用指针,组合用对象”

按照 一雨田这样说,那么关联也是用指针,聚合也是指针,那这两者又如何区分呢?
#dylgsy 发表于2006-12-28 00:02:00 IP: 61.144.107.*
caodixy,你好!

其实这些关系不应该从代码去区分,上面很多提到了语义的概念,其实UML里的很多概念应该从语义去区分。聚合是一种“整体-部分”概念,而关联是一种关系的概念。

老公和老婆是一种关联,他们的关系是夫妻关系;

身体和手、汽车和轮胎呈现一种“整体-部分”的概念。
#sunlightasean 发表于2007-01-22 11:00:19 IP: 192.160.10.*
不错!UML更偏向语义。
#d5aaron 发表于2007-04-10 15:55:38 IP: 218.80.192.*
有一点不太明白,对于java而言没有指针的概念,那根据生成的代码来看,聚合和关联是不是就一致了呢?
#天下无双 发表于2008-01-05 15:56:58 IP: 61.187.64.*
聚合/组合 的区分:

两者都表示 整体-部分 的关系。
但 聚合 的 整体-部分 是可分离的, 如 停车场-汽车

代码如下:

class Park
{
public:
Car MyFerraris [];//这里考虑到停车场可能有多辆车,所以用数组来表示,当然私人车库只能停放一辆法拉利也可以这样表示 Car MyFerrari
};

class Car
{

};

而 组合 的 整体-部分 是不可分离的,两者的生命周期相同,唇齿相依,唇亡齿寒

代码如下:

class Car
{
public:
Tyre tyre1;

Car()
{
tyre1=new Tyre(); //Car的构造函数中创建轮胎tyre1,因为两者共生
}

};

class Tyre // tyre 之意为 轮胎
{

};

对于UML中的类间关系 有时候 两种 不同的关系 所产生的代码是相同的,这就可能从 具体的应用环境 上去区分了。

#xiaoyvr 发表于2008-06-19 02:12:30 IP: 125.33.175.*
不错的总结,思路很清晰。

引用类型该在这里面能扮演什么角色呢?
[作者:一雨田 原帖:http://blog.csdn.net/dylgsy/archive/2006/08/16/1076044.aspx ]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: