Abstraction and Program Design · 抽象与程序设计
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| abstraction/əbˈstrækʃn/ | 抽象 | chōu xiàng |
| procedural abstraction/prəˈsiːdʒərəl əbˈstrækʃn/ | 过程抽象 | guò chéng chōu xiàng |
| method decomposition/ˈmeθəd ˌdiːkɒmpəˈzɪʃn/ | 方法分解 | fāng fǎ fēn jiě |
Hiding the details
- Abstraction 抽象 means hiding details to focus on the main idea.
- You drive a car by the steering wheel and pedals — not by knowing the engine's internals.
- In code, abstraction lets you use something by what it does, not how.
- It's the single most important idea for managing large programs.
隐藏细节
- 抽象意味着隐藏细节以聚焦主要思想。
- 你靠方向盘和踏板开车——而不是靠了解发动机的内部。
- 在代码里,抽象让你按它做什么、而非怎么做来使用某物。
- 它是管理大型程序最重要的一个思想。
Procedural abstraction
- Procedural abstraction 过程抽象 packages a behavior into a method you call by name.
- Once written,
average(scores)is used for its result — the code inside is hidden. - The method name and parameters (its interface) are all a caller needs.
- This is exactly how you use library methods like
Math.sqrt.
过程抽象
- 过程抽象把一个行为打包进一个你按名字调用的方法。
- 一旦写好,
average(scores)就按它的结果被使用——里面的代码被隐藏。 - 方法名和参数(它的接口)是调用者所需的全部。
- 这正是你使用像
Math.sqrt这样的库方法的方式。
Method decomposition
- Break a big problem into smaller behaviors, each its own method — method decomposition 方法分解.
- "Process an order" →
checkStock(),chargeCard(),ship(). - Small, named methods are easier to write, test, and reuse.
- The main method then reads like a high-level summary.
方法分解
- 把大问题拆成更小的行为,每个是它自己的方法——方法分解。
- “处理一个订单” →
checkStock()、chargeCard()、ship()。 - 小的、有名字的方法更易于书写、测试和复用。
- 主方法于是读起来就像一份高层摘要。
Planning a class
- To design a class, decide what data it holds and what behaviors it offers.
- A
Studenthas data (name, scores) and behaviors (addScore,average). - Good design groups related data and behavior together.
- Planning first makes a program easier to build, read, and reuse.
规划一个类
- 要设计一个类,决定它持有什么数据、提供什么行为。
- 一个
Student有数据(姓名、分数)和行为(addScore、average)。 - 好的设计把相关的数据和行为组合在一起。
- 先规划让程序更易于构建、阅读和复用。
Interface or implementation? · 接口还是实现?
Abstraction hides the how behind a simple what. Sort each item. · 抽象将“如何实现”隐藏在简单的“做什么”之后。对每一项进行分类。
Abstraction is about the interface, not the implementation. A caller of average(scores) needs only its name, inputs, and result — never the loop inside. Design each method to do one clear job (method decomposition); a method that does five unrelated things is hard to name, test, and reuse. Plan a class as data + behaviors before writing code.
抽象关乎接口,而非实现。average(scores) 的调用者只需要它的名字、输入和结果——从不需要里面的循环。把每个方法设计成做一件清晰的事(方法分解);一个做五件不相关事情的方法难以命名、测试和复用。在写代码前,把一个类规划为数据 + 行为。
Designing a Student class:
- Data: the student's name and a list of scores.
- Behaviors:
addScore(s),average(),highest(). - A caller uses
student.average()for its result — the calculation is hidden inside.
设计一个 Student 类:
- **数据:**学生的姓名和一个分数列表。
- 行为:
addScore(s)、average()、highest()。 - 调用者用
student.average()取它的结果——计算被隐藏在里面。
Abstraction hides details to focus on the main idea; procedural abstraction lets you use a method by what it does, not how. Method decomposition breaks a big problem into small, named behaviors. Design a class by deciding its data and behaviors — good design makes code easier to build, read, and reuse.
抽象隐藏细节以聚焦主要思想;过程抽象让你按做什么、而非怎么做来使用一个方法。方法分解把大问题拆成小的、有名字的行为。通过决定它的数据和行为来设计一个类——好的设计让代码更易于构建、阅读和复用。
Abstraction is best described as... · 抽象最好被描述为...
Abstraction hides how something works so you can use what it does. · 抽象隐藏了某物如何工作,从而让你只关注它能做什么。
Procedural abstraction lets you use a method by... · 过程抽象让你通过...来使用一个方法:
You call it by name and inputs, not by its internals. · 你通过名称和输入来调用它,而不是通过其内部机制。
Breaking a big problem into smaller named behaviors (methods) is called method ___ (one word). · 将一个复杂问题分解为更小的命名行为(方法)称为方法___(一个单词)。
Method decomposition splits work into focused methods. · 方法分解将工作拆分为专注的方法。
When designing a class, which should you decide? · 设计类时,你应该决定什么?
Design is about data + behaviors, grouped sensibly — not addresses. · 设计是关于数据+行为的合理分组——而非地址。
Good program design makes a program easier to build, read, and reuse. · 良好的程序设计使程序更容易构建、阅读和复用。
That's the payoff of abstraction and decomposition. · 这就是抽象和分解带来的回报。