0% found this document useful (0 votes)
147 views25 pages

Top-Down Design: Garmin GPS Software

This document discusses top-down and bottom-up design approaches for software. Top-down design involves starting with high-level abstractions and refining the design in steps, while bottom-up design involves starting with low-level parts and combining them. Neither approach is inherently better, and alternating between the two can help uncover different perspectives. The document also discusses principles like modularity, encapsulation, and avoiding duplication to improve software design.

Uploaded by

Anthony Scott
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
147 views25 pages

Top-Down Design: Garmin GPS Software

This document discusses top-down and bottom-up design approaches for software. Top-down design involves starting with high-level abstractions and refining the design in steps, while bottom-up design involves starting with low-level parts and combining them. Neither approach is inherently better, and alternating between the two can help uncover different perspectives. The document also discusses principles like modularity, encapsulation, and avoiding duplication to improve software design.

Uploaded by

Anthony Scott
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

Top-Down Design

Garmin GPS software


User
Interface
Show Export to Find
Compare
Ridec Google Similar

Rides List One Ride Route

Track
Track
Point
Point List
Object
Refine the design at each step
Decomposition / Divide and Conquer
Not a perfect, pretty picture
Boxes at lower levels are more concrete
and contain things like GPS records, actual
strings
Boxes at higher levels are more abstract
and closer to dealing with the user
In between are worker bees that do things
like file storage and waking up Google Earth
But dont take the hierarchy too seriously
Most things dont fit perfectly into trees
Bottom-Up Design
Just the opposite: start with parts
User
Interface
Show Export to Find
Compare
Ridec Google Similar

Rides List One Ride Route

Track
Track
Point
Point List
Object
Composition
Build-It-Yourself (e.g. IKEA furniture)
Top-Down vs. Bottom-Up
Is one of these ways better? Not really!
Its
sometimes good to alternate
By coming to a problem from multiple angles
you might notice something you had previously
overlooked
Not the only ways to go about it

With Top-Down its harder to test early


because parts needed may not have been
designed yet
With Bottom-Up, you may end up needing
things different from how you built them
Software Process
For simple programs, a simple process

Waterfall

But to use this process, you need to be


sure that the requirements are fixed and
well understood!
Many software problems are not like that
Often customer refines the requirements
when you try to deliver the initial solution!
Incremental & Iterative
Deliver versions of the system in several
small cycles

Recognizes that for some settings, software


development is like gardening
You plant seeds see what does well then
replace the plants that did poorly
Modularity
Module: component of a system with a
well-defined interface. Examples:
Tiresin a car (standard size, many vendors)
Cable adaptor for TV (standard input/output)
External storage for computer
...

Modules hide information behind


their interfaces
A module isnt just an
object
Were using the term to capture what
could be one object, but will often be a
larger component constructed using many
objects

In fact Java has a module subsystem for


this reason (we wont use it in cs2110)
A module implements some abstraction
You think of the whole module as a kind of big
object
Information Hiding
What information do modules hide?
Internal design decisions.
class Set {
...

public void add(Object o) ...

public boolean contains(Object o) ...

public int size() ...


}
A classs interface is everything in it that
is externally accessible
Encapsulation
By hiding code and data behind its
interface, a class encapsulates its
inner workings
Why is that good?
Lets us change the implementation later
without invalidating the code that uses the
class{
class LineSegment class LineSegment {
private Point2D _p1, _p2; private Point2D _p;
private double _length;
... private double _phi;
public double length() {
return _p1.distance(_p2); ...
} public double length() {
} return _length;
}
}
Encapsulation
Why is that good? (continued)
Sometimes, we want a few different classes
to implement some shared functionality
For example, recall the iterator construct
we saw in connection with collections:
Iterator it = collection.iterator();

while (it.hasNext()) { for (String s: args) {


Object next = it.next(); System.out.println(Argument +s);
doSomething(next); }
}

To support iteration, a class simply needs


to implement the Iterable interface
Degenerate Interfaces
Public fields are usually a Bad Thing:
class Set {
public int _count = 0;

public void add(Object o) ...

public boolean contains(Object o) ...

public int size() ...


}

Anybody can change them; the class


has no control
Interfaces vs.
Implementations
This says I need this specific implementation:

