CMPE 135: Object-Oriented Analysis and Design February 21 Class Meeting Department of Computer Engineering San Jose State University Spring 2019 Instructor:

Slides:



Advertisements
Similar presentations
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Advertisements

Chapter 8 Designing Classes. Assignment Chapter 9 Review Exercises (Written)  R8.1 – 8.3, 8.5 – 8.7, 8. 10, 8.11, 8.13, 8.15, 8.19, 8.20 Due Friday,
Chapter 3 (Horstmann’s Book) Guidelines for Class Design Hwajung Lee.
Road Map Introduction to object oriented programming. Classes
1 Class design guidelines. 2 Encapsulation Classes can be implemented many different ways –each has advantages & disadvantages –improvement/revision always.
1 CSC 222: Object-Oriented Programming Spring 2012 Object-oriented design  example: word frequencies w/ parallel lists  exception handling  System.out.format.
CS 46B: Introduction to Data Structures June 16 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
Low-Level Detailed Design SAD (Soft Arch Design) Mid-level Detailed Design Low-Level Detailed Design Design Finalization Design Document.
23-Oct-15 Abstract Data Types. 2 Data types A data type is characterized by: a set of values a data representation, which is common to all these values,
CS 151: Object-Oriented Design September 26 Class Meeting Department of Computer Science San Jose State University Fall 2013 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 12 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Object Oriented Software Development
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Chapter 3 Guidelines for Class Design. Objective of this chapter Have “bottom up point of view” Learn how to write a single class well.  The classes.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CS 151: Object-Oriented Design September 17 Class Meeting Department of Computer Science San Jose State University Fall 2013 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.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
ITEC324 Principle of CS III
Chapter 10 Thinking in Objects
Data Abstraction: The Walls
Classes and Objects: Encapsulation
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?
Chapter 10 Thinking in Objects
Objects as a programming concept
7. Inheritance and Polymorphism
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:
Chapter 10 Thinking in Objects
Review: Two Programming Paradigms
Chapter 3: Using Methods, Classes, and Objects
CMPE 135: Object-Oriented Analysis and Design October 17 Class Meeting
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
Array.
CMPE 152: Compiler Design February 6 Class Meeting
Classes and Objects 2nd Lecture
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Introduction to Linked Lists
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
CMSC 202 Composition.
Object Based Programming
CSC 480 Software Engineering
Chapter 9 Thinking in Objects
Object initialization: constructors
CMPE 152: Compiler Design October 4 Class Meeting
Constructors and Other Tools
CMPE 152: Compiler Design August 23 Class Meeting
Chapter 9 Thinking in Objects
Stacks Abstract Data Types (ADTs) Stacks
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
CMSC202 Computer Science II for Majors Lecture 07 – Classes and Objects (Continued) Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
CMPE 152: Compiler Design January 29 Class Meeting
CS2013 Lecture 7 John Hurley Cal State LA.
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
CS 144 Advanced C++ Programming February 21 Class Meeting
Classes and Objects Reusing Classes with Composition
CMPE 135 Object-Oriented Analysis and Design March 7 Class Meeting
slides created by Ethan Apter and Marty Stepp
ITEC324 Principle of CS III
CMPE 152: Compiler Design March 19 Class Meeting
Classes and Objects Systems Programming.
CS 240 – Advanced Programming Concepts
CMSC 202 Constructors Version 9/10.
Lecture 3 – Data collection List ADT
CMPE 152: Compiler Design August 27 Class Meeting
Presentation transcript:

CMPE 135: Object-Oriented Analysis and Design February 21 Class Meeting Department of Computer Engineering San Jose State University Spring 2019 Instructor: Ron Mak www.cs.sjsu.edu/~mak

const Fields and Immutability Another solution: Declare immutable fields of a class to be const. Example: class Employee { private: const string ssn; const Calendar *birthdate; public: string get_ssn() const { return ssn; } Calendar *get_birthdate() const { return birthdate; } }

