<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 0, comments - 77, trackbacks - 0, articles - 356
      BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

    UML 類圖關(guān)系

    Posted on 2008-04-22 17:35 semovy 閱讀(293) 評(píng)論(0)  編輯  收藏 所屬分類: 設(shè)計(jì)模式
    UML類圖關(guān)系全面剖析
     
    2008-04-10 作者:一雨田的專欄 來(lái)源:CSDN
     

    UML的類圖關(guān)系分為: 關(guān)聯(lián)、聚合/組合、依賴、泛化(繼承)。而其中關(guān)聯(lián)又分為雙向關(guān)聯(lián)、單向關(guān)聯(lián)、自身關(guān)聯(lián);下面就讓我們一起來(lái)看看這些關(guān)系究竟是什么,以及它們的區(qū)別在哪里。

    1、關(guān)聯(lián)

    1000){this.resized=true;this.style.width=1000;}">

    雙向關(guān)聯(lián):

    C1-C2:指雙方都知道對(duì)方的存在,都可以調(diào)用對(duì)方的公共屬性和方法。

    在GOF的設(shè)計(jì)模式書上是這樣描述的:雖然在分析階段這種關(guān)系是適用的,但我們覺得它對(duì)于描述設(shè)計(jì)模式內(nèi)的類關(guān)系來(lái)說(shuō)顯得太抽象了,因?yàn)樵谠O(shè)計(jì)階段關(guān)聯(lián)關(guān)系必須被映射為對(duì)象引用或指針。對(duì)象引用本身就是有向的,更適合表達(dá)我們所討論的那種關(guān)系。所以這種關(guān)系在設(shè)計(jì)的時(shí)候比較少用到,關(guān)聯(lián)一般都是有向的。

    使用ROSE 生成的代碼是這樣的:

    1000){this.resized=true;this.style.width=1000;}" align=top>class C1 
    1000){this.resized=true;this.style.width=1000;}" align=top>1000){this.resized=true;this.style.width=1000;}" align=top>...{
    1000){this.resized=true;this.style.width=1000;}" align=top>public:
    1000){this.resized=true;this.style.width=1000;}" align=top>    C2* theC2;
    1000){this.resized=true;this.style.width=1000;}" align=top>
    1000){this.resized=true;this.style.width=1000;}" align=top>};
    1000){this.resized=true;this.style.width=1000;}" align=top>
    1000){this.resized=true;this.style.width=1000;}" align=top>class C2 
    1000){this.resized=true;this.style.width=1000;}" align=top>1000){this.resized=true;this.style.width=1000;}" align=top>...{
    1000){this.resized=true;this.style.width=1000;}" align=top>public:
    1000){this.resized=true;this.style.width=1000;}" align=top>    C1* theC1;
    1000){this.resized=true;this.style.width=1000;}" align=top>
    1000){this.resized=true;this.style.width=1000;}" align=top>};

    雙向關(guān)聯(lián)在代碼的表現(xiàn)為雙方都擁有對(duì)方的一個(gè)指針,當(dāng)然也可以是引用或者是值。

    1000){this.resized=true;this.style.width=1000;}">

    單向關(guān)聯(lián):

    C3->C4:表示相識(shí)關(guān)系,指C3知道C4,C3可以調(diào)用C4的公共屬性和方法。沒有生命期的依賴。一般是表示為一種引用。

    生成代碼如下:

    1000){this.resized=true;this.style.width=1000;}" align=top>class C3 
    1000){this.resized=true;this.style.width=1000;}" align=top>1000){this.resized=true;this.style.width=1000;}" align=top>...{
    1000){this.resized=true;this.style.width=1000;}" align=top>public:
    1000){this.resized=true;this.style.width=1000;}" align=top>    C4* theC4;
    1000){this.resized=true;this.style.width=1000;}" align=top>
    1000){this.resized=true;this.style.width=1000;}" align=top>};
    1000){this.resized=true;this.style.width=1000;}" align=top>
    1000){this.resized=true;this.style.width=1000;}" align=top>class C4 
    1000){this.resized=true;this.style.width=1000;}" align=top>1000){this.resized=true;this.style.width=1000;}" align=top>...{
    1000){this.resized=true;this.style.width=1000;}" align=top>
    1000){this.resized=true;this.style.width=1000;}" align=top>};

    單向關(guān)聯(lián)的代碼就表現(xiàn)為C3有C4的指針,而C4對(duì)C3一無(wú)所知。

    1000){this.resized=true;this.style.width=1000;}">

    自身關(guān)聯(lián)(反身關(guān)聯(lián)):

    自己引用自己,帶著一個(gè)自己的引用。

    代碼如下:

    1000){this.resized=true;this.style.width=1000;}" align=top>class C14 
    1000){this.resized=true;this.style.width=1000;}" align=top>1000){this.resized=true;this.style.width=1000;}" align=top>...{
    1000){this.resized=true;this.style.width=1000;}" align=top>public:
    1000){this.resized=true;this.style.width=1000;}" align=top>    C14* theC14;
    1000){this.resized=true;this.style.width=1000;}" align=top>
    1000){this.resized=true;this.style.width=1000;}" align=top>};

    就是在自己的內(nèi)部有著一個(gè)自身的引用。

    2、聚合/組合

    當(dāng)類之間有整體-部分關(guān)系的時(shí)候,我們就可以使用組合或者聚合。

    1000){this.resized=true;this.style.width=1000;}">

    聚合:表示C9聚合C10,但是C10可以離開C9而獨(dú)立存在(獨(dú)立存在的意思是在某個(gè)應(yīng)用的問題域中這個(gè)類的存在有意義。這句話怎么解,請(qǐng)看下面組合里的解釋)。

    代碼如下:

    1000){this.resized=true;this.style.width=1000;}" align=top>class C9 
    1000){this.resized=true;this.style.width=1000;}" align=top>1000){this.resized=true;this.style.width=1000;}" align=top>...{
    1000){this.resized=true;this.style.width=1000;}" align=top>public:
    1000){this.resized=true;this.style.width=1000;}" align=top>    C10 theC10;
    1000){this.resized=true;this.style.width=1000;}" align=top>
    1000){this.resized=true;this.style.width=1000;}" align=top>};
    1000){this.resized=true;this.style.width=1000;}" align=top>
    1000){this.resized=true;this.style.width=1000;}" align=top>class C10 
    1000){this.resized=true;this.style.width=1000;}" align=top>1000){this.resized=true;this.style.width=1000;}" align=top>...{
    1000){this.resized=true;this.style.width=1000;}" align=top>
    1000){this.resized=true;this.style.width=1000;}" align=top>};
    1000){this.resized=true;this.style.width=1000;}" align=top>

    1000){this.resized=true;this.style.width=1000;}">

    組合(也有人稱為包容):一般是實(shí)心菱形加實(shí)線箭頭表示,如上圖所示,表示的是C8被C7包容,而且C8不能離開C7而獨(dú)立存在。但這是視問題域而定的,例如在關(guān)心汽車的領(lǐng)域里,輪胎是一定要組合在汽車類中的,因?yàn)樗x開了汽車就沒有意義了。但是在賣輪胎的店鋪業(yè)務(wù)里,就算輪胎離開了汽車,它也是有意義的,這就可以用聚合了。在《敏捷開發(fā)》中還說(shuō)到,A組合B,則A需要知道B的生存周期,即可能A負(fù)責(zé)生成或者釋放B,或者A通過(guò)某種途徑知道B的生成和釋放。

    他們的代碼如下:

    1000){this.resized=true;this.style.width=1000;}" align=top>class C7 
    1000){this.resized=true;this.style.width=1000;}" align=top>1000){this.resized=true;this.style.width=1000;}" align=top>...{
    1000){this.resized=true;this.style.width=1000;}" align=top>public:
    1000){this.resized=true;this.style.width=1000;}" align=top>    C8 theC8;
    1000){this.resized=true;this.style.width=1000;}" align=top>
    1000){this.resized=true;this.style.width=1000;}" align=top>};
    1000){this.resized=true;this.style.width=1000;}" align=top>
    1000){this.resized=true;this.style.width=1000;}" align=top>class C8 
    1000){this.resized=true;this.style.width=1000;}" align=top>1000){this.resized=true;this.style.width=1000;}" align=top>...{
    1000){this.resized=true;this.style.width=1000;}" align=top>};

    可以看到,代碼和聚合是一樣的。具體如何區(qū)別,可能就只能用語(yǔ)義來(lái)區(qū)分了。

    3、依賴

    1000){this.resized=true;this.style.width=1000;}">

    依賴:

    指C5可能要用到C6的一些方法,也可以這樣說(shuō),要完成C5里的所有功能,一定要有C6的方法協(xié)助才行。C5依賴于C6的定義,一般是在C5類的頭文件中包含了C6的頭文件。ROSE對(duì)依賴關(guān)系不產(chǎn)生屬性。

    注意,要避免雙向依賴。一般來(lái)說(shuō),不應(yīng)該存在雙向依賴。

    ROSE生成的代碼如下:

    1000){this.resized=true;this.style.width=1000;}" align=top>// C5.h
    1000){this.resized=true;this.style.width=1000;}" align=top>#i nclude "C6.h"
    1000){this.resized=true;this.style.width=1000;}" align=top>
    1000){this.resized=true;this.style.width=1000;}" align=top>class C5 
    1000){this.resized=true;this.style.width=1000;}" align=top>1000){this.resized=true;this.style.width=1000;}" align=top>...{
    1000){this.resized=true;this.style.width=1000;}" align=top>
    1000){this.resized=true;this.style.width=1000;}" align=top>};
    1000){this.resized=true;this.style.width=1000;}" align=top>
    1000){this.resized=true;this.style.width=1000;}" align=top>// C6.h
    1000){this.resized=true;this.style.width=1000;}" align=top>#i nclude "C5.h"
    1000){this.resized=true;this.style.width=1000;}" align=top>
    1000){this.resized=true;this.style.width=1000;}" align=top>class C6
    1000){this.resized=true;this.style.width=1000;}" align=top>1000){this.resized=true;this.style.width=1000;}" align=top>...{
    1000){this.resized=true;this.style.width=1000;}" align=top>
    1000){this.resized=true;this.style.width=1000;}" align=top>};
    1000){this.resized=true;this.style.width=1000;}" align=top>

    雖然ROSE不生成屬性,但在形式上一般是A中的某個(gè)方法把B的對(duì)象作為參數(shù)使用(假設(shè)A依賴于B)。如下:

    1000){this.resized=true;this.style.width=1000;}" align=top>#i nclude "B.h"
    1000){this.resized=true;this.style.width=1000;}" align=top>class A
    1000){this.resized=true;this.style.width=1000;}" align=top>1000){this.resized=true;this.style.width=1000;}" align=top>...{
    1000){this.resized=true;this.style.width=1000;}" align=top>          void Func(B &b);
    1000){this.resized=true;this.style.width=1000;}" align=top>}

    那依賴和聚合\組合、關(guān)聯(lián)等有什么不同呢?

    關(guān)聯(lián)是類之間的一種關(guān)系,例如老師教學(xué)生,老公和老婆,水壺裝水等就是一種關(guān)系。這種關(guān)系是非常明顯的,在問題領(lǐng)域中通過(guò)分析直接就能得出。

    依賴是一種弱關(guān)聯(lián),只要一個(gè)類用到另一個(gè)類,但是和另一個(gè)類的關(guān)系不是太明顯的時(shí)候(可以說(shuō)是“uses”了那個(gè)類),就可以把這種關(guān)系看成是依賴,依賴也可說(shuō)是一種偶然的關(guān)系,而不是必然的關(guān)系,就是“我在某個(gè)方法中偶然用到了它,但在現(xiàn)實(shí)中我和它并沒多大關(guān)系”。例如我和錘子,我和錘子本來(lái)是沒關(guān)系的,但在有一次要釘釘子的時(shí)候,我用到了它,這就是一種依賴,依賴錘子完成釘釘子這件事情。

    組合是一種整體-部分的關(guān)系,在問題域中這種關(guān)系很明顯,直接分析就可以得出的。例如輪胎是車的一部分,樹葉是樹的一部分,手腳是身體的一部分這種的關(guān)系,非常明顯的整體-部分關(guān)系。

    上述的幾種關(guān)系(關(guān)聯(lián)、聚合/組合、依賴)在代碼中可能以指針、引用、值等的方式在另一個(gè)類中出現(xiàn),不拘于形式,但在邏輯上他們就有以上的區(qū)別。

    這里還要說(shuō)明一下,所謂的這些關(guān)系只是在某個(gè)問題域才有效,離開了這個(gè)問題域,可能這些關(guān)系就不成立了,例如可能在某個(gè)問題域中,我是一個(gè)木匠,需要拿著錘子去干活,可能整個(gè)問題的描述就是我拿著錘子怎么釘桌子,釘椅子,釘柜子;既然整個(gè)問題就是描述這個(gè),我和錘子就不僅是偶然的依賴關(guān)系了,我和錘子的關(guān)系變得非常的緊密,可能就上升為組合關(guān)系(讓我突然想起武俠小說(shuō)的劍不離身,劍亡人亡...)。這個(gè)例子可能有點(diǎn)荒謬,但也是為了說(shuō)明一個(gè)道理,就是關(guān)系和類一樣,它們都是在一個(gè)問題領(lǐng)域中才成立的,離開了這個(gè)問題域,他們可能就不復(fù)存在了。

    4、泛化(繼承)

    1000){this.resized=true;this.style.width=1000;}">

    泛化關(guān)系:如果兩個(gè)類存在泛化的關(guān)系時(shí)就使用,例如父和子,動(dòng)物和老虎,植物和花等。

    ROSE生成的代碼很簡(jiǎn)單,如下:

    1000){this.resized=true;this.style.width=1000;}" align=top>#i nclude "C11.h"
    1000){this.resized=true;this.style.width=1000;}" align=top>
    1000){this.resized=true;this.style.width=1000;}" align=top>class C12 : public C11
    1000){this.resized=true;this.style.width=1000;}" align=top>1000){this.resized=true;this.style.width=1000;}" align=top>...{
    1000){this.resized=true;this.style.width=1000;}" align=top>};

    5、這里順便提一下模板

    1000){this.resized=true;this.style.width=1000;}">

    上面的圖對(duì)應(yīng)的代碼如下:

    1000){this.resized=true;this.style.width=1000;}" align=top>template<int>
    1000){this.resized=true;this.style.width=1000;}" align=top>class C13 
    1000){this.resized=true;this.style.width=1000;}" align=top>1000){this.resized=true;this.style.width=1000;}" align=top>...{
    1000){this.resized=true;this.style.width=1000;}" align=top>};
    1000){this.resized=true;this.style.width=1000;}" align=top>

    這里再說(shuō)一下重復(fù)度,其實(shí)看完了上面的描述之后,我們應(yīng)該清楚了各個(gè)關(guān)系間的關(guān)系以及具體對(duì)應(yīng)到代碼是怎么樣的,所謂的重復(fù)度,也只不過(guò)是上面的擴(kuò)展,例如A和B有著“1對(duì)多”的重復(fù)度,那在A中就有一個(gè)列表,保存著B對(duì)象的N個(gè)引用,就是這樣而已。

    主站蜘蛛池模板: 亚洲人成免费网站| 中文字幕日本人妻久久久免费| 99在线观看免费视频| 四虎影视永久免费观看| 97久久精品亚洲中文字幕无码| 一级毛片免费观看不收费| 女人被弄到高潮的免费视频| 亚洲国产精品久久66| 一区二区三区在线观看免费| 免费看美女被靠到爽的视频| 亚洲精品人成电影网| 国产又黄又爽又大的免费视频| 四虎影视精品永久免费| 亚洲熟女www一区二区三区| 999久久久免费精品播放| 亚洲中久无码永久在线观看同| 久久亚洲AV成人无码国产电影| 黄色片在线免费观看| 亚洲第一中文字幕| 你懂得的在线观看免费视频| 免费大黄网站在线观| 亚洲中文字幕一二三四区| 久久成人国产精品免费软件| 亚洲综合一区二区精品导航| a级片免费在线播放| 色久悠悠婷婷综合在线亚洲| 一级毛片在播放免费| 亚洲国产精品一区二区第一页免 | 亚洲精品第五页中文字幕| 91国内免费在线视频| 亚洲熟妇丰满多毛XXXX| 一级特黄a免费大片| 亚洲av无码成人精品区| 特级毛片全部免费播放| 日本视频免费在线| 亚洲成a人片在线不卡一二三区 | 国产91色综合久久免费| 亚洲综合综合在线| h视频在线观看免费网站| 亚洲美女中文字幕| 亚洲最大免费视频网|