What’s the Goal of Design? Answer: Flexibility As you arrive… If you have not done so already,...
-
Upload
scot-pitts -
Category
Documents
-
view
214 -
download
2
Transcript of What’s the Goal of Design? Answer: Flexibility As you arrive… If you have not done so already,...
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/
How is it possible that something like code could ever be inflexible?
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.
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
Keep it DRYKeep it shyTell the other guy
• 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
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();}
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; }}
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}
• 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
• Coupling/Cohesion• Coupling -> bad• Cohesion -> good
Keep it DRY – Don’t Repeat YourselfKeep it shyTell the other guy
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}
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
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.
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;
Open Closed Principle