160 likes | 238 Views
What’s the Goal of Design?. 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/.
E N D
What’s the Goal of Design? 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/ Answer: Flexibility
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? • I think the two papers were saying the same thing about object oriented design, but in different ways. • I think the two papers were saying two different, unrelated things about object oriented design. • I think the two papers disagreed with each other about what object oriented design is.
What We Will Do Today • We’ll look at OO In One Sentence • We’ll do an exercise for Open/Closed Principle • I’ll try to illustrate both the similarities and differences between the approaches to design between our two papers
Keep it DRY • Keep it shy • Tell the other guy
Keep it DRY • Keep it shy • Tell the other guy • Some might say that this particular admonition, properly interpreted, contains within it the majority of programming design
Fix (FIX #1) FileWriterfstream = new FileWriter("logfile.txt"); BufferedWriterout = new BufferedWriter(fstream); out.write("LOG: Beginning Computation"); out.close(); int result1 = doComputationPart1(data1); intresult2 = doComputationPart2(data2); intresult3 = doComputationPart3(data1, data2); if(result1 == -1 || result2 == -1 || result3 == -1) { FileWriterfstream = new FileWriter("logfile.txt"); BufferedWriterout = new BufferedWriter(fstream); out.write("LOG: There was an error in computation"); out.close(); } else { FileWriterfstream = 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 Script CREATE TABLE Persons(P_Idint,LastNamevarchar(255),FirstNamevarchar(255),Address varchar(255),City varchar(255)) DB Table Create Script class PersonDBEntry { public String getLastName() { DBEntry row = getMyEntry(); return row.getValue(“LastName”); } • public String getLastName() { • DBEntry row = getMyEntry(); • return row.getValue(“FirstName”); • } //and so on }
Keep it DRY – Don’t Repeat Yourself • Keep it shy • Tell the other guy • Is often your number one guide to good design • There is more to DRY than just code duplication
Keep it DRY – Don’t Repeat Yourself • Keep it shy • Tell the other guy • Coupling/Cohesion • Coupling -> bad • Cohesion -> good
Why is this bad? Fix? (FIX #3) public booleanisOrderValid() { //a whole bunch of other validity check code String state = getOrder().getCustomer().getAddress().getState(); intzipCode = 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 } OPTION 1 OPTION 2 class Circle extends Point { private double radius; } class Circle { private Point center; private double radius; }
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 Yourself • Keep it shy – Avoid unnecessary coupling • Tell the Other Guy String state = getOrder().getCustomer().getAddress().getState(); intzipCode = getOrder().getCustomer().getAddress().getZipCode(); if(!stateAndZipCodeMatch(state, zipCode)) return false; VERSES if(!getOrder().getCustomer().isValid()) return false;