在关联关系中,很多情况下我们的多重性并不是多对一或者一对多的,而是多对多的。

不过因为我们要考虑里面的导航性,如果直接搞的话就是需要去维护两群对象之间多对多的互指链接,这就十分繁杂且易错。那么我们怎么办呢?可以将多对多的多重性尝试拆解为两组一对多的设计。

我们可以改为上图的这种拆解方法。就是说在账户与基金之间多搞一个申购交易,这样就可以化解多对多的复杂度。一个账户底下可以记录多笔申购交易,而每一个申购交易将指定某一档基金。虽然可以重复申购同一档基金,不过每一个申购交易只能设定一档基金。

一个账户对象可以链接多个申购交易对象,而每个申购交易对象只能链接到一个基金对象。

下面我们来看一个“多对多”的例子

Account.h

 #include <cstdlib>
#include <vector>
#include "Bid.h"
using namespace std; class Account
{
public:
void setBid(Bid*);
int calcAsset();
private:
vector<Bid*> bidObj;
};

Account.cpp

 #include "Account.h"

 void Account::setBid(Bid *theBid)
{
bidObj.push_back(theBid);
} int Account::calcAsset()
{
int size,theAsset=;
size=bidObj.size();
for(int i=;i<size;i++)
theAsset=theAsset+bidObj[i]->calcAsset();
return theAsset;
}

Bid.h

 #include "Fund.h" 

 class Bid
{
public:
Bid(float);
void setFund(Fund*);
int calcAsset();
float getUnit();
private:
float unit;
Fund *fundObj;
};

Bid.cpp

 #include "Bid.h"

 Bid::Bid(float theUnit)
{
unit=theUnit;
} void Bid::setFund(Fund *theFund)
{
fundObj=theFund;
} int Bid::calcAsset()
{
return unit*fundObj->getPrice();
} float Bid::getUnit()
{
return unit;
}

Fund.h

 class Fund
{
public:
Fund(float);
float getPrice();
private:
float price;
};

Fund.cpp

 #include "Fund.h"

 Fund::Fund(float thePrice)
{
price=thePrice;
} float Fund::getPrice()
{
return price;
}

main.cpp

 #include <cstdlib>
#include <iostream>
#include "Bid.h"
#include "Account.h"
#include "Fund.h"
using namespace std; int main(int argc, char *argv[])
{
Fund *myFund;
Bid *myBid;
Account myAccount; myFund=new Fund(19.84);
myBid=new Bid();
myBid->setFund(myFund);
myAccount.setBid(myBid);
cout << "大华大华基金单位及净值: "
<< "(" << myBid->getUnit() << ")"
<< "(" << myFund->getPrice() << ")" << endl; myFund=new Fund(37.83);
myBid=new Bid();
myBid->setFund(myFund);
myAccount.setBid(myBid);
cout << "日盛上选基金单位及净值: "
<< "(" << myBid->getUnit() << ")"
<< "(" << myFund->getPrice() << ")" << endl; myBid=new Bid();
myBid->setFund(myFund);
myAccount.setBid(myBid);
cout << "日盛上选基金单位及净值: "
<< "(" << myBid->getUnit() << ")"
<< "(" << myFund->getPrice() << ")" << endl << endl; cout << "总资产为: "
<< myAccount.calcAsset() << endl << endl; system("PAUSE");
return EXIT_SUCCESS;
}

下面我们来画一下UML图,并且用UML自动生成C++代码来做一个比较

生成代码对比

Account.h

达到预期

Bid.h

达到预期

Fund.h

达到预期

