CS 151: Object-Oriented Design September 5 Class Meeting Department of

25 Slides582.50 KB

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

Class Relationships: Dependency Class C depends on class D: Dependency is asymmetric. Some method of C manipulates objects of D Example: Mailbox objects manipulate Message objects. The Message class is not aware of the existence of the Mailbox class. Therefore, Message objects do not depend on Mailbox objects. Loose coupling Minimize the number of dependency relationships. Another way for a design to handle change. SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design R. Mak 2

Class Relationships: Aggregation Class C aggregates class A: A special case of dependency. Objects of class C contains objects of class A over a period of time. The “has-a” relationship. Example: An Inventory object has a list of Instrument objects. Multiplicity 1:1 – Example: Each Person object has a single StreetAddress object. 1:n – Example: Each Inventory object has an array of multiple Instrument objects. SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design R. Mak 3

Class Relationships: Inheritance Class C inherits from class S. The “is-a” relationship. All class C objects are special cases of class S objects. Class S is the superclass of class C. Class C is a subclass of class S. An object of class C is an object of class S. Note Aggregation: A Mailbox object has a Message object. Inheritance: A ForwardedMessage object is a Message object. SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design R. Mak 4

The CRC Technique Class-Responsibilities-Collaborations An effective technique to discover classes, responsibilities, and relationships. CRC card is an index card that Describes one class. Lists its responsibilities. Lists its relationship with other classes. SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design R. Mak 5

The CRC Technique Example: class Mailbox Mailbox Responsibilities Relationships Manage passcode MessageQueue Manage greeting Create new message SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design R. Mak 6

The CRC Technique 1. Write the class name on each index card. 2. Distribute the responsibilities among the classes. 3. Find out their relationships and list all dependencies of each classes. 4. Don't write the methods or instance fields. Just write the responsibilities at a high level language. SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design R. Mak 7

UML Diagrams A picture is worth a thousand words! It is much easier to extract information from a graphical notation than reading a textual document. Show your design in graphical UML diagrams. UML: Unified Modeling Language There are several different types of UML diagrams. For now, we’ll use Class diagrams Sequence diagrams State diagrams SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design R. Mak 8

UML Class Diagram A class diagram has three compartments: Class Name Attributes : types Methods(parms : types) : return type SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design R. Mak 9

UML Class Diagram: Attributes and Methods Specify the most important attributes and methods. If you have too many attributes in a class, check if you can group them into a new class. Example: You have attributes that are specific to your class. But you also have name, street, city, state, and zip attributes. Create a new Address class to contain those attributes. Then your class has an address. SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design R. Mak 10

Example UML Class Diagram Mailbox newMessages : ArrayList Message savedMessages : ArrayList Message add(msg : Message) : boolean getCurrentMessage() : Message SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design R. Mak 11

UML Class Diagram: Relationships Relationships among classes using arrows. Dependency Aggregation Inheritance Composition Association Direct association Interface implementation SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design R. Mak 12

UML Class Diagram: Multiplicities Multiplicity in a “has” relationship. SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design R. Mak 13

UML Class Diagram: Aggregation Aggregation A “has a” relationship. The contained object can have an existence independent of its container. Example A mailbox has a set of messages. A message can exist without a mailbox. Therefore, a mailbox aggregates messages. Mailbox SJSU Dept. of Computer Science Spring 2013: September 3 1 * CS 151: Object-Oriented Design R. Mak Message 14

UML Class Diagram: Composition Composition A “has a” relationship. The contained object cannot (logically) have an existence independent of its container. Example A mailbox has a message queue. The message queue cannot (logically) exist without a mailbox. Therefore, a mailbox composes a message queue. Mailbox SJSU Dept. of Computer Science Spring 2013: September 3 1 1 CS 151: Object-Oriented Design R. Mak MessageQueue 15

UML Sequence Diagram A class diagram is static. It shows the classes that exist throughout the lifetime of the system. A sequence diagram shows the dynamic relationships among the classes at run time. It describes interaction among objects over time during run time. SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design R. Mak 16

UML Sequence Diagram Withdraw cash Display Keypad Account Bank Customer select notify display confirmation T I M E enter amount notify notify display bank ads verify accept notify dispense cash Withdraw Cash Sequence Diagram SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design R. Mak 17

UML Sequence Diagram Use underlined font for object names to distinguish between class and object names. The dashed vertical lines are lifelines. A rectangle on a lifeline is an activation bar. It shows when a object has control executing a method. The activation bar ends when the method returns. The horizontal arrows are call arrows. Use a sequence diagram to illustrate complex interactions among a set of objects. Don't show loops or branches. SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design R. Mak 18

UML State Diagram At run time, the state of an object is characterized by the values of its fields. In some objects, different states can cause different behaviors. Example: A voice mail system Caller dials voice mail number the state is "Connected" If caller dial a valid extension the state is "Recording" If in "Recording" state the caller can type in the passcode the state is "Main menu“ SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design R. Mak 19

UML State Diagram Connected Ha ng up g up anng H Ha Change passcode Pas sco de E nte red SJSU Dept. of Computer Science Spring 2013: September 3 Exte nsio n En tere d Recording #3 Main menu CS 151: Object-Oriented Design R. Mak red e t En e co d s s Pa 20

Free UML Design Tools StarUML: http://staruml.sourceforge.net/en/ Violet: http://horstmann.com/violet/ Dr. Horstmann’s product SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design R. Mak 21

javadoc The javadoc utility creates a set of HTML pages for your classes. It reads specially formatted comments in your code. Delimit comments for javadoc by /** and */ It copies the first sentence of your comment to a summary table. Write the first sentence carefully. It should start with an uppercase letter and end with a period. SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design R. Mak 22

javadoc Javadoc comment example: /** * Choose the test string with the highest score and return it. * @param sequence the current sequence. * @param store the sequence store. * @return the chosen test string. */ private String chooseString(CurrentSequence sequence, SequenceStore store) { . } Method parameter: @param paramname explantion Function return value: @return explanation If the method throws an exception: @throws exceptionname explanation SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design R. Mak 23

Run the javadoc Utility On the command line: javadoc path/*.java You can also run javadoc from within NetBeans or Eclipse. SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design R. Mak Demo 24

Program Design and javadoc Write only the skeletons of your classes. No implementation initially. Just include javadoc comments. The result is an outline summary of your design. Example: /** * Choose the test string with the highest score and return it. * @param sequence the current sequence. * @param store the sequence store. * @return the chosen test string. */ private String chooseString(CurrentSequence sequence, SequenceStore store) { // To be implemented. } SJSU Dept. of Computer Science Spring 2013: September 3 CS 151: Object-Oriented Design R. Mak 25

Back to top button