310 likes | 406 Views
Foundations of Software Design Fall 2002 Marti Hearst. Lecture 7: Object-Oriented Design. Design Processes. What is design? A creative problem solving process Key aspects of design Create use scenarios Consider several options Iterate, iterate, iterate (rarely right the first time)
E N D
Foundations of Software DesignFall 2002Marti Hearst Lecture 7: Object-Oriented Design
Design Processes • What is design? • A creative problem solving process • Key aspects of design • Create use scenarios • Consider several options • Iterate, iterate, iterate (rarely right the first time) • How to learn design? • By seeing (lots of) examples • By doing (lots of) examples
Key Software Design Goals • Simplicity • Often leads to elegance • Easier to understand and debug the code • Reduces errors • Reliability / Robustness • Can handle unexpected input • Recovers from errors gracefully • Doesn’t damage anything • Effectiveness • Solves the right problems (intuitive for users) • Efficient • Compatible with other software
i) CPU using 3 chips (classes) ii) CPU using 3 chips (classes) Complex vs. Simple Design What is the problem here? Slide adapted from James Landay
Good vs. Bad Design • The designs functionally equivalent, but the 2nd is • hard to understand • hard to locate faults • difficult to extend or enhance • cannot be reused in another program. • -> expensive to perform maintenance • Good modules must be like the 1st design • maximal relationships within classes (cohesion) • minimal relationships between classes (coupling) Slide adapted from James Landay
Other Software Design Goals • Reusability • Adaptability
OO Design • Object languages allow advantages, but don’t provide them. • Database models are data-oriented. OO models are responsibility-oriented. • Special emphasis on • Abstraction and generalization • Encapsulation and information hiding • Modularity
OOD: Data & Action Are Equal • Actions & data are intertwined • data cannot change unless an action is performed on it • actions without associated data are equally meaningless • Object consists of • data • attributes, state variables, instance vars, fields, … • actions • methods, member functions • plus inheritance • Objects should • model all aspects of one (physical) entity • Objects are independent units • easier to maintain & combine into a larger program Slide adapted from James Landay
Information Hiding • Really “details hiding” • hiding implementation details, not information • design classes so that items likely to change are hidden • Localizes future changes • Changes cannot affect other classes Slide adapted from James Landay
Inheritance • New data types defined as extensions to previously defined types • don’t have to define from scratch -> provides more data abstraction • Example Define humanBeing to be a class A humanBeing has attributes, such as age, height, gender Assign values to attributes when describing object Define parent to be a subclass of humanBeing A parent has all attributes of humanBeing, plus attributes of his/her own (name of oldest child, number of children) A parent inherits all attributes of humanBeing Slide adapted from James Landay
Inheritance (Java) class HumanBeing { privateint age; private float height; public // declarations of operations on HumanBeing } // class HumanBeing class Parent extends HumanBeing { privateString nameOfOldestChild; private int numberOfChildren; public // declarations of operations on Parent } // class Parent Slide adapted from James Landay
Multiple Inheritance • Class inherits from more than one superclass • In java, must use an indirect method • Interfaces • We’ll learn more about this when we do sorting
OO Design • Write a description of what should happen • Nouns: objects • Verbs: methods • Example: Create an address list • Create an address list that includes each person’s name, address, telephone number, and e-mailaddress. This list should be printed in alphabeticalorder. • Reconsider, and discard some: • Order acts as a modifier, not a noun • Person’s: possessive indicate a potential grouping
Person (superclass) OO Design Example • Example: A Party • What are the objects? • Hosts • Guests • Invitation • Address • Of party • Of guest • Date • Theme • Food/Drinks • Music
OO Design Example • Example: A Party • What are the methods? • Shop • Cook • Send invitations • Need a list of invitation objects • Clean the house • RSVP • Travel to the party • Which objects have which responsibilities?
General Approach • Define the problem • Create scenarios of use • Do interviews, fieldwork, to find out what is needed • Scenarios show typical examples of use • Use CRC cards to define • Objects • Begin with the most important ones • Responsibilities • The main thing(s) these objects do(es) • Relations to other objects
Class name: Superclass: Subclasses: Responsibilities: Collaborations: CRC Cards • Class, Responsibility, Collaboration • Responsibilities: tasks to perform • Collaborations: other objects this object works with
Class name: Superclass: Subclasses: Responsibilities: Collaborations: CRC Card for Host/Hostess Person Host(ess) Send Invitations Invitation, Person, List Shop Money, Store, Food, Car, … Clean House Sponge, Cleaner …
Class name: Superclass: Subclasses: Responsibilities: Collaborations: CRC Card for Guest Person Guest RSVP Phone, Host(ess)
CRC to UML • CRC cards are used to define the main classes and their interactions • A brainstorming tool • Write many, throw many away • UML: Unified Modeling Language • Used to • Refine the design • Describe the design to others
UML: Unified Modeling Language • Notation for describing OO design • Associated with a design methodology • Several types of diagrams • Class diagrams • Static structure • Use Case diagrams • Functionality using actors and cases • Sequence diagrams • Exchange of messages between classes over time • There are many others http://www.smartdraw.com/resources/centers/uml/uml.htm
The Three Steps of OOD • Not a rigid set of rules • There are many different equivalent methods • Represent using UML (unified modeling language) • Three iterative steps • use-case modeling • determine how various results are computed w/o sequencing • largely action oriented • Technique: natural language, flow charts • class modeling (object modeling) • determine the classes, their attributes, & their interrelationships • purely data-oriented • technique: CRC cards followed by UML class diagrams • dynamic modeling • determine the actions performed by or to each class • purely action-oriented Slide adapted from James Landay
Class Diagrams • Class Details • (don’t put all this in unless finalizing the design) http://www.stanford.edu/~gpathy/umlshort/
Class Diagram – Complex Version http://www.stanford.edu/~gpathy/umlshort/
Class Diagrams’ Relationships • association -- a relationship between instances of the two classes. There is an association between two classes if an instance of one class must know about the other in order to perform its work. In a diagram, an association is a link connecting two classes. • aggregation -- an association in which one class belongs to a collection. An aggregation has a diamond end pointing to the part containing the whole. • generalization -- an inheritance link indicating one class is a superclass of the other. A generalization has a triangle pointing to the superclass. http://www.stanford.edu/~gpathy/umlshort/
Class Diagrams • An association has two ends. • An end may have a role name to clarify the nature of the association. • A navigability arrow on an association shows which direction the association can be traversed or queried. • The arrow also lets you know who "owns" the association's implementation • Associations with no navigability arrows are bi-directional. • The multiplicity of an association end is the number of possible instances of the class associated with a single instance of the other end. • Multiplicities are single numbers or ranges of numbers. http://www.stanford.edu/~gpathy/umlshort/
PersonalComputer Printer CPU Monitor Keyboard Aggregation UML notation: diamond (1 or more) no diamond (1) Aggregation refers to the components of a class - used to group related items - each of these (CPU, etc.) would be instance var Slide adapted from James Landay
Class Diagrams • Composition is a strong association in which the part can belong to only one whole -- the part cannot exist without the whole. Composition is denoted by a filled diamond at the whole end. http://www.stanford.edu/~gpathy/umlshort/
HumanBeing (base class) inherits from (“ isA ”) Parent derived (derived class) part additional part Inheritance (UML) UML notation: boxes represent classes open triangle represents inheritance Slide adapted from James Landay
To Think about:Using Objects to Make Objects • Goal: Write Java code to create a Saladclass • The main goal is to create and use objects representing the various components of a salad. • Salad is made up of Ingredients • These are not subclasses; they are components • Methods include wash(), chop() • Different types of salads use different ingredients • But all salads have the same methods: • Prepare() • Calories() • Cost()