CS 112 Introduction to Programming - Zoo

Download Report

Transcript CS 112 Introduction to Programming - Zoo

CS 112 Introduction to
Programming
Design Good Classes:
Encapsulation and OOP Analysis
Yang (Richard) Yang
Computer Science Department
Yale University
308A Watson, Phone: 432-6400
Email: [email protected]
Admin
 Updated class schedule
2
Recap: Encapsulating Data
Client can see only
the external API
Client
Methods
Data/state
Client should not see
the internal state or
behaviors
3
Recap: Encapsulating Data
 Benefit
 Consistency:
so that it is impossible for others
to "reach in" and directly alter another
object's state

Flexibility: so that you can change the state
representation later without worrying about
breaking others’ code
Ask, Don't Touch
 Encapsulated data types.


Don't touch data and do whatever you want.
Instead, ask object to manipulate its data.
"Ask, don't touch."
Adele Goldberg
Former president of ACM
Co-developed Smalltalk
 Lesson. Limiting scope makes programs easier to maintain and
understand.
"principle of least privilege"
Recap: Encapsulation/How
 access modifiers enforce encapsulation


public members (data and methods: can be
accessed from anywhere
private members: can be accessed from a
method defined in the same class
 Members
without an access modifier: default
private accessibility, i.e., accessible in the same
package; otherwise, not accessible.
6
Examples: Set the Access Modifiers
 Coin
 Ball
 BankAccount
 Point
7
Class Diagram
Coin
class name
- face : int
attributes
+ flip() : void
+ isHeads() : boolean
+ toString() : String
methods
Above is a class diagram representing the Coin class.
“-” indicates private data or method
“+” indicates public data or method
8
Outline
 Admin and recap
 Defining classes
o Motivation and basic syntax
o Examples
o The encapsulation principle
 An OOP example: geomap visualization
9
Data Visualization
“ If I can't picture it, I can't
understand it. ”
— Albert Einstein
Edward Tufte. Create charts with
high data density that tell the truth.
10
Visualization based on
Geography
Classes?
Major Classes and Relationship
GeoMap
1
m
Region
m
2
Color
1
A composition
relationship
1
Polygon
An association
relationship
1
m
Point
12
Major Classes and Relationship
GeoMap
Region
…
String
Color
Region
Color
Point
Polygon
…
Point
13
Major Classes and Relationship
GeoMap
1
m
Region
m
2
Color
1
A composition
relationship
1
Polygon
An association
relationship
1
Design question:
- Immutable objects?
m
Point
14
Major Classes and Relationship
GeoMap
1
m
Region
m
2
Color
1
A composition
relationship
1
Polygon
An association
relationship
1
Design question:
- What is the basic
controller (of GeoMap)
structure?
m
Point
15
Coloring Controller Structure
 Retrieve region
Batch: retrieve list of all regions
 Specific: retrieve one specific region

 Coloring
 Map properties of a region to a color
16
Major Classes and Relationship
GeoMap
1
m
Region
m
2
Color
1
A composition
relationship
1
Polygon
An association
relationship
1
Discussion:
- Public methods (API)
of Point
m
Point
17
Major Classes and Relationship
GeoMap
1
m
Region
m
2
Color
1
A composition
relationship
1
Polygon
An association
relationship
1
Discussion:
- Public methods (API)
of Polygon
m
Point
18
Polygon
public class Polygon {
private final int N;
private final Point[] points;
// number of boundary points
// the points
// read from input stream
public Polygon(Scanner input) {
N = input.nextInt();
points = new Point[N+1];
for (int i = 0; i < N; i++) {
points[i] = new Point ( input );
}
points[N] = points[0];
}
}
…
public
public
public
public
…
void draw() { … }
void fill() { … }
boolean contains(Point p) { … }
Point centroid() { … }
Major Classes and Relationship
GeoMap
1
m
Region
m
2
Color
1
A composition
relationship
1
Polygon
An association
relationship
1
Discussion:
- Public methods (API)
of Region
m
Point
20
Region
Q: Should Region have a
method that returns its
internal Polygon?
public class Region {
private final String regionName; // name of region
private final String mapName;
private final Polygon poly;
// polygonal boundary
private Color fillColor, drawColor;
public Region(String mName, String rName, Polygon poly) {
regionName = rName;
Even though most complexity
mapName = mName;
is in Polygon, Polygon is not
this.poly = poly;
exposed. Region delegates
setDefaultColor();
tasks internally to Polygon.
}
public void setDrawColor (Color c) { drawColor = c; }
public void draw() { setDrawColor(); poly.draw (); }
public void fill() { … }
public boolean contains(Point p) {
return poly.contains(p);
}
public Point centroid() { return poly.centroid() }
…
}
Example Controllers
 GeoMap.java
 RandomColorMap.java
 ClickColorMap.java
 RedBlueMap.java
Purple America (PurpleMap.java)
 Idea. [Robert J. Vanderbei] Assign color based
on number of votes.



a1 = Rep. votes.
a2 = Other votes.
a3 = Dem. votes.
100% Rep
55% Dem, 45% Rep
100% Other
100% Dem
æ
ö
a1
a2
a3
(R, G, B) = ç
,
,
÷
è a1 + a2 + a3 a1 + a2 + a3 a1 + a2 + a3 ø
public Color getColor() {
int dem = tally.dem(), rep = tally.rep(), ind = tally.ind();
int tot = tally.dem + tally.rep + tally.ind;
return new Color((float) rep/tot, (float) ind/tot, (float) dem/tot);
}
Cartograms
 Cartogram. Area of state proportional to
number of electoral votes.
Michael Gastner, Cosma Shalizi, and Mark Newman
www-personal.umich.edu/~mejn/election
Cartograms
 Cartogram. Area of country proportional
to population.
Summary
 Encapsulation is a key problem solving
technique for large, complex problems
 A good way to learn more is to read
about designs of large-scale systems