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

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

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

2.3 尽可能提供最小化的用户接口

当设计类时,通用规则是尽量不要让用户知道类内部的工作原理。为了实现这一点,请遵守以下简单规则:

·只提供给用户绝对需要的东西。实际上这意味着类的接口要尽可能少。当你开始设计一个类时,先从最小化的接口开始。类的设计是迭代式的,所以随后即使你发现最小化的接口可能不合适,也没关系。

·最好只有用户真正需要时才添加接口,不要提供超出用户需求的接口。有时对用户来说,访问某些接口可能会出问题。比如,你不该提供给所有用户关于薪水信息的接口,而应该只提供给需要的用户。

·这次我们用硬件示例来阐述。想象一个用户有一台个人电脑主机,但没有显示器和键盘。显然这台个人电脑主机用处不大。你只提供给了该用户关于该个人电脑最小化的接口。然而,该最小化的接口是不充足的,有必要立即添加接口。

·公共接口定义了用户可以访问什么。你可以刚开始设置所有接口为私有的,这样向用户隐藏了整个类,当程序员开始使用这个类时,再把某些方法公开出来,这些方法就变成了公共接口。

·从用户角度设计类而不是从信息系统的角度设计类是至关重要的。通常类(而不是软件)的设计者设计类时会使其适用一个具体的技术模型。即使该设计者站在用户的视角,它仍然可能是一个技术用户的视角。而这个类的设计往往只考虑了技术因素,而没有基于用户的视角。

·确保设计类时你向真正的用户了解了需求和设计。这些人并不局限于开发人员(包括所有级别的测试)。当构建系统原型时,类通常都会需要更新和演化。

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