CS 151: Object-Oriented Design August 22 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak www.cs.sjsu.edu/~mak.

Slides:



Advertisements
Similar presentations
Schedule and Effort. Planning Big Project: Waterfall-ish Style 1.Figure out what the project entails Requirements, architecture, design 2.Figure out dependencies.
Advertisements

HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Good software.
Object-Oriented Analysis & Design Chapter 1 NTPCUG Study Series.
HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.
Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
CS 153: Concepts of Compiler Design August 25 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
July 16, Introduction to CS II Data Structures Hongwei Xi Comp. Sci. Dept. Boston University.
CS 235: User Interface Design January 22 Class Meeting
CS 160: Software Engineering August 25 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
Unified Modeling Language
CS 235: User Interface Design August 27 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
CS 235: User Interface Design August 27 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CS 46B: Introduction to Data Structures July 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
Welcome to CS 115! Introduction to Programming. Class URL Please write this down!
CS 46B: Introduction to Data Structures July 7 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
CSE 501N Fall ‘09 00: Introduction 27 August 2009 Nick Leidenfrost.
CS 153: Concepts of Compiler Design August 24 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Eng. Mohammed Timraz Electronics & Communication Engineer University of Palestine Faculty of Engineering and Urban planning Software Engineering Department.
CS 235: User Interface Design August 25 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
1 Welcome to CS 362 Applied Software Engineering What happens after (and during) design? Testing, debugging, maintaining programs Lessons for software.
CS 46B: Introduction to Data Structures June 16 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
CS Fall 2007 Dr. Barbara Boucher Owens. CS 2 Text –Main, Michael. Data Structures & Other Objects in Java Third Edition Objectives –Master building.
CS 46B: Introduction to Data Structures June 16 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
CS 151: Object-Oriented Design September 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Welcome to CS 115! Introduction to Programming. Class URL Write this down!
CS 46B: Introduction to Data Structures July 9 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
CS 235: User Interface Design September 22 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CS 160: Software Engineering October 15 Class Meeting
CS 151: Object-Oriented Design October 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CS 151: Object-Oriented Design September 26 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CS 153: Concepts of Compiler Design August 26 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CS 151: Object-Oriented Design September 5 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak
CS 151: Object-Oriented Design September 12 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CS 151: Object-Oriented Design August 27 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CS 160: Software Engineering October 22 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
Chapter 17 – Object- Oriented Design. Chapter Goals To learn about the software life cycle To learn about the software life cycle To learn how to discover.
CS 152: Programming Language Paradigms January 27 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CS 151: Object-Oriented Design October 15 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CS 151: Object-Oriented Design August 29 Class Meeting Department of Computer Science San Jose State University Spring 2012 Instructor: Ron Mak
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
Refactoring1 Improving the structure of existing code.
CS 46B: Introduction to Data Structures June 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
CS 146: Data Structures and Algorithms June 11 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
CS 46B: Introduction to Data Structures July 21 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
CS 46B: Introduction to Data Structures July 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
CS 151: Object-Oriented Design November 26 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Unified Modeling Language (UML)
Part 1: Composition, Aggregation, and Delegation Part 2: Iterator COMP 401 Fall 2014 Lecture 10 9/18/2014.
CS 153: Concepts of Compiler Design August 24 Class Meeting
CS 153: Concepts of Compiler Design August 29 Class Meeting
CMPE 135: Object-Oriented Analysis and Design October 24 Class Meeting
CMPE 152: Compiler Design January 25 Class Meeting
CMPE 152: Compiler Design August 21 Class Meeting
CMPE 135: Object-Oriented Analysis and Design August 23 Class Meeting
CMPE 135: Object-Oriented Analysis and Design August 30 Class Meeting
CMPE 152: Compiler Design August 23 Class Meeting
CMPE 152: Compiler Design January 24 Class Meeting
CMPE 135 Object-Oriented Analysis and Design March 21 Class Meeting
CMPE 152: Compiler Design January 29 Class Meeting
CMPE 135: Object-Oriented Analysis and Design February 5 Class Meeting
CS 151: Object-Oriented Design October 8 Class Meeting
CMPE 152: Compiler Design August 22 Class Meeting
CMPE 135 Object-Oriented Analysis and Design August 22 Class Meeting
CMPE 152: Compiler Design August 27 Class Meeting
Presentation transcript:

