1 / 16

What’s the Goal of Design?

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/.

tacey
Download Presentation

What’s the Goal of Design?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

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

  3. 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.

  4. 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

  5. Keep it DRY • Keep it shy • Tell the other guy

  6. 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

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

  8. 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; } }

  9. 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 }

  10. 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

  11. Keep it DRY – Don’t Repeat Yourself • Keep it shy • Tell the other guy • Coupling/Cohesion • Coupling -> bad • Cohesion -> good

  12. 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 }

  13. 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; }

  14. 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.

  15. 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;

  16. Open Closed Principle

More Related