CMPE 135: Object-Oriented Analysis and Design September 12 Class Meeting Department of Computer Engineering San Jose State University Fall 2017 Instructor:

Slides:



Advertisements
Similar presentations
Unified Modeling Language
Advertisements

Behavioral Patterns C h a p t e r 5 – P a g e 128 BehavioralPatterns Design patterns that identify and realize common interactions between objects Chain.
Java Programming, Second Edition Chapter Four Advanced Object Concepts.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CS 46B: Introduction to Data Structures June 16 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Chapter 12: Internationalization Processing Date and Time Processing Date and Time  Locale  Date  TimeZone  Calendar and GregorianCalendar  DateFormat.
CSC 395 – Software Engineering Lecture 13: Object-Oriented Analysis –or– Let the Pain Begin (At Least I’m Honest!)
Review: Cohesion and Coupling, Mutable, Inheritance Screen Layouts Software methodologies – Extreme Programming Object-Oriented Design – CRC Cards - UML.
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
CS 153: Concepts of Compiler Design September 16 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
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 160: Software Engineering October 22 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
ITEC324 Principle of CS III Chapter 2 (Horstmann’s Book) – Part 1 The Object-Oriented Design Process Hwajung Lee.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CS 151: Object-Oriented Design September 10 Class Meeting
Chapter 3: Introducing the UML
Lect Chap 3 (cont.) 2/2. Chapter 3 The Importance of Encapsulation Even a simple class can benefit from different implementations Users are.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
CS 151: Object-Oriented Design September 17 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CS 160 and CMPE/SE 131 Software Engineering February 25 Class Meeting Department of Computer Science Department of Computer Engineering San José State.
Chapter 2 (Horstmann’s Book) – Part 1 The Object-Oriented Design Process Hwajung Lee.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Chapter 12 – Object-Oriented Design
CS 153: Concepts of Compiler Design September 14 Class Meeting
Cmpe 589 Spring 2006.
Unified Modeling Language Tutorial
CMPE 280 Web UI Design and Development August 29 Class Meeting
Unified Modeling Language
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
CMPE 135: Object-Oriented Analysis and Design September 26 Class Meeting Department of Computer Engineering San Jose State University Fall 2017 Instructor:
CMPE 135: Object-Oriented Analysis and Design October 3 Class Meeting
CS 153: Concepts of Compiler Design August 29 Class Meeting
CMPE 135: Object-Oriented Analysis and Design September 14 Class Meeting Department of Computer Engineering San Jose State University Fall 2017 Instructor:
CMPE 135: Object-Oriented Analysis and Design October 24 Class Meeting
Activity and State Transition Diagram
Review: Two Programming Paradigms
Object-Oriented Programming & Design Lecture 14 Martin van Bommel
CMPE 135: Object-Oriented Analysis and Design October 17 Class Meeting
CS 153: Concepts of Compiler Design November 30 Class Meeting
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
CMPE 152: Compiler Design February 6 Class Meeting
CMPE 152: Compiler Design September 13 Class Meeting
CMPE 152: Compiler Design October 4 Class Meeting
CMPE 152: Compiler Design September 11/13 Lab
CMPE 152: Compiler Design October 4 Class Meeting
CIS 375 Bruce R. Maxim UM-Dearborn
CMPE 152: Compiler Design August 23 Class Meeting
Copyright 2007 Oxford Consulting, Ltd
CMPE 135 Object-Oriented Analysis and Design March 21 Class Meeting
CMPE 152: Compiler Design January 29 Class Meeting
CMPE 152: Compiler Design February 7 Class Meeting
CMPE 152: Compiler Design February 21/26 Lab
CMPE 135: Object-Oriented Analysis and Design February 21 Class Meeting Department of Computer Engineering San Jose State University Spring 2019 Instructor:
CS 144 Advanced C++ Programming January 31 Class Meeting
CIS 375 Bruce R. Maxim UM-Dearborn
Chapter 13 Abstract Classes and Interfaces Part 01
CS 144 Advanced C++ Programming February 21 Class Meeting
CMPE 135 Object-Oriented Analysis and Design March 7 Class Meeting
CMPE 152: Compiler Design April 18 – 30 Labs
CMPE 152: Compiler Design March 19 Class Meeting
ITEC324 Principle of CS III
CS 151: Object-Oriented Design October 8 Class Meeting
ITEC324 Principle of CS III
CMPE 152: Compiler Design August 27 Class Meeting
CMPE/SE 131 Software Engineering February 22 Class Meeting
CMPE 152: Compiler Design September 17 Class Meeting
Presentation transcript:

CMPE 135: Object-Oriented Analysis and Design September 12 Class Meeting Department of Computer Engineering San Jose State University Fall 2017 Instructor: Ron Mak www.cs.sjsu.edu/~mak

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.

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.

Textual Analysis, cont’d Not all words are important in a document. Some nouns refer to entities that are outside of your system. 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?

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.

CRC Cards Class, Responsibility, Collaboration 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.

CRC Cards for the Dog Door Use Case Head First Object-Oriented Analysis & Design by Brett D. McLaughlin, et al. O’Reilly, 2006.

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

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[ ]

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 msgQueue : Message[ ] Mailbox Message msgQueue 1 * Replace the attribute with the association.

