首先先来明确一个概念,即多重性。什么是多重性呢?多重性是指两个对象之间的链接数目,表示法是“下限...上限”,最小数据为零(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类图详解_关联关系_一对多,一旦有了比较复杂的类型,那么生成的代码就不靠谱了。一旦类图里面包含了一次成员那么在关联端点再声明一次的话就会重复,另外如果不在类图里面包含一次成员而在关联端点处声明一次的话生成的代码比较傻,很多情况下无法满足我们的要求。所以我们就是把成员都在类图里面包含进去,关联端点处就是声明一下多重性,而不要再声明成员就可以了。
原文链接: https://www.cnblogs.com/abc-begin/p/7749579.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/262247
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!