OCR H446, section 2.2.2 (e)
In 2.1.1 you met what abstraction is. Here it's used as a method for solving problems: deciding which details to remove or hide so that a problem becomes simple enough to solve.
Abstraction as a method
- Identify what is essential to the problem.
- Remove or hide everything else.
- Represent what's left in a form that can be processed (a variable, a structure, a model).
- Solve the simpler problem.
- Check the solution against the real problem, and add back detail if it's too simple.
Levels of abstraction
The same problem can be described at different levels of detail. Working top-down, you start with a high-level view and add detail (stepwise refinement).
Example: making a cup of tea
| Level | Description |
|---|---|
| High | Make tea |
| Medium | Boil water; put a teabag in a cup; pour water; add milk |
| Low | Fill the kettle to the line; switch on; wait until it clicks off; ... |
At the high level you don't think about the kettle. At the low level you do.
Abstraction in program design
| Technique | How it uses abstraction |
|---|---|
| Subroutines | "Call sort(list)" hides how the sorting is done |
| Variables and constants | PI stands for 3.14159... |
| Data structures | A stack or queue hides how the data is stored |
| Classes | Hide data and the internals behind public methods |
| Libraries and APIs | Offer features without the code |
| Layers | Network and operating system layers each hide the layers below |
| Models | A graph, state machine or equations stand for a real system |
| Interfaces | Show what can be done, not how |
Top-down design with abstraction
At level 1 you assume that the lower levels work. You write each level separately.
def play_game(): # level 1
board = setup()
take_turns(board)
show_winner(board)
def take_turns(board): # level 2
while not check_win(board):
move = get_move(board)
update_board(board, move)
This is readable because the detail is hidden in the subroutines.
Choosing the right level of abstraction
| Too much detail | Too little detail |
|---|---|
| Problem too big and slow | Model is too crude to be useful |
| Hard to understand | Gives wrong answers |
| Time wasted on irrelevant things | Important cases are missed |
A good abstraction has just enough detail for the purpose (see 2.1.1 (c)).
Worked example
Task: use abstraction to make planning a school timetable manageable.
- Real situation: hundreds of students, teachers, rooms, subjects, preferences, corridors, lunch queues, and so on.
- Abstract: keep only what limits who can be where and when:
- Lessons (the things to place).
- Time slots (the places to put them).
- Constraints: a teacher can't be in two places, a room holds one class, a student can't take two lessons at once.
- Remove: how far apart the rooms are, the teacher's personality, the colour of the classroom.
- Model: a constraint problem. Each lesson is a node in a graph, and an edge joins two lessons that can't be at the same time (they share a teacher, room or student). Giving each lesson a time slot so that no two joined lessons have the same slot is a graph colouring problem, which is a known problem with known solutions (2.2.2 (b)).
- Check: compare the result with reality, then add back detail such as room travel time, if it's needed.
The huge messy situation has become a small, well-defined problem that a program can solve.
Exam tip: say what you've kept and what you've removed, and how it helps solve the problem. Mention levels (top-down, stepwise refinement) and techniques that hide detail (subroutines, classes, libraries). Always relate it to the scenario.
Check yourself
How is abstraction used to solve a problem?
By identifying the essential features, removing or hiding everything else, solving the simpler problem, and checking it against the real situation.
What is stepwise refinement?
Starting with a high-level description of the problem and adding detail in stages until each part can be coded.
Give two programming techniques that use abstraction
Any two of: subroutines; classes and objects; data structures such as a stack; libraries and APIs; layers; models such as graphs.
What is the risk of too little detail in an abstraction?
The model may be too crude, give wrong answers or miss important cases.