`
ocean
  • 浏览: 48747 次
  • 来自: ...
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

对association class的疑问

UML 
阅读更多

今天看association class怎么看好像都不是很明白。

摘自UML2.0 in a Nutshell

2.6.6. Association Classes

Often the relationship between two elements isn't a simple structural connection. For example, a football player may be associated with a league by virtue of being on a team. If the association between two elements is complex, you can represent the connection using an association class. An association class is an association that has a name and attributes, like a normal class. You show an association class like a regular class with a dashed line connecting it to the association it represents. Figure 2-29 shows a football player's relationships to a league.

 

Figure 2-29. Example association class

 


When translated into code, relationships with association classes often result in three classes: one for each end of the association and one for the association class itself. There may or may not be a direct link between the association ends; the implementation may require you to traverse through the association class to get to the opposite end of the link. In other words, FootballPlayer may not have a direct reference to FootballLeague but may have a reference to FootballTeam instead. FootballTeam would then have a reference to FootballLeague. How the relationships are constructed is a matter of implementation choices; however, the fundamental concept of an association class is unchanged.

按照我的理解以上例子其实看成是FootballPlayer和FootballTeam的Aggregation relationship和FootballTeam和FootballLeague的Aggregation relationship.只不过用Association Classes可以更好的表达FootballPlayer和FootballLeague的间接关系。但这两种表达方法最后的代码表达应该都是一样的。

[代码表现](我认为)

java 代码
  1. public class FootballPlayer {   
  2.   
  3.     public int name;   
  4.   
  5.     public int number;   
  6.        
  7.     FootballTeam footballTeam;   
  8. }  
java 代码
  1. public class FootballTeam {   
  2.   
  3.     public String name;   
  4.   
  5.     public int numPlayers;   
  6.   
  7.     public String homeCity;   
  8.   
  9.     Collection<FootballPlayer> footballPlayer;   
  10.   
  11.     FootballLeague footballLeague;   
  12. }  
java 代码
  1. public class FootballLeague {   
  2.   
  3.     public String name;   
  4.   
  5.     public int numTeams;   
  6.   
  7.     Collection<FootballTeam> footballTeam;   
  8. }  

但用together生成的代码和我的想法又不一样

[代码表现](together generation)

java 代码
  1. public class FootballPlayer {   
  2.   
  3.     /**  
  4.      * @model.uin <code>design:node:::hnimszew7l00y4-ezsg1t:-tdsqjuew7kty3j-7fwj74</code>  
  5.      */  
  6.     public int name;   
  7.   
  8.     /**  
  9.      * @model.uin <code>design:node:::2plt7ew7l4dei-q4hyxp:-tdsqjuew7kty3j-7fwj74</code>  
  10.      */  
  11.     public int number;   
  12.   
  13.     /**  
  14.      * @model.uin <code>design:link:::ieje0rew8ip8hf-3ho9xj:-2urp5jew7l1xaj1vaegh</code>  
  15.      */  
  16.     FootballLeague footballLeague;   
  17.   
  18.     /**  
  19.      * @model.uin <code>design:node:::-s99ai1ew7l1xajmc7cgz</code>  
  20.      */  
  21.     FootballTeam footballTeam;   
  22. }   
  23.   
  24. public class FootballTeam {   
  25.   
  26.     /**  
  27.      * @model.uin <code>design:node:::hnimszew7lak9j-67pmjw:-s99ai1ew7l1xajmc7cgz</code>  
  28.      */  
  29.     public String name;   
  30.   
  31.     /**  
  32.      * @model.uin <code>design:node:::hnimszew7lb0vy-dlg5p6:-s99ai1ew7l1xajmc7cgz</code>  
  33.      */  
  34.     public int numPlayers;   
  35.   
  36.     /**  
  37.      * @model.uin <code>design:node:::hnimszew7lbej9s8soby:-s99ai1ew7l1xajmc7cgz</code>  
  38.      */  
  39.     public String homeCity;   
  40.   
  41.     /**  
  42.      * @model.uin <code>design:link:::ieje0rew8ip4w3-eo406e:-2urp5jew7l1xaj1vaegh</code>  
  43.      */  
  44.     FootballPlayer footballPlayer;   
  45.   
  46.     /**  
  47.      * @model.uin <code>design:link:::ieje0rew8ip8hf-3ho9xj:-2urp5jew7l1xaj1vaegh</code>  
  48.      */  
  49.     FootballLeague footballLeague;   
  50. }   
  51.   
  52. public class FootballLeague {   
  53.   
  54.     /**  
  55.      * @model.uin <code>design:node:::hnimszew7l0j2az6d4si:-tdsqjuew7ku7zj-apfzse</code>  
  56.      */  
  57.     public String name;   
  58.   
  59.     /**  
  60.      * @model.uin <code>design:node:::hnimszew7l0vjz-ahe69h:-tdsqjuew7ku7zj-apfzse</code>  
  61.      */  
  62.     public int numTeams;   
  63.   
  64.     /**  
  65.      * @model.uin <code>design:link:::ieje0rew8ip4w3-eo406e:-2urp5jew7l1xaj1vaegh</code>  
  66.      */  
  67.     Collection<FootballPlayer> footballPlayer;   
  68.   
  69.     /**  
  70.      * @model.uin <code>design:node:::-s99ai1ew7l1xajmc7cgz</code>  
  71.      */  
  72.     Collection<FootballTeam> footballTeam;   
  73. }  
所以我现在我都不清楚这个association class到底是怎么用的了。假如那位有任何思路请一起讨论下。
分享到:
评论
1 楼 ocean 2006-12-28  
今天看了UML Distilled这本书的解释才好像明白了这个association class关系。
摘自(UML Distilled third)
Association Class
Association classes allow you to add attributes, operations, and other features to associations.
然后再看了下
http://www.javaworld.com.tw/jute/post/view?bid=33&id=92661&tpg=7&ppg=1&sty=1&age=0#92661网上的另一个评论。
以下是我的总结。
Association Class其实对于数据库中的多对多关系时是非常有用的。而这个association class就相当于数据库中的那个中间表的作用。

相关推荐

Global site tag (gtag.js) - Google Analytics