CS 151: Object-Oriented Design August 22 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak

SJSU Dept. of Computer Science Fall 2013: August 22 CS 151: Object-Oriented Design © R. Mak 2 Goals of the Course  Become better programmers. Develop well-designed software applications that do what they're supposed to do and are flexible, reliable, and maintainable. Use proven object-oriented techniques.  Learn important job skills that employers want. Work as a member of a small programming team. Gain experience on how to cooperate and coordinate your joint efforts to design, develop, and test applications. Employ modern industry-standard software engineering practices. _

SJSU Dept. of Computer Science Fall 2013: August 22 CS 151: Object-Oriented Design © R. Mak 3 Course Notes  Class website Green sheet Lecture notes and handouts Assignments  Required textbook: Object-Oriented Design & Patterns, 2 nd edition by Cay Horstmann _

SJSU Dept. of Computer Science Fall 2013: August 22 CS 151: Object-Oriented Design © R. Mak 4 Course Overview  First half Journey to good design Object-oriented design process Guidelines for class design Interface types and polymorphism  Midterm  Second half Patterns and GUI programming Inheritance and abstract classes The Java object model Frameworks Multithreaded programming  Final There will be quizzes during some classes.

SJSU Dept. of Computer Science Fall 2013: August 22 CS 151: Object-Oriented Design © R. Mak 5 Small Project Teams  Assignments will be done by small project teams. Each team will turn in one set of work for each assignment. Each team member will get the same score for the assignment. Each team is responsible for choosing a team lead and dividing up the work among the team members.  Form your own teams of 3 or 4 students each. Choose your team members wisely!  Be sure you’ll be able to meet and communicate with each other and work together well.  No moving to another team. me your team name and the list of team members and addresses by Thursday, August 29:

SJSU Dept. of Computer Science Fall 2013: August 22 CS 151: Object-Oriented Design © R. Mak 6 Individual Responsibilities You are personally responsible for participating and contributing to your team’s work, and for understanding each part of the work for every assignment, whether or not you worked on that part.

SJSU Dept. of Computer Science Fall 2013: August 22 CS 151: Object-Oriented Design © R. Mak 7 Postmortem Assessment Report  At the end of the semester, each student will individually turn in a short (1 or 2 pp.) report: A brief description of what you learned in the course.  An assessment of your personal accomplishments for your project team. An assessment of each of your project team members. _

SJSU Dept. of Computer Science Fall 2013: August 22 CS 151: Object-Oriented Design © R. Mak 8 Individual Student’s Overall Class Grade  50% assignments (team scores)  15% quizzes (individual scores)  15% midterm exam (individual score)  20% final exam (individual score)  Final letter grade based on the class curve.  Participation will be important! Can move your final grade up or down, especially in borderline cases. Participation in class Participation in your team  As reported by the postmortem assessment reports. _

SJSU Dept. of Computer Science Fall 2013: August 22 CS 151: Object-Oriented Design © R. Mak 9 Take roll!

SJSU Dept. of Computer Science Fall 2013: August 22 CS 151: Object-Oriented Design © R. Mak 10 It’s Not Just Object-Oriented Design  The full title of the course really ought to be Object-Oriented Analysis and Design (OOAD) We’ll see later what we mean by analysis. Do we know what we mean by design?  architecture  appearance  functionality  how something is built  good vs. bad? _

SJSU Dept. of Computer Science Fall 2013: August 22 CS 151: Object-Oriented Design © R. Mak 11 What Makes a Software Application Good?  It does what it’s supposed to do.  It’s well-designed. reliable robust flexible object-oriented architecture? uses design patterns?  It’s easy to modify and maintain. Things are always changing! _

