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

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

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

2.3.4 识别公共接口

我们已经收集到了用户信息、对象行为和环境信息,接下来需要为每个用户对象指定公共接口。所以,想象一下会如何使用出租车对象:

·上车

·告诉司机你想去哪里

·付车费

·给司机小费

·下车

你需要做什么才能使用出租车对象?

·知道去哪里

·租用出租车

·付给司机钱

刚开始,你只需考虑如何使用这个对象,不用思考如何构建这个对象。稍后你可能会发现对象需要更多的接口,比如“把行李放到后备厢中”或者“和司机进行随意交谈”。图2.7提供了一个类图来列出Cabbie类的可能的方法。

图2.7 Cabbie类中的方法

得到最终的接口始终是一个迭代的过程。你必须确定每个接口是否有助于对象的操作。如果不是,那么它可能没必要存在。很多面向对象的文章推荐每个接口模型只包含一个行为。这带给我们的问题是设计究竟要抽象到哪种层次。如果有一个叫作enterTaxi()的接口,我们肯定不想enterTaxi()包含付费的逻辑。这样做不仅设计上毫无道理,而且事实上该类的使用者会不知道到底如何付费。

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