Department of Computer Science
University of Toronto
Decomposition
Lecture 5:
Decomposition and Abstraction
➜
➜
Decomposition
➜
When to decompose
Large problems can be tackled with “divide and
conquer”
Decompose the problem so that:
Each subproblem is at (roughly) the same level of detail
Each subproblem can be solved independently
The solutions to the subproblems can be combined to solve the original
problem
Identifying components
Modelling components
➜
Department of Computer Science
University of Toronto
Abstraction
Abstraction by parameterization
➜
Abstraction by specification
Advantages
Different people can work on different subproblems
Parallelization may be possible
Maintenance is easier
Pre-conditions and Post-conditions
➜
Disadvantages
the solutions to the subproblems might not combine to solve the original
problem
Poorly understood problems are hard to decompose
© 2001, Steve Easterbrook
1
© 2001, Steve Easterbrook
Department of Computer Science
University of Toronto
2
Decomposition Examples
➜
How to decompose
Decomposition can work well:
➜
E.g. designing a restaurant menu
Design entrees menu
Design desserts menu
coupling - a measure of inter-component connectivity
cohesion - a measure of how well the contents of a component go together
Assemble
and edit
information hiding
having modules keep their data private
provide limited access procedures
this reduces coupling
Design drinks menu
➜
Decomposition doesn’t always work
E.g. writing a play:
Choose a set of
character parts
Step 1: Identify components
a good decomposition minimizes dependencies between components
Design appetizers menu
Choose style
and theme
write character 1’s part
module a
write character 2’s part
write character 3’s part
Private
data
Private
data
for very complex problems (e.g. Managing the economy)
for impossible problems (e.g. Turning water into wine)
for atomic problems (e.g. Adding 1 and 1)
module b
[a, c, fg, e];
[df, 4, rt, 5];
[qw, 1, t, 6]
[c, fg, 8, 1];
“Lksdfkiroer
erte;roifgkd
peoritlkgpeo
werp;tlkpoig
rtmnkm;km”
Decomposition isn’t always possible
© 2001, Steve Easterbrook
.x=?
merge
…etc…
➜
Department of Computer Science
University of Toronto
“42!”
3
© 2001, Steve Easterbrook
4
Department of Computer Science
University of Toronto
Department of Computer Science
University of Toronto
How to decompose (cont.)
➜
Abstraction
Step 2: Model the components
➜
At the design level
➜
object diagrams
structure charts
Abstraction is the main tool used in reasoning about
software
Why? It allows you to:
ignore inconvenient detail
dataflow diagrams
treat different entities as though they are the same
simplify many types of analysis
➜
Example abstractions
graph
At the coding level
procedure specifications
float sqrt(int x){
/* requires: x is a positive integer
effects: returns an approximation
of the square root of x to within
±10-4 */
procedure declarations
float sqrt(int);
© 2001, Steve Easterbrook
directed graph
tree
5
B
found
found == false;
false;
ii == highbound(a);
highbound(a);
while
while (i
(i >> lowbound(a)-1){
lowbound(a)-1){
if
if (a[i]
(a[i] ==
== e)
e) {{
zz == i;
i;
found
found == TRUE;
TRUE;
}}
ii == ii -- 1;
1;
}}
...
...
A program that takes an
integer and a list returns
the index of the first
occurrence of the element
or null if the element does
not occur in the list
A sequence of
bits on a disk
...
6
Department of Computer Science
Using Abstraction
Can I replace A with B?
found
found == false;
false;
ii == lowbound(a);
lowbound(a);
while
while (i
(i << highbound(a)+1){
highbound(a)+1){
if
if (a[i]
(a[i] ==
== e)
e) {{
zz == i;
i;
found
found == TRUE;
TRUE;
}}
ii == ii ++ 1;
1;
}}
undirected
graph
University of Toronto
example
A
A file
© 2001, Steve Easterbrook
Department of Computer Science
University of Toronto
DAG
set
membership
➜
Abstraction can help with Decomposition
e.g. To manage the economy, try focussing on some abstracted features
such as inflation, growth, GDP, etc.
Abstraction allows us to ignore inconvenient details
➜
In programming:
Abstraction is the process of naming compound objects and dealing with
them as single entities
(i.e. ignoring their details)
➜
Abstraction doesn’t solve problems…
…but it allows us to simplify them
if we could abstract away all the detail…
© 2001, Steve Easterbrook
7
© 2001, Steve Easterbrook
8
University of Toronto
Department of Computer Science
Abstraction by Parameterization
➜
➜
Abstraction by Specification
The program fragment:
➜
Abstraction by parameterization…
x * x - y * y
…allows us to express infinitely many computations
computes the difference of the squares of two specific variables, x and y.
…but does not tell us about the intention of those computations
The abstraction:
➜
We need to capture the intention
e.g. consider what is true before and after a computation
int squares (int x, int y) {
before
unsorted
array
return(x * x - y * y);
}
describes a set of computations which act on any two (integer) variables to
compute the difference of their squares
The specific computation:
uses the abstraction ‘squares’ on two specific variables (‘big’ and ‘small’)
University of Toronto
9
Department of Computer Science
© 2001, Steve Easterbrook
The two forms of abstraction are complementary
➜
specification allows us to ignore how it is done
➜
Decomposition allows us to simplify difficult design
tasks
A good decomposition
minimizes coupling between components
Unfortunately…
maximizes cohesion within components
only abstraction by parameterization is built into our programming languages
permits information hiding
as function (procedure) definitions
We can overcome this using comments:
➜
Methods provide…
… techniques for decomposing problems
int
int strlen
strlen (char
(char s[])
s[]) {{
/*
/* precondition:
precondition: ss must
must contain
contain aa character
character array,
array,
delimited
delimited by
by the
the null
null character;
character;
postcondition:
postcondition: returns
returns the
the length
length of
of ss as
as an
an integer;
integer;
*/
*/
int
length
=
0;
int length = 0;
while
while (s[length])
(s[length])
length++;
length++;
return(length);
return(length); }}
© 2001, Steve Easterbrook
Department of Computer Science
Summary
parameterization allows us to perform a computation on any arbitrary
variables (values)
➜
10
University of Toronto
Pre-conditions and Post-conditions
➜
after
sorted
array
specification
this function can be used whenever we have an
array. After it is applied, the array will be
sorted into ascending order
result = squares(big, small);
© 2001, Steve Easterbrook
function for
sorting arrays
we can abstract away from a computation (or a plan, program, function,
etc) by talking about what it achieves
Note: locally the variables are called x and y for convenience
➜
Department of Computer Science
University of Toronto
… notations for describing the components
➜
Abstraction allows us to ignore detail
by parameterization: allows us to describe and name sets of computations
by specification: allows us to ignore how the computation is done
11
© 2001, Steve Easterbrook
12
Department of Computer Science
University of Toronto
References
van Vliet, H. “Software Engineering: Principles and Practice (2nd Edition)”
Wiley, 1999.
Chapter 11 provides an introduction to the concepts in this lecture, especially section 11.1. However,
van Vliet does not go into much detail about documenting procedural and data abstractions in the
style I use in this and the next two lectures. For this you’ll need:
Liskov, B. and Guttag, J., “Program Development in Java: Abstraction,
Specification and Object-Oriented Design”, 2000, Addison-Wesley.
See especially chapters 1 and 3. I draw on Liskov’s ideas extensively for advice on program design in
this course. The commenting style I use (“requires”, “effects”, etc) is Liskov’s. If you plan to do any
extensive programming in Java, you should buy this book. If you don’t buy it, borrow it and read the
first few chapters.
© 2001, Steve Easterbrook
13
© Copyright 2026 Paperzz