SJSU Dept. of Computer Science Fall 2013: August 22 CS 151: Object-Oriented Design © R. Mak 12 How Do You Achieve “Good Design”?  Sorry, there is no magic formula. Learning lots of object-oriented tools and techniques alone won’t give you good design. Just using design patterns won’t give you good design. For a nontrivial application, good design won’t simply “happen”.  Good design is a destination reached after a journey. Every programmer must take this trip for every application. The journey can be longer for less-experienced programmers.  false starts  meandering  wrong paths  backtracking _

SJSU Dept. of Computer Science Fall 2013: August 22 CS 151: Object-Oriented Design © R. Mak 13 It’s an Iterative Process  Achieving good design is an iterative process. As you’re developing the application, you will revisit your design several times. Even the very best programmers can’t come up with the perfect good design the first time every time. The journey to good design requires that you make corrections, refinements, and other improvements along the way.  The journeys will become shorter as you become more experienced. Practice, practice, practice. Learn object-oriented tools and techniques. Learn when to use design patterns. More practice, practice, practice. _

SJSU Dept. of Computer Science Fall 2013: August 22 CS 151: Object-Oriented Design © R. Mak 14 A Poor Design is Not Necessarily a Failure... ... if it soon leads to a better design. Don’t paralyze yourself trying to come up with a perfect design right from the start.  Goal: Recognize a poor design early during development and start to improve it iteratively as soon as possible.  Even better: Try not to start with a really bad design. You will learn quickly how not to do a bad design! _

SJSU Dept. of Computer Science Fall 2013: August 22 CS 151: Object-Oriented Design © R. Mak 15 Example: Rick’s Guitars  Inventory Management Application for Rick’s Guitars Maintain a guitar inventory. Locate guitars for customers. UML class diagrams From: Head First Object-Oriented Analysis & Design, O’Reilly, 2006.

SJSU Dept. of Computer Science Fall 2013: August 22 CS 151: Object-Oriented Design © R. Mak 16 public class Guitar { private String serialNumber, builder, model, type, backWood, topWood; private double price; public Guitar(String serialNumber, double price, String builder, String model, String type, String backWood, String topWood) { this.serialNumber = serialNumber; this.price = price; this.builder = builder; this.model = model; this.type = type; this.backWood = backWood; this.topWood = topWood; }... The Guitar Class Why private?

SJSU Dept. of Computer Science Fall 2013: August 22 CS 151: Object-Oriented Design © R. Mak public String getSerialNumber() {return serialNumber;} public double getPrice() {return price;} public void setPrice(float newPrice) { this.price = newPrice; } public String getBuilder() {return builder;} public String getModel() {return model;} public String getType() {return type;} public String getBackWood() {return backWood;} public String getTopWood() {return topWood;} } The Guitar Class, cont’d

SJSU Dept. of Computer Science Fall 2013: August 22 CS 151: Object-Oriented Design © R. Mak 18 The Inventory Class public class Inventory { private List guitars; public Inventory() { guitars = new LinkedList(); } public void addGuitar(String serialNumber, double price, String builder, String model, String type, String backWood, String topWood) { Guitar guitar = new Guitar(serialNumber, price, builder, model, type, backWood, topWood); guitars.add(guitar); }... }

