What’s the Goal of Design? Answer: Flexibility As you arrive… If you have not done so already,...

16
What’s the Goal of Design? Answer: Flexibility As you arrive… If you have not done so already, please set up your computer to snarf code using the Ambient plugin. You can see instructions for how to do this in the code section of the course website: http://www.cs.duke.edu/courses/spring12/cps108/code/

Transcript of What’s the Goal of Design? Answer: Flexibility As you arrive… If you have not done so already,...

Page 1: What’s the Goal of Design? Answer: Flexibility As you arrive… If you have not done so already, please set up your computer to snarf code using the Ambient.

What’s the Goal of Design?Answer: Flexibility

As you arrive…If you have not done so already, please set up your computer to snarf code using the Ambient plugin. You can see instructions for how to do this in the code section of the course website:

http://www.cs.duke.edu/courses/spring12/cps108/code/

Page 2: What’s the Goal of Design? Answer: Flexibility As you arrive… If you have not done so already, please set up your computer to snarf code using the Ambient.

How is it possible that something like code could ever be inflexible?

Page 3: What’s the Goal of Design? Answer: Flexibility As you arrive… If you have not done so already, please set up your computer to snarf code using the Ambient.

You did two different readings for today – the The Open Closed Principle and OO in 1 Sentence. How would you characterize the two points presented?

1. I think the two papers were saying the same thing about object oriented design, but in different ways.

2. I think the two papers were saying two different, unrelated things about object oriented design.

3. I think the two papers disagreed with each other about what object oriented design is.

Page 4: What’s the Goal of Design? Answer: Flexibility As you arrive… If you have not done so already, please set up your computer to snarf code using the Ambient.

What We Will Do Today

1. We’ll look at OO In One Sentence2. We’ll do an exercise for Open/Closed

Principle3. I’ll try to illustrate both the similarities and

differences between the approaches to design between our two papers

Page 5: What’s the Goal of Design? Answer: Flexibility As you arrive… If you have not done so already, please set up your computer to snarf code using the Ambient.

Keep it DRYKeep it shyTell the other guy

Page 6: What’s the Goal of Design? Answer: Flexibility As you arrive… If you have not done so already, please set up your computer to snarf code using the Ambient.

• Some might say that this particular admonition, properly interpreted, contains within it the majority of programming design

Keep it DRYKeep it shyTell the other guy

Page 7: What’s the Goal of Design? Answer: Flexibility As you arrive… If you have not done so already, please set up your computer to snarf code using the Ambient.

Fix (FIX #1)FileWriter fstream = new FileWriter("logfile.txt"); BufferedWriter out = new BufferedWriter(fstream); out.write("LOG: Beginning Computation");out.close();

int result1 = doComputationPart1(data1);int result2 = doComputationPart2(data2);int result3 = doComputationPart3(data1, data2);

if(result1 == -1 || result2 == -1 || result3 == -1) { FileWriter fstream = new FileWriter("logfile.txt"); BufferedWriter out = new BufferedWriter(fstream); out.write("LOG: There was an error in computation"); out.close();} else { FileWriter fstream = new FileWriter("logfile.txt"); BufferedWriter out = new BufferedWriter(fstream); out.write("LOG: Computation Completed Sucessfully!"); out.close();}

Page 8: What’s the Goal of Design? Answer: Flexibility As you arrive… If you have not done so already, please set up your computer to snarf code using the Ambient.

Fix (FIX #2)class Line{ private Point start, end; private double length;

public Line(Point start, Point end, double length) { this.start = start; this.end = end; this.length = length; }

public getLength() { return length; }}

Page 9: What’s the Goal of Design? Answer: Flexibility As you arrive… If you have not done so already, please set up your computer to snarf code using the Ambient.

Fix?DB Table Create ScriptCREATE TABLE Persons

(P_Id int,LastName varchar(255),FirstName varchar(255),Address varchar(255),City varchar(255))

DB Table Create Scriptclass PersonDBEntry {

public String getLastName() { DBEntry row = getMyEntry(); return row.getValue(“LastName”); }

public String getLastName() { DBEntry row = getMyEntry(); return row.getValue(“FirstName”); } //and so on}

Page 10: What’s the Goal of Design? Answer: Flexibility As you arrive… If you have not done so already, please set up your computer to snarf code using the Ambient.

• Is often your number one guide to good design

• There is more to DRY than just code duplication

Keep it DRY – Don’t Repeat YourselfKeep it shyTell the other guy

Page 11: What’s the Goal of Design? Answer: Flexibility As you arrive… If you have not done so already, please set up your computer to snarf code using the Ambient.

• Coupling/Cohesion• Coupling -> bad• Cohesion -> good

Keep it DRY – Don’t Repeat YourselfKeep it shyTell the other guy

Page 12: What’s the Goal of Design? Answer: Flexibility As you arrive… If you have not done so already, please set up your computer to snarf code using the Ambient.

Why is this bad? Fix? (FIX #3)public boolean isOrderValid() { //a whole bunch of other validity check code String state =

getOrder().getCustomer().getAddress().getState(); int zipCode =

getOrder().getCustomer().getAddress().getZipCode(); if(!stateAndZipCodeMatch(state, zipCode)) return false; // still more validity check code}

Page 13: What’s the Goal of Design? Answer: Flexibility As you arrive… If you have not done so already, please set up your computer to snarf code using the Ambient.

Which is better? (FIX #4)class Point{ protected double x, y; //many handy methods}

class Circle extends Point{ private double radius; }

class Circle{ private Point center; private double radius; }

OPTION 1 OPTION 2

Page 14: What’s the Goal of Design? Answer: Flexibility As you arrive… If you have not done so already, please set up your computer to snarf code using the Ambient.

Temporal Coupling

Every day at 10pm the script generateDailyReceipts runs. It takes up to 15 minutes and It creates the file dailyReports.dat, which includes the profits for the day.

Every day at 11pm the script validateReceipts runs. It reads dailyReports.dat and some other files, and if there is anything that looks amiss, it notifies the accounting problem via email.

Page 15: What’s the Goal of Design? Answer: Flexibility As you arrive… If you have not done so already, please set up your computer to snarf code using the Ambient.

Keep it DRY – Don’t Repeat YourselfKeep it shy – Avoid unnecessary couplingTell the Other Guy

String state = getOrder().getCustomer().getAddress().getState();

int zipCode = getOrder().getCustomer().getAddress().getZipCode();

if(!stateAndZipCodeMatch(state, zipCode)) return false;

VERSES

if(!getOrder().getCustomer().isValid()) return false;

Page 16: What’s the Goal of Design? Answer: Flexibility As you arrive… If you have not done so already, please set up your computer to snarf code using the Ambient.

Open Closed Principle