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

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

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

7.2.2 设计决策

理论上来说找到越多的共同点越好。然而有时过于精确也会存在设计问题。尽管找出尽可能多的共同点会更加接近真实生活,然而并不表示更靠近模型。找出的点越多,系统会越复杂。所以你会陷入一个难题,到底想要更加精确的模型还是复杂度更低的系统?你需要根据情况进行决策,有一些简单的指导性建议可以帮助你做决策。

计算机不擅长的事

显而易见,计算机模型只能近似描述真实世界的情况。计算机善于捣鼓数字,但不善于抽象操作。

例如,将Dog类分解为BarkingDog和YodelingDog比假设所有的狗都会吠叫更接近现实世界模型,但同时也增加了复杂度。

模型复杂度

本例中多加两个类不会复杂到无法驾驭模型。但在大型的系统中,如果不停地做这样的决策,那么复杂度会急剧上升。在大型系统中,尽可能保持简单往往是最佳实践。

可以通过实例来讲解过于精确的模型是无法维持较低复杂度的。假设你作为动物饲养员想构造一个系统来记录所有的狗。该系统模型包括了会叫的狗和不会叫的狗,看起来还可以。然而假设你没有饲养任何不会叫的狗,以前没养过,以后也不会养。那么你可能就无须引入复杂度来区别会叫的狗和不会叫的狗。这会减少系统复杂度,并且依然能满足功能需求。

决定在设计时引入更小的复杂度或者更多的功能是一项平衡艺术。我们的主要目标始终是创建一个系统,该系统保持一定的弹性,引入复杂度时不至于被自身压垮。如果在项目的后期,你又需要增加不叫的狗该怎么办?

当前还有未来的开销因素也是决策的主要因素。尽管新加功能让系统更加完整和灵活,可能勉强带来了一点好处,但投资回报率却很低。例如,假设需要扩展Dog系统,引入其他的犬科动物,比如鬣狗和狐狸会怎么样?(如图7.5)

如果你是动物园主,那么这样的设计是精巧的,但如果你只是饲养或售卖宠物狗,那么Canine类的扩展是没有必要的。 ...

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