1.CMPE 135: Object-Oriented Analysis and Design September 11 Class Meeting Department of Computer Engineering San Jose State University Fall 2018 Instructor: Ron Mak www.cs.sjsu.edu/~mak 1
2.2 Textual Analysis Automatic dog door with bark recognizer: Use case for opening and closing the door. Head First Object-Oriented Analysis & Design by Brett D. McLaughlin, et al. O’Reilly, 2006.
3.3 Textual Analysis , cont’d Nouns the (owner’s) dog the owner the button bark recognizer request inside/outside dog door remote control bark Head First Object-Oriented Analysis & Design by Brett D. McLaughlin, et al. O’Reilly, 2006.
4.4 Textual Analysis , cont’d Not all words are important in a document. Some nouns refer to entities that are outside of your system (application). Examples: owner, dog Some nouns refer to things you don’t have to model or create. Example: request How will the classes that you’ll create support the behavior that your use cases describe?
5.5 Review: Class Responsibilities Responsibilities correspond to verbs in the use cases. Each responsibility should be owned by one and only one class . Common mistakes: Assigning a responsibility to an inappropriate class. Assigning too many responsibilities to a class. Ideally, each class should have a single primary responsibility .
6.6 CRC Cards Class name Optional Responsibilities of this class Classes this class works with to perform its responsibilities Head First Object-Oriented Analysis & Design by Brett D. McLaughlin, et al. O’Reilly, 2006. C lass, R esponsibility, C ollaboration
7.7 CRC Cards for the Dog Door Use Case Head First Object-Oriented Analysis & Design by Brett D. McLaughlin, et al. O’Reilly, 2006.
8.8 UML Class Diagram: Dependency Class A uses class B . This is generally a transient relationship . Example: A method of class A is passed a parameter of class B . Example: A method of class A returns a value of class B . In UML diagrams, draw a dashed line with an open arrowhead from class A to class B . A B
9.9 UML Class Diagrams: Association A relationship between class A and class B that lasts as long as class A objects and class B objects live at runtime. Class A can have an attribute (field) that refers to class B. Mailbox msgQueue : Message[ ]
10.10 UML Class Diagrams: Association , cont’d In UML class diagrams, draw a solid line with an open arrowhead from class A to class B . Label the line with the name of the attribute. Don’t repeat the attribute inside the class box. Can also be an aggregation or a composition . Optionally indicate multiplicity. Mailbox Message msgQueue 1 * Mailbox msgQueue : Message[ ] Replace the attribute with the association.
11.11 Class Diagram Examples What’s in the frontend package of a compiler? UML package diagram Access control + public – private # protected ~ package Writing Compilers and Interpreters, 3 rd ed. by Ronald Mak John Wiley & Sons, 2009. What information can you learn from these class diagrams?
12.12 Class Diagram Examples , cont’d Message handling in the front end of a compiler. frontend and message packages Writing Compilers and Interpreters, 3 rd ed. by Ronald Mak John Wiley & Sons, 2009.
13.13 Class Diagram Examples , cont’d The frontend , intermediate , and backend packages. Writing Compilers and Interpreters, 3 rd ed. by Ronald Mak John Wiley & Sons, 2009.
14.14 Class Diagram Examples , cont’d Implement the abstract base classes Parser and Scanner with language-specific subclasses . Writing Compilers and Interpreters, 3 rd ed. by Ronald Mak John Wiley & Sons, 2009.
15.15 Class Diagram Examples , cont’d The back end can be a CodeGenerator or an Executor . Writing Compilers and Interpreters, 3 rd ed. by Ronald Mak John Wiley & Sons, 2009.
16.UML State Diagram A state diagram depicts the various states that a single object may be in at run time and the transitions between those states. A state represents a stage in the runtime behavior pattern of the object. A state of an object is characterized by the values of its fields. A transition is triggered by an internal or external event on the object. 16
17.Basic State Diagram Symbols Represent a state with a rectangle. A simple state A state with internal activities. An arrow from one state to another represents a transition between states. A filled circle represents the object’s initial state . A filled circle nested inside another circle represents the object’s final state . There can be more than one final state. 17 https://www.smartdraw.com/state-diagram/
18.State Diagram Example: Undergraduate 18 https://www.lucidchart.com/pages/uml-state-machine-diagram State changes of an undergraduate.
19.19 State Diagram Example: College Class http://www.agilemodeling.com/artifacts/stateMachineDiagram.htm State changes of a college class.
20.20 State Diagram Example: Class During Enrollment http://www.agilemodeling.com/artifacts/stateMachineDiagram.htm State changes of a college class during the enrollment period.
21.21 State Diagram Example: Digital Clock State changes of a digital clock during time setting.
22.22 State Diagram Example: CPU https://cloud.smartdraw.com/editor.aspx?templateId=011d3688-733e-44de-9b6a-176d4ac950e3
23.Control Splits and Synchronization A short heavy bar called a fork represents a control split , with several transitions leaving it: A short heavy bar called a join represents a synchronization of control , where several concurrent transitions reduce back to one. 23 https://www.smartdraw.com/state-diagram/
24.24 State Diagram Example: Airline Passenger https://www.lucidchart.com/pages/uml-state-machine-diagram State changes of an airline passenger.
25.25 Design Goals for Good Classes Reliable Robust Flexible Coherent Loosely coupled Easy to use (by other programmers) Safe to use Easy to test Easy to extend Easy to maintain Good design is not easy!
26.26 A Proposed C++ Date Class Enable programs to manipulate dates. Example: // Construct a Date object to represent // the current date and time. Date *now = new Date(); // Print out the date such as // Tue Sep 11 14:50:10 PST 2018 cout << now-> date_string () << endl ;
27.27 Methods of the Date Class Date::after() and Date::before() are convenience member functions. Not necessary, but nice to have. A date value is represented as a scalar value. bool after(Date *other) Test if this date is after the specified date bool before(Date *other) Test if this date is before the specified date int compare_to (Date *other) Tell which date came before the other long get_time () Return milliseconds since the epoch (1970-01-01 00:00:00 GMT) void set_time (long n) Set the date to the given number of milliseconds since the epoch
28.28 Points in Time Object-Oriented Design & Patterns, 2 nd ed. by Cay Horstmann John Wiley & Sons, 2006.
29.29 The date_string Function The date_string () member function of the Date class is primarily for debugging purposes. It would be awkward to have to parse the string that it returns.