UML类图详解_关联关系_多对多的更多相关文章

  1. UML类图详解_关联关系_一对多

    对于一对多的示例,可以想象一个账户可以多次申购.在申购的时候没有固定上限,下限为0,那么就可以使用容器类(container class)来搞,最常见的就是vector了. 下面我们来看一个“一对多” ...

  2. UML类图详解_关联关系_多对一

    首先先来明确一个概念,即多重性.什么是多重性呢?多重性是指两个对象之间的链接数目,表示法是“下限...上限”,最小数据为零(0),最大数目为没有设限(*),如果仅标示一个数目级上下限相同. 实际在UM ...

  3. UML类图详解_泛化关系

    泛化其实就是继承关系,还是比较简单的,那么我们就把之前有些问题的博客UML类图重新来实现一次. 依旧是这个图 下面我们来看一个例子 Account.h #include <cstdlib> ...

  4. UML类图详解

    下面是类图的实例: (注:飞翔接口那里应为空心三角形) UML中类图实例 接口:空心圆+直线(唐老鸭类实现了‘讲人话’):依赖:虚线+箭头(动物和空气的关系):关联:实线+箭头(企鹅需要知道气候才迁移 ...

  5. UML 类图详解

    转载来源:http://blog.csdn.net/shift_wwx/article/details/79205187 可以参考:http://www.uml.org.cn/oobject/2012 ...

  6. UML类图详解和示例

    ps:博客园markdown不能自动生成列表,更好的阅读体验可访问我的个人博客http://www.isspark.com/archives/UMLDescription UML类图概述 什么是UML ...

  7. UML类图详解_组合关系

    组合关系和聚合关系有一个最大的不同,组合关系中的整体直接掌握部件的生灭,聚合关系中的整体并不具有生灭部件的权力.一旦组合中的整体不存在时,其组合部件也不能单独存在,必须同时消灭.另外,外界也不能直接与 ...

  8. UML类图详解_聚合关系

    结合UML关系,以看台和基金来介绍聚合关系 aggregation,是一种特殊的关联关系,既有关联关系的特质,还独有“整体 —— 部分(whole —— part)”的特质. 也就是说,用之前的关联关 ...

  9. UML简单介绍—类图详解

    类图详解 阅读本文前请先阅读:UML简单介绍—类图这么看就懂了 1.泛化关系 一个动物类: /** * 动物类 */ public class Animal { public String name; ...

随机推荐

  1. Codeforces 702 D Road to Post Office

    题目描述 Vasiliy has a car and he wants to get from home to the post office. The distance which he needs ...

  2. Codeforces 311E Biologist

    Discription SmallR is a biologist. Her latest research finding is how to change the sex of dogs. In ...

  3. [Contest20180323]King

    跳蚤国王要召开第二届内阁会议,所以把所有跳蚤都召集到了会议室.所有跳蚤在会议室的圆桌前坐成了一个圈,从$1$到$n$标号,每人的面前都有一盏明灯. 就在会议就要开始的时候,国王突然发现,并不是所有的灯 ...

  4. [SourceTree]--记录Win10 安装SourceTree免注册登陆

    记录SourceTree一次安装不成功的过程及解决办法 SourceTree简介 按照官网介绍:SourceTree是一款用于Windows和Mac的免费Git客户端.简化了用户与Git存储(仓)库的 ...

  5. Spark1.4安装问题

    1)按照<大数据Spark企业级实战>第2章中的方法构建Spark集群,最后发现master可以正常启动,但是worker却都没有启动,原因是不能直接使用在slave模版文件 slaves ...

  6. 百度地图API的事件处理:覆盖物的如何阻止冒泡

    百度地图,为了让事件使用的更方便,进行一层封装 详情可以看官方的文档 http://developer.baidu.com/map/jsdevelop-5.htm 主要的修改点: 1. 使用事件代理. ...

  7. 静态html分页

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. 三星s3c24xx平台GPIO操作详解

    转:http://blog.chinaunix.net/uid-22030783-id-3391515.html 先介绍三星S3C24XX平台BSP中定义外设寄存器和GPIO的相关头文件 以linux ...

  9. WebForm页面使用Ajax

    AJAX:”Asynchronous JavaScript and XML” 中文意思:异步JavaScript和XML.指一种创建交互式网页应用的网页开发技术.AJAX并非缩写词,而是由Jesse ...

  10. jquery获取select下拉框的前一个,后一个,第一个,最后一个option对象

    $("select option:selected").next(); <select> <option value="1" selected ...