Skip to Content
面向对象的思考过程(原书第5版)
book

面向对象的思考过程(原书第5版)

by 马特 魏斯费尔德
August 2021
Intermediate to advanced
450 pages
4h 46m
Chinese
Pearson
Content preview from 面向对象的思考过程(原书第5版)

9.3.3 同时使用联合和聚合

你可能注意到所有的例子中聚合和联合的区别通常是模糊的。这也说明大多数设计决策都是在考虑是使用联合还是聚合。

例如,桌面计算机系统例子可用来描述联合关系,但也包含了一些聚合关系。尽管计算机主机、显示器、键盘以及鼠标之间的交互是联合关系,但计算机主机本身则代表了聚合。你只看到了计算机主机,但它其实是由其他对象组成的一个复杂系统,它包括了集成电路、主板、显卡等。

一个Employee对象可能会由Address对象和Spouse对象组成。你可能认为Address对象是聚合关系(基于它是Employee对象的一部分),而Spouse对象则是联合关系。为了阐述,假设该雇员和其配偶都是同一个公司的雇员。如果雇员被解雇了,那么其配偶仍然在系统中,但破坏了联合关系。

同样在立体音响例子中,接收器、扬声器以及CD播放器是联合关系。扬声器和CD播放器自身则是其他对象(比如电源插头)聚合起来的结果。

在汽车例子中,尽管引擎、火花塞和门代表组合关系,而音响系统则代表了联合关系。现实中,汽车和桌面计算机都是聚合和联合的混合体。

没有绝对答案

事实上进行设计决策时并不一定有一个绝对正确的答案。设计并不是一门精密的科学。不过我们可以使用一些通用规则,这些规则不难使用,而且能帮助我们快速开始。

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Node与Express开发(第2版)

Node与Express开发(第2版)

Ethan Brown
计算机科学导论:跨学科方法

计算机科学导论:跨学科方法

罗伯特 塞奇威克, 凯文 韦恩
手把手教会你linux

手把手教会你linux

桑德.范.乌格特
Go程序设计语言

Go程序设计语言

艾伦A. A.多诺万, 布莱恩W. 柯尼汉

Publisher Resources

ISBN: 9787111684527