Class Diagram Examples What’s in the frontend package of a compiler? UML package diagram What information can you learn from these class diagrams? Access control + public – private # protected ~ package Writing Compilers and Interpreters, 3rd ed. by Ronald Mak John Wiley & Sons, 2009.

Class Diagram Examples, cont’d Message handling in the front end of a compiler. frontend and message packages Writing Compilers and Interpreters, 3rd ed. by Ronald Mak John Wiley & Sons, 2009.

Class Diagram Examples, cont’d The frontend, intermediate, and backend packages. Writing Compilers and Interpreters, 3rd ed. by Ronald Mak John Wiley & Sons, 2009.

Class Diagram Examples, cont’d Implement the abstract base classes Parser and Scanner with language-specific subclasses. Writing Compilers and Interpreters, 3rd ed. by Ronald Mak John Wiley & Sons, 2009.

Class Diagram Examples, cont’d The back end can be a CodeGenerator or an Executor. Writing Compilers and Interpreters, 3rd ed. by Ronald Mak John Wiley & Sons, 2009.

UML State Diagram A state diagram shows the behavior of classes in response to external stimuli. AKA statechart diagram A state diagram describes the behavior of a single object in response to a series of events in a system. At run time, the state of an object is characterized by the values of its fields. Different states can cause different behaviors.

Initial and Final States A filled circle represents the object’s initial state. A filled circle nested inside another circle represents the object’s final state.

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. https://www.smartdraw.com/state-diagram/

UML State Diagram Example #1

UML State Diagram Example #2

UML State Diagram Example #3

UML State Diagram Example #4

UML State Diagram Example #5

Design Goals for Good Classes Reliable Robust Flexible Coherent Easy to use (by other programmers) Safe to use Easy to test Easy to extend Easy to maintain

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 // Mon Feb 20 16:34:10 PST 2012 cout << now->date_string() << endl;

Methods of the Date Class 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 after() and before() are convenience member functions. Not necessary, but nice to have. A date value is represented as a scalar value.

Points in Time Object-Oriented Design & Patterns, 2nd ed. by Cay Horstmann John Wiley & Sons, 2006.

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.

What about Month, Day, and Year? The Date class can delegate to a Calendar class the task of converting the number of milliseconds since the epoch to year, month, day, hour, minute, and second fields. Example Calendar classes: GregorianCalendar LunarCalendar MayanCalendar etc.

Calendar Classes The specific Calendar class can be a changing part of an application’s design. Make Calendar an abstract base class for the GregorianCalendar class and any other calendar classes you may write: Date Calendar Gregorian Lunar

Some Functions of the Calendar Class int get(int field) Get a field value, where field is a Calendar class constant such as YEAR, MONTH, DATE, HOUR, MINUTE, SECOND. void set(int field, int value) Set a date field. void add(int field, int increment) Add to a field. Date get_time() Convert to a Date value. void set_time(Date *d) Convert from a Date value.

What’s Wrong with this Code? GregorianCalendar *g = new GregorianCalendar(); g->set(Calendar::YEAR, 2013); g->set(Calendar::MONTH, Calendar::SEPTEMBER); g->set(Calendar::DATE, 10); This code is permanently bound to the Gregorian calendar. What if you decide later to switch to the lunar calendar?

What’s Wrong with this Code? cont’d Instead, code to the interface. In this case, the interface is the abstract Calendar class. Calendar *cal = CalendarFactory.create(type); cal->set(Calendar::YEAR, 2013); cal->set(Calendar::MONTH, Calendar::SEPTEMBER); cal->set(Calendar::DATE, 10);

Factory Design Pattern Calendar *cal = CalendarFactory.create(type); cal->set(Calendar::YEAR, 2013); cal->set(Calendar::MONTH, Calendar::SEPTEMBER); cal->set(Calendar::DATE, 10); This code uses the factory method design pattern. public class CalendarFactory { public static Calendar *create(int type) if (type == GREGORIAN) { return new GregorianCalendar(); } else if (type == LUNAR) { return new LunarCalendar(); ...

Design a Day Class Answer questions such as How many days between now and the end of the year? What day is 100 days from now? A Day object represents a particular day. Some limitations Uses the Gregorian calendar only No time of day No time zone

Functions of the Day Class Day(int year, int month, int date) Constructor. int days_from(Day *d) Return the number of days between day d and the day represented by the object. The return value can be negative if day d comes before, or 0 if it’s the same day. Example: int n = today->days_from(birthday); Day add_days(int count) Return the day that is count days from the day represented by the object. The count value can be negative. Example: Day *later = holiday->add_days(5); int get_year() int get_month() int get_date() Getter methods.

The Day Class d->add_days(n)->days_from(d) equals n Add 3 (n) days to September 12 (d) to get September 15, which is 3 days from September 12. (d + n) - d == n d1->add_days(d2->days_from(d1)) equals d2 September 15 (d2) is 3 days from September 12 (d1). Add those 3 days to September 12 to get back to September 15. d1 + (d2 - d1) == d2

The Day Class Methods days_from() and add_days() are not trivial! April, June, September, November have 30 days. February has 28 days, except in leap years it has 29 days. All other months have 31 days. Leap years are divisible by 4, except after 1582, years divisible by 100 but not 400 are not leap years. There is no year 0; year 1 is preceded by year -1. In the switchover to the Gregorian calendar, ten days were dropped: October 15, 1582 is preceded by October 4.