关联类上的操作 :: 沉思者
来源: BlogBus 原始链接: http://www.blogbus.com:80/blogbus/blog/diary.php?diaryid=454720 存档链接: https://web.archive.org/web/20041208145750id_/http://www.blogbus.com:80/blogbus/blog/diary.php?diaryid=454720
沉思者 我思故我在 <<<Link操作(三) | 主页 | Unlink操作>>> 关联类上的操作 2004-10-22 一个关联类(Association Class)即是一个关联又是一个类,所以关联类对象是与关联共生共灭的。比如: |---------|* R1 *|--------| | Student |-------| Course | |---------| | |--------| | |------------|
| Score Card |
|---|
| ------------ |
| +getScore() |
| ------------ |
| 在这个例子中,任何一个Score Card对象的存在都是因为在一个Student对象和一个Course对象之间建立了link所致。所以,如果一个Student对象和一个Course对象之间的link被拆除了,那么它们之间的Score Card对象也应该Destroy。 |
| 基于这种语义,当我们进行Student和Course对象之间的link操作的时候,应该自动创建一个Score Card对象,像这样: |
| create object instance |
| James |
| of |
| Student; |
| create object instance |
| Math |
| of |
| Course; |
| // |
| relate |
| James |
| to |
| Math |
| across |
| R1 |
| creating |
| 'Score Card of Math for James'; |
| relate |
| James |
| to |
| Math |
| creating |
| 'Score Card of Math for James'; |
| 而不是我在以前的文章讨论的,先创建一个Score Card对象,再进行link操作。像这样: |
| // This is a wrong way!!! |
| create object instance |
| 'Score Card of Math for James' |
| of |
| 'Score Card'; |
| relate |
| James |
| to |
| Math |
| using |
| 'Score Card of Math for James'; |
| 在你不关心这个Score Card对象的时候,也可以不用creating部分。这样,一个潜在的Score Card对象会被自动创建,如果你想得到它,应该使用select语句,比如: |
| relate |
| James |
| to |
| Math; |
| // |
| select one |
| 'Score Card of Math for James' |
| that relates |
| James |
| to |
| Math |
| across |
| R1; |
| select one |
| 'Score Card of Math for James' |
| that relates |
| James |
| to |
| Math; |
| 如果我们对一个关联类进行unlink操作,则link上的关联类对象就会被自动删除。比如: |
| relate |
| James |
| to |
| Math |
| creating |
| 'Score Card of Math for James'; |
| unrelate |
| James |
| from |
| Math; // 'Score Card of Math for James' is destroyed simultaneously. |
| score = 'Score Card of Math for James'.getScore(); // Error!!! |
| 反过来,如果我们Destroy了一个关联类对象,语义上等同于拆除了其关联对象上的link。比如: |
| relate |
| James |
| to |
| Math |
| creating |
| 'Score Card of Math for James'; |
| destroy object instance |
| 'Score Card of Math for James'; |
| // After destroy, the link between James and Math has been unrelated. |
| 另外,如果Destroy了一个关联类所依附的对象,所有与这个对象link的关联类对象都会被潜在的自动删除, |
| 比如: |
| create object instance |
| James |
| of |
| Student; |
| create object instance |
| Math |
| of |
| Course; |
| create object instance |
| English |
| of |
| Course; |
| relate |
| James |
| to |
| Math |
| creating |
| 'Score Card of Math for James'; |
| relate |
| James |
| to |
| English |
| creating |
| 'Score Card of English for James'; |
| destroy object instance |
| James; // 'Score Card of Math for James' and |
| // 'Score Card of English for James' |
| // are all destoryed simultaneously |
| 'math score' = 'Score Card of Math for James'.getScore(); // Error!!! |
| 'english score' = 'Score Card of English for James'.getScore(); // Error!!! |
| 综上上述,关联类即是一个关联,又是一个类。所以对其的操作必须考虑其两方面的原因,必须搞清楚其确切的语义,才能准确地设计它和使用它。 |
| darwin_yuan |
| 发表于 |
| 2004-10-22 13:42 |
| 引用Trackback(0) |
| 编辑 |
| Comments |
| 发表评论 |
| 最近更新 |
| Delegate Class |
| Package vs. Namespace |
| Function Signature:新思维 |
| Visibility: 两种观点 |
| Java在Interface方面的缺陷 |
| 也谈对象的生命 |
| Unlink操作 |
| 关联类上的操作 |
| Link操作(三) |
| Link操作(二) |