UML類圖詳解_關聯關系_多對一


首先先來明確一個概念,即多重性。什么是多重性呢?多重性是指兩個對象之間的鏈接數目,表示法是“下限...上限”,最小數據為零(0),最大數目為沒有設限(*),如果僅標示一個數目級上下限相同。

實際在UML中是可以隱藏上圖中申購交易的細節

導航性(navigation):關聯關系的細節信息通常放置於兩關聯端,像是關聯端標示箭頭,代表該關聯端具有可導航性。

下面我們來看一個“多對一”的例子

Fund.h

 1 class Fund
 2 { 
 3 public:
 4     void setPrice(float);
 5     float getPrice();
 6     void setFee(float);
 7     float getFee();
 8 private:
 9     float price;
10     float fee;
11 };

Fund.cpp

 1 #include "Fund.h"
 2 
 3 void Fund::setPrice(float thePrice)
 4 {
 5     price=thePrice;
 6 }
 7 
 8 float Fund::getPrice()
 9 {
10     return price;
11 }
12 
13 void Fund::setFee(float theFee)
14 {
15     fee=theFee;
16 }
17 
18 float Fund::getFee()
19 {
20     return fee;
21 }

Bid.h

 1 #include "Fund.h"
 2  
 3 class Bid 
 4 {
 5 public:
 6     float calcUnit();
 7     float calcFee();   
 8     void setFund(Fund*);
 9     void setAmount(int);
10     int getAmount();
11 private:
12     int amount;
13     Fund *fundObj; 
14 };

聲明一個基金對象指針,此即為多重性為1的實現方法之一。然后類Bid中的函數setFund設計了一個公有操作,讓外界可以傳入基金對象的指針,也就建立起申購交易對象指向基金對象的鏈接了。

Bid.cpp

 1 #include "Bid.h"
 2 
 3 float Bid::calcUnit()
 4 {
 5       float thePrice, theUnit;
 6       thePrice=fundObj->getPrice();
 7       theUnit=amount/thePrice;
 8       return theUnit;
 9 } 
10 
11 float Bid::calcFee()
12 {
13       float theFundFee, theFee;
14       theFundFee=fundObj->getFee();
15       theFee=amount*theFundFee;
16       return theFee;
17 }
18 
19 void Bid::setFund(Fund *theFund)
20 {
21     fundObj=theFund;
22 }
23 
24 void Bid::setAmount(int theAmount)
25 {
26     amount=theAmount;
27 }
28 
29 int Bid::getAmount()
30 {
31     return amount;
32 }

main.cpp

 1 #include <cstdlib>
 2 #include <iostream>
 3 #include "Fund.h"
 4 #include "Bid.h"
 5 using namespace std;
 6 
 7 int main(int argc, char *argv[])
 8 {
 9     Fund myFund;
10     float thePrice, theFee;
11     Bid myBid; 
12     int theAmount;
13     
14     cout << "請輸入大華大華基金凈值: ";
15     cin >> thePrice;
16     myFund.setPrice(thePrice);
17     cout << "請輸入大華大華基金手續費: ";
18     cin >> theFee;
19     myFund.setFee(theFee);
20      
21     cout << "請輸入申購金額: ";
22     cin >> theAmount;
23     myBid.setAmount(theAmount);
24     myBid.setFund(&myFund);
25 
26     cout << "您申購的單位及手續費為: " 
27          << "(" << myBid.calcUnit() << ")"
28          << "(" << myBid.calcFee() << ")" << endl << endl;
29 
30     system("PAUSE");
31     return EXIT_SUCCESS;
32 }

通過調用myBid.setFund(&myFund)將基金對象的指針傳給申購交易對象,於是就建立起申購交易對象指向基金對象的鏈接。

下面我們來畫一下UML圖,並且用UML自動生成C++代碼來做一個比較

畫法一:

生成代碼對比

Bid.h

達到預期

Fund.h

達到預期

 

畫法二:

生成代碼對比

Bid.h

沒有達到預期,重復生成了成員變量

Fund.h

達到預期

 

畫法三:

生成代碼對比

Bid.h

達到預期

Fund.h

達到預期

綜上所述,在實際畫圖的時候采用畫法一或者畫法三都可以,個人還是覺得畫法一好一些   (update 2017-10-29)其實應該是使用畫法一,這樣可以避免錯誤,具體錯誤的例子可以見UML類圖詳解_關聯關系_一對多,一旦有了比較復雜的類型,那么生成的代碼就不靠譜了。一旦類圖里面包含了一次成員那么在關聯端點再聲明一次的話就會重復,另外如果不在類圖里面包含一次成員而在關聯端點處聲明一次的話生成的代碼比較傻,很多情況下無法滿足我們的要求。所以我們就是把成員都在類圖里面包含進去,關聯端點處就是聲明一下多重性,而不要再聲明成員就可以了。 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM