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

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

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

2.1 清楚接口和实现之间的区别

正如第1章中所示,构建健壮的面向对象设计的关键之一是理解接口和实现之间的不同。因此,当设计类时,应该向用户暴露什么,隐藏什么是非常重要的。而封装与生俱来的数据隐藏机制可以对用户隐藏不必要的数据。

小心

不要混淆接口与图形化用户接口(Graphical User Interface,GUI)这两个概念。虽然GUI名称中包含了interface这个单词,但是我们所说的接口是一种更通用的术语,它并不局限于图形化接口。

还记得第1章中的烤面包机例子吗?烤面包机(或相似功能的设备)需要插入一个接口,即电源插座,如图2.1所示。所有需要电的设备都需要遵从正确的接口,即电源插座。烤面包机不需要知道插座的任何实现,或者电是如何产生的。对所有烤面包机而言,它不关心电是燃煤电厂还是核电站生产的,只关心具体接口可以正确安全地工作就行。

图2.1 再访发电厂

还有一个汽车的例子。司机和汽车之间具有很多接口,比如方向盘、油门踏板、刹车和点火开关。先抛开美观问题,大多数人开车时主要关注的点是启动、加速、停止、转向等,很少有人关心那些眼睛看不到的组件(实现)。事实上,大多数人根本就无法识别某些组件,比如催化转化器和垫圈。然而,任何司机都必须清楚如何使用方向盘,因为这是一个通用接口。制造厂商会为车安装一个标准的方向盘,确保市场上的目标客户都能够使用这个系统。

然而,如果制造厂商决定安装一个操纵杆来代替方向盘,大多数司机都会不习惯,那么这个车型销量不会很广(只能博得一些喜欢打破常规的人的喜爱)。而如果制造厂商替换了汽车的引擎(改变了部分实现),只要没有改变性能和外观,大多数司机都不会注意到。 ...

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

桑德.范.乌格特
Java并发编程实战

Java并发编程实战

Brian Goetz, Tim Peierls

Publisher Resources

ISBN: 9787111684527