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

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

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

5.6.2 抽象不可移植的代码

如果你在设计一个系统,必须使用不可移植的(原生的)代码(即代码只能运行在特定的硬件平台上),你应该把这些代码从类中抽象出来。通过抽象,可以将这些不可移植的代码隔离到各自的类中,或至少隔离在方法中(一个可以被覆盖的方法)。例如,如果编写代码来访问特定硬件的串行端口,你应该创建一个包装类来处理它。然后,你的类应该向包装类发送一条消息,以获取它所需的信息或服务。不要在主类中包含依赖系统的代码(请见图5.5)。

图5.5 串行端口包装器

例如,考虑程序员需要直接与硬件交互的场景。这种情况下,不同平台的对象代码应该是完全不同的,因为这些代码必须针对每个平台进行编写。然而,如果这个功能是放置在一个包装类中,然后该类的用户可以通过接口直接与该类交互而不需要担心各种各样的低层码。该包装类会处理平台的不同之处并调用相应的代码。

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