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

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

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

5.1 对现实世界系统建模

面向对象编程的主要目标是按照类似于人们实际所想的方式对现实世界系统建模。以面向对象的方式设计类来创建这些模型。数据和行为在逻辑上是独立的实体,而不是使用结构化或自顶向下的方式。面向对象的方式把数据和行为封装到可以相互交互的对象里。我们处理问题时不会再以一系列事件或者程序来操作独立的数据文件。这种思维模式的优雅之处在于,类真正模拟了现实世界的对象,而且展示了这些对象如何与现实世界中的其他对象交互。

类之间交互的方式与现实世界对象之间的交互方式相似,比如人与其他对象的交互。因此当创建类时,你应该设计这些类来代表对象的真实行为。我们继续使用前一章节中的出租车司机的例子。Cab类和Cabbie类模拟了一个现实世界中的实体。如图5.1所示,Cab和Cabbie对象封装了各自的数据和行为,并且通过各自的公共接口进行交互。

图5.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

桑德.范.乌格特
Go程序设计语言

Go程序设计语言

艾伦A. A.多诺万, 布莱恩W. 柯尼汉

Publisher Resources

ISBN: 9787111684527