SJSU Dept. of Computer Science Fall 2013: August 22 CS 151: Object-Oriented Design © R. Mak 19 The Inventory Class, cont’d public Guitar getGuitar(String serialNumber) { for (Iterator iter = guitars.iterator(); iter.hasNext(); ) { Guitar guitar = (Guitar) iter.next(); if (guitar.getSerialNumber().equals(serialNumber)) { return guitar; } return null; }

SJSU Dept. of Computer Science Fall 2013: August 22 CS 151: Object-Oriented Design © R. Mak 20 The Inventory Class, cont’d public Guitar search(Guitar searchGuitar) { for (Iterator iter = guitars.iterator(); iter.hasNext(); ) { Guitar guitar = (Guitar) iter.next(); String builder = searchGuitar.getBuilder(); if ((builder != null) && (!builder.equals("")) && (!builder.equals(guitar.getBuilder()))) continue; String model = searchGuitar.getModel(); if ((model != null) && (!model.equals("")) && (!model.equals(guitar.getModel()))) continue; String type = searchGuitar.getType(); if ((type != null) && (!searchGuitar.equals("")) && (!type.equals(guitar.getType()))) continue; String backWood = searchGuitar.getBackWood(); if ((backWood != null) && (!backWood.equals("")) && (!backWood.equals(guitar.getBackWood()))) continue; String topWood = searchGuitar.getTopWood(); if ((topWood != null) && (!topWood.equals("")) && (!topWood.equals(guitar.getTopWood()))) continue; return guitar; } return null; }

SJSU Dept. of Computer Science Fall 2013: August 22 CS 151: Object-Oriented Design © R. Mak 21 Problems!  Case-sensitive string comparisons. Make them case insensitive.  Badly used string fields. Replace them with enumerated types.  Assumes at most only one guitar match. Return a list of matching guitars. _

SJSU Dept. of Computer Science Fall 2013: August 22 CS 151: Object-Oriented Design © R. Mak 22 Iteration #1: Remove String Fields public class Guitar { private String serialNumber, model; private double price; private Builder builder; private Type type; private Wood backWood, topWood; … public enum Type { ACOUSTIC("acoustic"), ELECTRIC("electric"), UNSPECIFIED("unspecified"); String value; private Type(String value) {this.value = value;} public String toString() {return value;} } Why private?

SJSU Dept. of Computer Science Fall 2013: August 22 CS 151: Object-Oriented Design © R. Mak 23 Iteration #2: Return Multiple Matching Guitars public List search(Guitar searchGuitar) { List matchingGuitars = new LinkedList(); for (Iterator iter = guitars.iterator(); iter.hasNext(); ) { Guitar guitar = (Guitar) iter.next(); // Ignore serial number since that's unique. // Ignore price since that's unique. if (searchGuitar.getBuilder() != guitar.getBuilder()) continue; String model = searchGuitar.getModel().toLowerCase(); if ((model != null) && (!model.equals("")) && (!model.equals(guitar.getModel().toLowerCase()))) continue; if (searchGuitar.getType() != guitar.getType()) continue; if (searchGuitar.getBackWood() != guitar.getBackWood()) continue; if (searchGuitar.getTopWood() != guitar.getTopWood()) continue; matchingGuitars.add(guitar); } return matchingGuitars; }

SJSU Dept. of Computer Science Fall 2013: August 22 CS 151: Object-Oriented Design © R. Mak 24 Still More Problems!  Customers don’t always know all the characteristics of the guitar they want. Need wildcard search fields?  Rick may decide to add more guitar characteristics to his inventory. Example: Number of guitar strings  The search() method of class Inventory is going to get complicated really fast. It will be difficult to maintain as more guitar characteristics are added. _

SJSU Dept. of Computer Science Fall 2013: August 22 CS 151: Object-Oriented Design © R. Mak 25 What’s Changing?  The characteristics of a guitar can change. Rick can decide to add, remove, or modify them.  The inventory keeps track of guitars, not guitar characteristics. Therefore, the inventory code should not change when the guitar characteristics change.  If we encapsulate what changes (guitar characteristics), we can isolate those changes from the rest of the code. Goal: When the guitar characteristics change, the rest of the code does not need to change. _

SJSU Dept. of Computer Science Fall 2013: August 22 CS 151: Object-Oriented Design © R. Mak 26 The Solution: Encapsulation  Create a new GuitarSpec class that represents the characteristics of a guitar. Only the GuitarSpec class needs to change if the characteristics change. Therefore, the GuitarSpec class encapsulates the changes and isolates them from the rest of the code. _

SJSU Dept. of Computer Science Fall 2013: August 22 CS 151: Object-Oriented Design © R. Mak 27 The New GuitarSpec Class public class GuitarSpec { private Builder builder; private String model; private Type type; private int numStrings; private Wood backWood; private Wood topWood; public GuitarSpec(Builder builder, String model, Type type, int numStrings, Wood backWood, Wood topWood) { this.builder = builder; this.model = model; this.type = type; this.numStrings = numStrings; this.backWood = backWood; this.topWood = topWood; } … public int getNumStrings() {return numStrings;} … }

SJSU Dept. of Computer Science Fall 2013: August 22 CS 151: Object-Oriented Design © R. Mak 28 Guitar - GuitarSpec Class Diagram  This UML class diagram shows that: A Guitar aggregates a GuitarSpec.  A GuitarSpec is part of a Guitar. The relationship is one-to-one. _ From: Head First Object-Oriented Analysis & Design, O’Reilly, 2006.

SJSU Dept. of Computer Science Fall 2013: August 22 CS 151: Object-Oriented Design © R. Mak 29 Updated Guitar Class public class Guitar { private String serialNumber; private double price; GuitarSpec spec; public Guitar(String serialNumber, double price, Builder builder, String model, Type type, Wood backWood, Wood topWood) { this.serialNumber = serialNumber; this.price = price; this.spec = new GuitarSpec(builder, model, type, backWood, topWood); } … public GuitarSpec getSpec() {return spec;} }

SJSU Dept. of Computer Science Fall 2013: August 22 CS 151: Object-Oriented Design © R. Mak 30 Time to Refactor Again!  Refactor: To modify the structure of your code without modifying its behavior in order to improve it in some way.  If the guitar characteristics change, such as adding the number of guitar strings, then the search method needs to change. The customer may want to search for a guitar that matches a certain number of strings. What do we need to do with code that changes?  We need to move the guitar matching algorithm out of the Inventory class (the search() method) and into the new GuitarSpec class in order to encapsulate the changes to the search method. _

SJSU Dept. of Computer Science Fall 2013: August 22 CS 151: Object-Oriented Design © R. Mak 31 Method matches() of Class GuitarSpec public class GuitarSpec { … public boolean matches(GuitarSpec otherSpec) { if (builder != otherSpec.builder) return false; if ((model != null) && (!model.equals("")) && (!model.toLowerCase().equals(otherSpec.model.toLowerCase()))) return false; if (type != otherSpec.type) return false; if (numStrings != otherSpec.numStrings) return false; if (backWood != otherSpec.backWood) return false; if (topWood != otherSpec.topWood) return false; return true; } … } This code was originally in the search() method of class Inventory.

SJSU Dept. of Computer Science Fall 2013: August 22 CS 151: Object-Oriented Design © R. Mak 32 Updated search() Method of Class Inventory public class Inventory {... public List search(GuitarSpec searchSpec) { List matchingGuitars = new LinkedList(); for (Iterator iter = guitars.iterator(); iter.hasNext(); ) { Guitar guitar = (Guitar) iter.next(); if (guitar.getSpec().matches(searchSpec)) { matchingGuitars.add(guitar); } } return matchingGuitars; }... } This code is a lot easier to read! Now the search() method delegates the matching algorithm to the matches() method of the GuitarSpec class.

SJSU Dept. of Computer Science Fall 2013: August 22 CS 151: Object-Oriented Design © R. Mak 33 Application Development Big Picture

SJSU Dept. of Computer Science Fall 2013: August 22 CS 151: Object-Oriented Design © R. Mak 34 Iterative Development

SJSU Dept. of Computer Science Fall 2013: August 22 CS 151: Object-Oriented Design © R. Mak 35 Incremental Development  Each iteration adds functionality to code that already works.  No Big Bang! _ Start Goal From: Head First Object-Oriented Analysis & Design, O’Reilly, 2006.