const Fields, cont’d class Employee { private: const string ssn; const Calendar *birthdate; public: string get_ssn() const { return ssn; } Calendar *get_birthdate() const { return birthdate; } } The value of a const field cannot change after the object has been constructed. Field birthdate cannot be changed to refer to another birthdate object.

const Fields, cont’d class Employee { private: const string ssn; const Calendar *birthdate; public: string get_ssn() const { return ssn; } Calendar *get_birthdate() const { return birthdate; } } Advantage: It’s a compile-time error if you forget to initialize a const field. Disadvantage: You cannot assign the value of a const field to a variable of the same type.

Separate Accessors and Mutators Separate the roles of accessors and mutators. If we call a function to access an object, we don’t expect the object to mutate. Example of a violation:  Method next() returns the current token and advances the cursor of the scanner object. It’s both an accessor and a mutator. What if you want to read the current token again? Scanner *in = . . .; string s = in->next();

Separate Accessors and Mutators, cont’d Solution: Use separate accessor and mutator functions. Rule of thumb: Mutator methods should return void. string get_current(); // get the current token void next(); // advance to the next token

Separate Accessors and Mutators, cont’d Refined rule of thumb: A mutator method can return a value as a convenience, provided there is an accessor method that returns the same value without changing the object’s state. string get_current(); string next(); // also returns the current token // for your convenience

Side Effects A side effect is a change to an object’s state due to a method call. Nasty side effects are unexpected by the programmer.

Nasty Side Effect Examples Calling a getter function changes the value of some object field. The dangerous setter functions of our original Day class. A function call changes the value of an actual parameter that’s passed by the call. A function call changes a global value, such as a static object.

Example Side Effect Basic date string parser: Advanced: Parse multiple dates in the string. Side effect: Function parse() updates parameter index to the string index of the next date. A better design: Add an index field to the date parser state. DateParser *date_parser = new DateParser(); string date_string = "February 21, 2019"; Date *d = date_parser->parse(date_string); int index = 0; Date *d = date_parser->parse(date_string, index);

