1、3.3 关系 关联(Association) 泛化(Generation) 依赖(Dependency) 精化(Refinement)3.4 关联关联是类之间的连接,是涉及此关联的那些类的对象之间的语义连接(链接) 。3.4.1 标准关联 关联Author ComputerUses图 3.16 关联Player TeamPlays on图 3.17 队员与球队之间的关联Player TeamPlays onEmploys图 3.18 两个类之间的不同关联Forward TeamPlays onGuardCenterPlays onPlays on图 3.19 多个类可以和同一个类关联 关联的多
2、重性Person CarOwnsOwned by0*1*图 3.20 关联的多重性范围:01(0 或 1) ,0*, 或 *(0 到多) ,1*(1 到多) ,511(5 到 11) 。(1, 4, 6, 8, 10, 12)StartingPlayer Team5 1教师 学生0*1图 3.21 关联的多重性 可导航关联Person CarOwns 0*图 3.22 一个可导航关联例子: 一个保险公司拥有多份保险合同,这些合同与一个或多个客户关联。 一个客户拥有多份保险合同(0 对多) ,这些合同与一个保险公司关联。 一份保险合同位于一个保险公司和一个或多个客户之间。保险合同同时与一个客户(
3、或多个客户)和一个保险公司相关联。 保险合同在一份(0 或 1)保险契约(一份书面形式的保险合同)里表达。 保险契约与保险合同关联。InsurancepolicyInsurancecompanyInsurancecontractCustomeris expressedinanexpressesanreferstohasrefers tohas011*0*11 0*图 3.23 描述保险业务的类图3.4.2 对象图Authorname: Stringage: IntegerComputername: Stringmemory: IntegerBob: Authorname = “Bob J.”a
4、ge = 32Bobs PC: Computername = “Dell 466”memory = 640* 1*Use图 3.24 类图和对象图,以及类图被例示的一个例子objectname:classname3.4.3 递归关联从一个类到其自身的关联称为递归关联(Recursive Association) 。节点*连接图 3.25 递归关联Java 实现InsurancecompanyInsurancecontractrefers tocontracts1 0*图 3.26 保险公司与保险合同的关联/Insurance_company.java filepublic class Insu
5、rance_company/* Methods */Insurance_contractVector is a specialization of the/Vector class ensuring hard typing. Vector is a standard/Java class for dynamic arrays.private Insurance_contractVector contracts; /Insurance_contract.java filepublic class Insurance_contract/* Method */private Insurance_co
6、mpany refer_to;A BA C B*11可被转换为图 3.27 关联的转换3.4.4 关联中的角色Car Persondrives *company car driver图 3.28 关联 drives 中的角色Personwifehusbandmarried to图 3.29 关联 married to 中的角色Player TeamPlays onEmployee Employer图 3.30 关联 plays on 中的角色InsurancepolicyInsurancecompanyInsurancecontractPersonis expressedinanreferst
7、oreferstohasrefers tohas011*0*11 0*insurerpolicyholderhusbandwifemarried to图 3.31 一个类在不同的关联中可以扮演不同的角色3.4.5 限定关联限定关联(Qualified Association)用于一对多或多对多关联的情况。限定符(Qualifier)指定了如何标识一对多关联或多对多关联中的多端的一个特定对象。Canvas Figure*figure id图 3.32 限定关联例子:ReservationList Reservation*1ReservationListReservationconfirmatio
8、nNumber1 1图 3.33 使用限定关联3.4.6 异或关联异或关联 xorInsurancecompanyInsurancecontractPerson Company1 0* 0*0*1* 1*图 3.34 在同一时间,一份保险合同不能具有一个同时与 Company和 Person 相连的关联InsurancecompanyInsurancecontractPerson Company1 0* 0*0*1* 1*xor图 3.35 一个异或关联StudentCommercialAcademicChoosesChoosesxor图 3.36 两个关联之间的xor约束3.4.7 有序关联
9、InsurancecontractCustomer0*1*ordered图 3.37 一个有序关联Bank Teller CustomerServes ordered图 3.38 一个有序关联3.4.8 关联类可以将类附属到关联上,这种情况下的类称为关联类(Association Class) 。Elevator controlElevatorButtonQueue*4图 3.39 一个关联类Player TeamEmployee EmployerContractGeneralManagerNegotiates图 3.40 一个关联类3.4.9 三元关联InsurancecompanyInsu
10、rancecontractPerson1 0*0*1*Insurancepolicy01保险公司保险客户图 3.41 一个三元关联3.4.10 链链:关联的实例johnDoe:Player tyrannosaurs:TeamPlays on图 3.42 链是关联的实例3.4.11 聚合聚合(Aggregation)“整体部分”关系关键词:“由组成” 、 “包含” 、 “是的部分” 。两种特殊的聚合:共享聚合(Shared Aggregation)和组合聚合(Composition Aggregation) 基本聚合Navy Warship*Contains图 3.43 聚合的一个例子 共享聚合
11、共享聚合:聚合内的部分可以是任意整体的部分。Team Person*Members*图 3.44 共享聚合的一个例子Remix Sound clips*Contains* ordered图 3.45 共享聚合的一个例子 组合聚合组合聚合拥有它的各个组成部分。整体方的多重性必须是 0 或 1(01) ,而部分方的多重性可以是任意值。有三种不同的方式用于显示组合聚合。WindowTextListboxButtonMenu*图 3.46 组合聚合的一个例子滑翔机机身 机尾 机翼1 11 1leftWing rightWing图 3.47 组合聚合的一个例子WindowTextListboxButtonMenu*Contains图 3.48 组合聚合的第二种显示方式WindowTextListboxButtonMenu*图 3.49 组合聚合的第三种显示方式Laurel andHardy MovieActorStan Laurel Oliver Hardy(a)Actor11Oliver HardyStan LaurelLaurel and Hardy Movie(b)Laurel and Hardy MovieOliver Hardy : ActorStan Laurel : Actor(c)图 3.50 一个聚合只可以有一个角色名称时的例子