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

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

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

2.1.2 实现

实现细节对于用户是隐藏的。我们必须时刻牢记关于实现的一个目标,那就是修改实现不需要变动用户代码。可能你看起来有些困惑,但该目标是设计问题的核心所在。

好的接口

如果接口设计得当,实现的修改不需要对用户代码做任何改变。

请记住接口包含了调用方法及返回值的语法。如果没有改变接口,用户无须关心是否修改了实现。程序员只关心使用相同的语法能够获得相同的值即可。

我们可以拿手机来举例。打电话的接口很简单,我们只需拨一个号码或者从地址簿中选取一个条目。如果供应商更新了软件,它不会改变你打电话的方式。无论如何修改实现,打电话的接口始终保持不变。然而,如果我的电话区号变了,供应商也有可能会修改接口。基础接口变了(比如电话区号变了),确实需要用户改变行为。商家希望保持这样的修改最小化,因为有些用户不喜欢这种改变,或者不想忍受这样的麻烦。

再说烤面包机的例子。只要接口一直是电源插座,具体实现可能会从一个燃煤电厂切换为核电站,但这不会影响烤面包片机。这里有一个非常重要的规则,即燃煤电厂和核电厂都必须遵循接口规格。如果燃煤电厂提供交流电(AC),而核电厂提供直流电(DC),就会出问题。用户和实现都必须要遵循接口规格。

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