The Law of Demeter “Principle of Least Knowledge” More a rule of thumb than a hard law. Summary (https://en.wikipedia.org/wiki/Law_of_Demeter) Each unit should have only limited knowledge about other units: only units “closely” related to the current unit. Each unit should only talk to its friends. Don't talk to strangers. Only talk to your immediate friends.

The Law of Demeter, cont’d A member function should only use: Member variables of its class Parameters Objects that it constructs with new To obey this law: A method should never return a reference to an object that is part of its internal representation. Return a copy instead. A class should have sole responsibility to interact with objects that are part of its internal representation.

The Law of Demeter, cont’d The law enables you to modify the internal structure of a class without modifying its public interface. Encapsulation! Loose coupling!

How Good is an Interface? Who is the user of a class that you write? Other programmers? Perhaps you yourself, later!

How Good is an Interface? cont’d Class designer priorities Efficient algorithm Convenient coding etc. Class user priorities Easy to use Don’t have to understand the implementation

How Good is an Interface? cont’d Is there a “conflict of interest” if you’re both the class designer and the class user? Can you make the right engineering tradeoffs?

Cohesion A cohesive class implements a single abstraction or responsibility. Member functions should be related to this single abstraction.

Cohesion Why is this a badly designed class? Method processCommand() doesn’t belong. Delegate command processing to another class. class Mailbox { public: void add_message(Message *msg); Message *get_current_message(); Message *remove_current_message(); void process_command(string command); ... }

Completeness Support operations for a well-defined abstraction. A potentially bad example: The Date class: How many milliseconds have elapsed? No such operation in the Date class. Does it fall outside the responsibility? We have before(), after(), get_time() Date start = new Date(); // do some work Date end = new Date();

Completeness If you encounter an incomplete class: Negotiate with the class designer. Fill in what’s missing with a subclass.

Convenience A good interface makes all tasks possible and common tasks simple. Example of inconvenience: The C++ string class Why are stoi, stof, to_string, etc. individual functions and not member functions of class string? To shift all the letters of a string to upper or lower case, you need to call the complicated transform function. How to do a case-insensitive string comparison?

Clarity Confused programmers write buggy code. Example: The C++ list class has many ways to insert elements into a list. Confusing. http://www.cplusplus.com/reference/list/list/insert/

Clarity, cont’d Another example: The C++ vector class A vector can be indexed using an integer value enclosed in [ and ] or by calling member function at with an integer value. But other member functions such as insert() and erase() require iterators to indicate positions within the vector.

Consistency Related features of a class should have matching names parameters return values behavior A bad example: Why is month 0-based? new GregorianCalendar(year, month - 1, day)

Assignment #3 Implement the first version of the Rock-Paper-Scissors game. Each game has 20 rounds. Prompt the human player for each round’s choice. The computer makes a random choice. What can change in the future? How the opposing (i.e., human) player’s choices are obtained. How the computer makes its choices (it may not always be random).

Assignment #3, cont’d In a short (2- or 3-page) report, describe: How you encapsulated code that will change. How you used the Law of Demeter. Examples of cohesive classes. Examples of loosely-coupled classes. Due Friday, March 1

Programming by Contract Metaphor promoted by Bertrand Meyer. A pioneer of object-oriented programming. Inventor of the Eiffel programming language. Ensure that: All class constructors only create objects with valid initial states. All mutators preserve the valid states.

Programming by Contract, cont’d Therefore: We should never have invalid objects. No need to waste run time checking for invalid objects.

Programming by Contract, cont’d Member functions are agents that fulfill a contract. The contract spells out the responsibilities Of the caller (the class user) Of the implementer (you, the programmer) Involves preconditions, postconditions, and invariants.

Preconditions class MessageQueue { public: void add(Message *msg); Message *remove(); Message *peek(); int size(); ... private: vector<Message *> elements; } Class MessageQueue can declare this to be a runtime error. Class MessageQueue can simply return null. Which is better? What should happen if the class user attempts to remove a message from an empty queue?

Preconditions, cont’d Excessive error checking is costly. Returning dummy values can complicate testing.

Preconditions, cont’d A precondition is a condition that must be true before the service provider can promise to fulfill its part of the contract. If the precondition is not true and the service is still requested, the provider can choose any action that is convenient for it, no matter how disastrous the outcome may be for the service requester.

Contract Metaphor The service provider (i.e., the class) must specify preconditions. If the precondition is true, the service provider must work correctly. If the precondition is not true, the service provider can do anything. Throw an exception? Return a default or false answer? Corrupt data? Handle the error gracefully?

Preconditions, cont’d What happens if the precondition not fulfilled? /** * Remove the message at the head. * @return the message at the head * @precondition size() > 0 */ Message *MessageQueue::remove() { Message *r = elements[0]; elements->erase(0); return r; } What happens if the precondition not fulfilled? remove() makes no promises to do anything sensible if called on an empty queue.

Inefficient Queue Implementation The current MessageQueue implementation removes messages inefficiently. After the head message (element 0) is removed, all the remaining messages must shift one position:

Queue as a Circular Array Implement a queue as a circular array. Two index variables, head and tail head: Index of the next message to be removed tail: Index of the position where the next new message will be inserted.

Queue as a Circular Array As messages are added and removed: head will chase after tail. Both will wrap around to the beginning of the array (hence the name “circular”).

Queue as a Circular Array Indexes head and tail must never cross each other. Can head and tail be equal? What does that mean?

Queue as a Circular Array /** * Remove message at head. * @return the message that was removed from the queue * @precondition size() > 0 */ Message *MessageQueue::remove() { Message *r = elements[head]; head = (head + 1) % elements->capacity(); count--; return r; } What if the precondition is violated? count becomes negative. After head has wrapped around, remove() returns a previously removed message. The cost of violating a precondition can be very high!