Link操作 :: Dominoo
未识别博客 · darwin_yuan · 2004-10-20
Dominoo
Design Of Model IN Object-Oriented
|
首页
|
相等性判断的自动化>>>
Link操作
时间: 2004-10-20
在UML的class图中,class与class之间可以存在关联(Association),它意味着,关联两端的类的实例(Instance)之间,或者类与实例之间(如果一个Association End是静态的)可以建立起link。而Link操作正是为了这一目的而存在的。
1. 语法
Link操作的语法为:
1.
relate
<object instance handle>
to
<object instance handle> [
across
<association specification> ]
2.
relate
<object instance handle>
to
<class> [
across
<association specification> ]
3.
relate
<object instance handle>
to
<object instance handle> [
across
<association specification> ]
using
<object instance handle>
当两个类之间存在关联,关联的两端都不是静态的,就使用第一种语法。
当两个类之间存在关联,并且关联的一端是静态的(两端都是静态的关联是非法的),则是用第二种语法。
当两个类之间存在一个关联类(关联类的关联两端不能使静态的),则使用第三种语法。
其中,association specification的形式为:
R<number>
r<number>
R<number>.<role>
r<number>.<role>
o 普通关联
如果两个类之间存在着普通关联,当设计师需要将两个类实例通过这个普通关联link起来时,使用第一种语法。比如:
|--------| R1 |-------|
| A |---------| B |
|--------| 1 *|-------|
create object instance a of A;
create object instance b of B;
relate a to b across R1;
o 静态关联
如果两个类之间存在着静态关联,则使用第2种语法.例如:
|--------| R1 *|-------|
| A |------------>| B |
|--------|
all bars
|-------|
create object instace b of B;
relate b to A across R1.'all bars';
o 关联类
如果两个类之间存在一个关联类,你必须使用之前所述的第三种语法,即必须通过using指明关联类的实例. 比如:
|--------| R1 |-------|
| A |---------| B |
|--------| 1 | *|-------|
|
|
|---------|
| C |
|---------|
create object instance a of A;
create object instance b of B;
create object instance c of C;
relate a to b across R1 using c;
2. association specification
上述3种语法中的across部分,并非在任何情况下都是需要的,在不引起二义性的情况下,我们可以尽量的简化。下面我们就各种不同的情况来逐一讨论。
o 单一关联
如果两个类之间只存在一个关联,在进行link操作时,你无需指定association specification. 比如:
|--------| R1 |-------|
| A |---------| B |
|--------| 1 *|-------|
图中,class A和B之间仅仅只存在关联R1,当你执行A和B的实例之间的link操作时,不需要指定association specification,编译器为自动识别这一点。比如:
create object instance a of A;
create object instance b of B;
// relate a to b across R1;
relate a to b;
o 多个关联
如果两个类之间存在一个以上的关联,在进行link操作时,你必须指定association specification. 比如:
|--------| R1 |-------|
| |-----------| |
| | 1 * | |
| A | R2 | B |
| |-----------| |
| | * 0..1| |
|--------| |-------|
这种情况,如果不指定association specification,编译器无从知道需要在两者之间建立那种link,这就会引起二义性。此时,设计师需要明确的指定association specification。比如:
create object instance a of A;
create object instance b of B;
relate a to b across R2;
两个类之间存在的关联也应该把它们的super class的关联计算在内,比如:
|--------| |-------|
| B0 |<|------| B |
|--------| |-------|
| 1 |*
R1| R2|
| * |0..1
|--------| |-------|
| A0 |<|------| A |
|--------| |-------|
以及这种情况:
|--------| |-------|
| B0 |<|------| B |
|--------| |-------|
| 1 |*
R1| R2|
| |0..1
| |-------|
|-------------| A |
* |-------|
这两个例子中,Class A和B之间的关联数量都为2,在link A和B的实例时,都必须指定Association specification.
或许你会建议,在这些情况下, 如果不指定association specification,可以认为设计师的意图是在两个实例之间建立所有的link,这样做看起来合情合理,但事实上存在很大的问题. 这是因为:
1.在现实的模型中,如果两个类之间存在多个关联,往往从语义上,这些关联基本上不会同时出现在同样的两个实例之间.
2.给设计师提供了更多由于疏忽而造成错误的机会.
如果设计师确实想在两个实例之间建立起所有可能存在的link,它必须亲自逐个的执行link操作.
o 自关联
如果关联存在于在两个不同的类之间,当你指定association specification的时候,你只需要制定关联的名字.但,如果一个关联是一个自关联,
你首先必须使用association specification,其次你还必须在association specification中指名角色.
例如:
|------------|
| |
| A | parent
| |------|
| |0..1 |
|------------| |
*| children | R1
| |
|------------|
依据这张图,如果你想将class A的两个实例a和b通过R1连接起来,你必须制定它们二者所需要扮演的角色,否则会引起二义性. 比如:
create object instance a of A;
create object instance b of A;
// a is a child of b
relate a to b across R1.children;
最后一条语句指明了,在a和b建立起的新的连接中,a扮演的角色是children,b扮演的则是parent.
o 普通关联,关联类和静态关联
需要特别指明的是,普通关联,关联类和静态关联是三种不同的关联,在考虑是否会引起二义性的时候,需要分开考虑.例如:
|--------| R1 1..*|---------|
| |------------| |
| | | |
| | R2 * | |
| A |----------->| B |
| |
bars
| |
| | R3 | |
| |------------| |
|--------| | |---------|
|
|----------|
| C |
|----------|
在这个例子中,class A和B之间存在三个关联,其中:
R1是普通关联;
R2是静态关联;
R3是关联类。
尽管A,B之间存在三个关联,但由于他们的类型不同,所以我们对他们进行关联的操作的时候,无需指定association specification。如下:
create object instance a1 of A;
create object instance b1 of B;
// relate a1 to b1 across R1;
relate a1 to b1;
create object instance b2 of B;
// relate b2 to B accross R2;
relate b2 to B;
create object instance a3 of A;
create object instance b3 of B;
create object instance c of C;
// relate a3 to b3 across R3 using c;
relate a3 to b3 using c;
darwin_yuan
引用(Trackback0)
|
编辑
评论
发表评论
最后更新
相等性判断的自动化
Link操作
在GC系统上的Destroy语义
Composite的约束
再谈Invariant Constraint
Invariant Constraint
Dominoo
原始快照:web.archive.org