Top Banner
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]
26

CS 112 Introduction to Programming Design Good Classes: Encapsulation and OOP Analysis Yang (Richard) Yang Computer Science Department Yale University.

Dec 13, 2015

Download

Documents

Lenard McDonald
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS 112 Introduction to Programming Design Good Classes: Encapsulation and OOP Analysis Yang (Richard) Yang Computer Science Department Yale University.

CS 112 Introduction to Programming

Design Good Classes: Encapsulation and OOP Analysis

Yang (Richard) YangComputer Science Department

Yale University308A Watson, Phone: 432-6400

Email: [email protected]

Page 2: CS 112 Introduction to Programming Design Good Classes: Encapsulation and OOP Analysis Yang (Richard) Yang Computer Science Department Yale University.

2

Admin

Updated class schedule

Page 3: CS 112 Introduction to Programming Design Good Classes: Encapsulation and OOP Analysis Yang (Richard) Yang Computer Science Department Yale University.

3

Recap: Encapsulating Data

ClientClient Methods

Data/stateClient should not see the internal state or

behaviors

Client can see only the external API

Page 4: CS 112 Introduction to Programming Design Good Classes: Encapsulation and OOP Analysis Yang (Richard) Yang Computer Science Department Yale University.

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

Page 5: CS 112 Introduction to Programming Design Good Classes: Encapsulation and OOP Analysis Yang (Richard) Yang Computer Science Department Yale University.

Encapsulated data types. Don't touch data and do whatever you want. Instead, ask object to manipulate its data.

Lesson. Limiting scope makes programs easier to maintain and understand.

Adele GoldbergFormer president of ACMCo-developed Smalltalk

Ask, Don't Touch

"principle of least privilege"

"Ask, don't touch."

Page 6: CS 112 Introduction to Programming Design Good Classes: Encapsulation and OOP Analysis Yang (Richard) Yang Computer Science Department Yale University.

6

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.

Page 7: CS 112 Introduction to Programming Design Good Classes: Encapsulation and OOP Analysis Yang (Richard) Yang Computer Science Department Yale University.

7

Examples: Set the Access Modifiers

Coin

Ball

BankAccount

Point

Page 8: CS 112 Introduction to Programming Design Good Classes: Encapsulation and OOP Analysis Yang (Richard) Yang Computer Science Department Yale University.

8

Class Diagram

Coin

- face : int

+ flip() : void+ isHeads() : boolean+ toString() : String

Above is a class diagram representing the Coin class.“-” indicates private data or method“+” indicates public data or method

class nameclass name

attributesattributes

methodsmethods

Page 9: CS 112 Introduction to Programming Design Good Classes: Encapsulation and OOP Analysis Yang (Richard) Yang Computer Science Department Yale University.

9

Outline

Admin and recap Defining classes

o Motivation and basic syntaxo Exampleso The encapsulation principle An OOP example: geomap visualization

Page 10: CS 112 Introduction to Programming Design Good Classes: Encapsulation and OOP Analysis Yang (Richard) Yang Computer Science Department Yale University.

Data Visualization

“ If I can't picture it, I can't understand it. ” — Albert Einstein

10

Edward Tufte. Create charts with high data density that tell the truth.

Page 11: CS 112 Introduction to Programming Design Good Classes: Encapsulation and OOP Analysis Yang (Richard) Yang Computer Science Department Yale University.

Visualization based on Geography

Classes?

Page 12: CS 112 Introduction to Programming Design Good Classes: Encapsulation and OOP Analysis Yang (Richard) Yang Computer Science Department Yale University.

12

Major Classes and Relationship

GeoMap Region

Polygon

Color

Point

1 m m 2

1

1

1

m

A composition relationship An association

relationship

Page 13: CS 112 Introduction to Programming Design Good Classes: Encapsulation and OOP Analysis Yang (Richard) Yang Computer Science Department Yale University.

13

Major Classes and Relationship

GeoMap

Region Region

String

PolygonColorColor

Point … Point

