Abstraction and Program Design
| English | Chinese | Pinyin |
|---|---|---|
| abstraction | 抽象 | chōu xiàng |
| procedural abstraction | 过程抽象 | guò chéng chōu xiàng |
| method decomposition | 方法分解 | 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.
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.
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.
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.
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.
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.