Забыли?

?

# Top-Down Design

код для вставкиСкачать
```Top-Down Design
Topics
вЂў Top-Down Design
вЂў Top-Down Design Examples
вЂў The Function Concept
вЂў Sections 3.9, 3.10
CMSC 104, Version 9/01
1
Top-Down Design
вЂў If we look at a problem as a whole, it may seem
impossible to solve because it is so complex.
Examples:
o writing a tax computation program
o writing a word processor
вЂў Complex problems can be solved using topdown design, also known as stepwise
refinement, where
o We break the problem into parts
o Then break the parts into parts
o Soon, each of the parts will be easy to do
CMSC 104, Version 9/01
2
вЂў Breaking the problem into parts helps us to
clarify what needs to be done.
вЂў At each step of refinement, the new parts
become less complicated and, therefore,
easier to figure out.
вЂў Parts of the solution may turn out to be
reusable.
вЂў Breaking the problem into parts allows more
than one person to work on the solution.
CMSC 104, Version 9/01
3
An Example of Top-Down Design
вЂў Problem:
o
o
o
o
We own a home improvement company.
We do painting, roofing, and basement
waterproofing.
A section of town has recently flooded (zip
code 21222).
We want to send out pamphlets to our
customers in that area.
CMSC 104, Version 9/01
4
The Top Level
вЂў Get the customer list from a file.
вЂў Sort the list according to zip code.
вЂў Make a new file of only the customers with the zip
code 21222 from the sorted customer list.
вЂў Print an envelope for each of these customers.
Main
CMSC 104, Version 9/01
Sort
Select
Print
5
Another Level?
вЂў Should any of these steps be broken down
further? Possibly.
вЂў How do I know? Ask yourself whether or
not you could easily write the algorithm for
the step. If not, break it down again.
вЂў When you are comfortable with the
breakdown, write the pseudocode for each
of the steps (modules) in the hierarchy.
вЂў Typically, each module will be coded as a
separate function.
CMSC 104, Version 9/01
6
Structured Programs
вЂў We will use top-down design for all remaining
programming projects.
вЂў This is the standard way of writing programs.
вЂў Programs produced using this method and
using only the three kinds of control
structures, sequential, selection and
repetition, are called structured programs.
вЂў Structured programs are easier to test,
modify, and are also easier for other
programmers to understand.
CMSC 104, Version 9/01
7
Another Example
вЂў Problem: Write a program that draws this
picture of a house.
CMSC 104, Version 9/01
8
The Top Level
вЂў Draw the outline of the house
вЂў Draw the chimney
вЂў Draw the door
вЂў Draw the windows
Main
Draw
Outline
CMSC 104, Version 9/01
Draw
Chimney
Draw
Door
Draw
Windows
9
Pseudocode for Main
Call Draw Outline
Call Draw Chimney
Call Draw Door
Call Draw Windows
CMSC 104, Version 9/01
10
Observation
вЂў The door has both a frame and knob. We
could break this into two steps.
Main
Draw
Outline
Draw
Chimney
Draw
Door Frame
CMSC 104, Version 9/01
Draw
Door
Draw
Windows
Draw
Knob
11
Pseudocode for Draw Door
Call Draw Door Frame
Call Draw Knob
CMSC 104, Version 9/01
12
Another Observation
вЂў There are three windows to be drawn.
Main
Draw
Outline
. . .
Draw
Window 1
CMSC 104, Version 9/01
Draw
Windows
Draw
Window 2
Draw
Window 3
13
One Last Observation
вЂў But donвЂ™t the windows look the same? They
just have different locations.
вЂў So, we can reuse the code that draws a
window.
o
o
Simply copy the code three times and edit it to
place the window in the correct location, or
Use the code three times, вЂњsending itвЂќ the
correct location each time (we will see how to
do this later).
вЂў This is an example of code reuse.
CMSC 104, Version 9/01
14
Reusing the Window Code
Main
Draw
Outline
. . .
Draw
Windows
Draw a
Window
CMSC 104, Version 9/01
15
Pseudocode for Draw Windows
Call Draw a Window, sending in Location 1
Call Draw a Window, sending in Location 2
Call Draw a Window, sending in Location 3
CMSC 104, Version 9/01
16
```
###### Документ
Категория
Презентации
Просмотров
10
Размер файла
48 Кб
Теги
1/--страниц
Пожаловаться на содержимое документа