Page 14: CS 112 Introduction to Programming Design Good Classes: Encapsulation and OOP Analysis Yang (Richard) Yang Computer Science Department Yale University.

14

Major Classes and Relationship

GeoMap Region

Polygon

Color

Point

1 m m 2

1

1

1

m

A composition relationship An association

relationship

Design question:- Immutable objects?

Page 15: CS 112 Introduction to Programming Design Good Classes: Encapsulation and OOP Analysis Yang (Richard) Yang Computer Science Department Yale University.

15

Major Classes and Relationship

GeoMap Region

Polygon

Color

Point

1 m m 2

1

1

1

m

A composition relationship An association

relationship

Design question:- What is the basic controller (of GeoMap) structure?

Page 16: CS 112 Introduction to Programming Design Good Classes: Encapsulation and OOP Analysis Yang (Richard) Yang Computer Science Department Yale University.

Retrieve region Batch: retrieve list of all regions Specific: retrieve one specific region

Coloring Map properties of a region to a color

16

Coloring Controller Structure

Page 17: CS 112 Introduction to Programming Design Good Classes: Encapsulation and OOP Analysis Yang (Richard) Yang Computer Science Department Yale University.

17

Major Classes and Relationship

GeoMap Region

Polygon

Color

Point

1 m m 2

1

1

1

m

A composition relationship An association

relationship

Discussion:-Public methods (API) of Point

Page 18: CS 112 Introduction to Programming Design Good Classes: Encapsulation and OOP Analysis Yang (Richard) Yang Computer Science Department Yale University.

18

Major Classes and Relationship

GeoMap Region

Polygon

Color

Point

1 m m 2

1

1

1

m

A composition relationship An association

relationship

Discussion:-Public methods (API) of Polygon

Page 19: CS 112 Introduction to Programming Design Good Classes: Encapsulation and OOP Analysis Yang (Richard) Yang Computer Science Department Yale University.

Polygon

public class Polygon { private final int N; // number of boundary points private final Point[] 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 void draw() { … } public void fill() { … } public boolean contains(Point p) { … } public Point centroid() { … } …}

Page 20: CS 112 Introduction to Programming Design Good Classes: Encapsulation and OOP Analysis Yang (Richard) Yang Computer Science Department Yale University.

20

Major Classes and Relationship

GeoMap Region

Polygon

Color

Point

1 m m 2

1

1

1

m

A composition relationship An association

relationship

Discussion:-Public methods (API) of Region

Page 21: CS 112 Introduction to Programming Design Good Classes: Encapsulation and OOP Analysis Yang (Richard) Yang Computer Science Department Yale University.

Region

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; mapName = mName; this.poly = poly; setDefaultColor(); } 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() } …}

Q: Should Region have a method that returns its internal Polygon?

Even though most complexity is in Polygon, Polygon is not exposed. Region delegates tasks internally to Polygon.

Page 22: CS 112 Introduction to Programming Design Good Classes: Encapsulation and OOP Analysis Yang (Richard) Yang Computer Science Department Yale University.

Example Controllers

GeoMap.java RandomColorMap.java ClickColorMap.java RedBlueMap.java

Page 23: CS 112 Introduction to Programming Design Good Classes: Encapsulation and OOP Analysis Yang (Richard) Yang Computer Science Department Yale University.

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% Dem

100% Rep

100% Other

55% Dem, 45% Rep

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);}

Page 24: CS 112 Introduction to Programming Design Good Classes: Encapsulation and OOP Analysis Yang (Richard) Yang Computer Science Department Yale University.

Cartograms

Cartogram. Area of state proportional to number of electoral votes.

Michael Gastner, Cosma Shalizi, and Mark Newmanwww-personal.umich.edu/~mejn/election

Page 25: CS 112 Introduction to Programming Design Good Classes: Encapsulation and OOP Analysis Yang (Richard) Yang Computer Science Department Yale University.

Cartograms

Cartogram. Area of country proportional to population.

Page 26: CS 112 Introduction to Programming Design Good Classes: Encapsulation and OOP Analysis Yang (Richard) Yang Computer Science Department Yale University.

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

Summary