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

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

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

5.7 设计时请考虑可维护性

设计有用且简明的类促进了高层次的可维护性。正如设计类时需要考虑扩展性一样,你应该还需要考虑以后的可维护性。

设计类的过程强迫你组织你的代码为(理想情况下)很多可管理的块。把代码分块比大块代码更具维护性(至少应该这么想)。促进可维护性的最好方式之一是减少代码依赖,即修改一个类中的代码不会影响(或使影响最小化)其他类。

高度耦合的类

高度依赖其他类的行为被称为高度耦合。即如果修改一个类会强迫修改另一个类,那么这两个类则可以说是高度耦合的。没有这样依赖的类则拥有低级别的耦合。可以通过Scott Ambler写的The Object Primer一书来学习该主题的更多信息。

如果一开始就正确设计类,任何修改系统的行为应当只修改对象的具体实现。应当不惜一切代价避免对公共接口的改变。对公共接口的任何更改将会导致使用该接口的所有系统引起涟漪效应。

例如,如果对Cabbie类的getName()方法做了修改,所有系统中使用了该接口的地方都需要进行修改并重新编译。找到所有的方法调用是个艰巨的任务,而且找不全的可能性非常高。

为了促进高度的可维护性,保持类的耦合度越低越好。

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