public void doSomething(LinkedList items) ...


This says I can operate on anything that
supports the Iterable interface

public void doSomething(Iterable items) ...


Interfaces represent higher levels of
abstraction
(they focus on what and leave out the how)
Principle of Least
Astonishment
Have your designs work how a user
would
Bad:
expect
public int product(int a, int b) {
return a*b > 0 ? a*b : -a*b;
}

Better:
public int absProduct(int a, int b) {
return a*b > 0 ? a*b : -a*b;
}

Names and comments matter!


Principle of Least
Astonishment
Unexpected side effects are a Bad
Thing
class Integer {
private int _value;
...
public Integer times(int factor) {
_value *= factor;
return new Integer(_value);
} Developer was trying to
} be clever. But what
...
does this code do to i?
Integer i = new Integer(100);
Integer j = i.times(10);
Duplication
It is very common to find some chunk of
working code, make a replica, and then edit the
replica
But this makes your software fragile: later, when
the code you copied needs to be revised, either
The person doing that changes all instances, or
some become inconsistent

Duplication can arise in many ways:


constants (repeated magic numbers)
code vs. comment
within an objects state
...
Duplication in Comments
public double totalArea() {
public double totalArea() { ...
... area += circleArea(radius);
// now add the circle ...
area += PI * pow(radius,2); }
...
} private double circleArea(double radius) {
return PI * pow(radius, 2);
}

Some people use comments when they


should be creating a new method to do the
thing the comment says you are doing!
If the thing you are doing captures some idea
(here, it involves the formula for the area of a
circle), create a method and use that
The code itself becomes self documenting
Duplication of State
Duplication of state can lead to
inconsistency within an object:
class LineSegment { class LineSegment {
private Point2D _p1, _p2; private Point2D _p1, _p2;
private double _length;

... ...
public double length() { public double length() {
return _length; return _p1.distance(_p2);
} }
} }
Can you see how this code could evolve
later and accidentally become inconsistent?
Duplication may be desirable for
performance, but dont optimize too soon
DRY Principle
Dont Repeat Yourself

A nice goal is to to have each piece of


knowledge live in one place
But dont go crazy over it
DRYing up at any cost can increase
dependencies between code
3 strikes and you refactor (i.e., clean
up)
Refactoring
Refactor: to improve codes internal structure
without changing its external behavior
Most of the time were modifying existing
software
Improving the design after it has been written
Refactoring steps can be very simple:
public double weight(double mass) { static final double GRAVITY = 9.80665;
return mass * 9.80665;
} public double weight(double mass) {
return mass * GRAVITY;
}

Other examples: renaming variables, methods,


classes
Why is refactoring good?

If your application later gets used as part


of a Nasa mission to Mars, it wont make
mistakes
Every place that the gravitational
constant shows up in your program a
reader will realize that this is what she is
looking at
The compiler may actually produce better
code
Extract Method
A comment explaining what is being
done usually indicates the need to
extract a method
public double totalArea() { public double totalArea() {
... ...
// now add the circle area += circleArea(radius);
area += PI * pow(radius,2); ...
... }
}
private double circleArea(double radius) {
return PI * pow(radius, 2);
}

One of the most common refactorings


Extract Method
Simplifying conditionals with Extract
Method
before
if (date.before(SUMMER_START) || date.after(SUMMER_END)) {
charge = quantity * _winterRate + _winterServiceCharge;
}
else {
charge = quantity * _summerRate;
}

after
if (isSummer(date)) {
charge = summerCharge(quantity);
}
else {
charge = winterCharge(quantity);
}
Refactoring & Tests
Eclipse supports various refactorings

You can refactor manually


Automated tests are essential
to ensure external behavior
doesnt change
Dont refactor manually without
retesting to make sure you didnt
break the code you were improving!

More about tests and how to drive


development with tests next week
Back to the future
All of this leads back to
solving problems like our
bike-ride comparisons
In fact well solve a similar problem for
homework!
Animals with DNA containing genes
Comparing genes is like comparing bike routes
Comparing animals is like finding people who rode
similar sets of bike routes
But solution will need some tricks of the
trade that well cover in the next few weeks of
classes

You might also like