Guidelines for class design Horstmann ch. 3.2-3.6 Noter ch.1.1.

Slides:



Advertisements
Similar presentations
Chapter 17 Failures and exceptions. This chapter discusses n Failure. n The meaning of system failure. n Causes of failure. n Handling failure. n Exception.
Advertisements

Chapter 23 Organizing list implementations. This chapter discusses n The notion of an iterator. n The standard Java library interface Collection, and.
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Singleton vs utility class  at first glance, the singleton pattern does not seem to offer any advantages to using a utility class  i.e., a utility class.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
 Both System.out and System.err are streams—a sequence of bytes.  System.out (the standard output stream) displays output  System.err (the standard.
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
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.
CSM-Java Programming-I Spring,2005 Class Design Lesson - 4.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
1 TCSS 360, Spring 2005 Lecture Notes Programming by Contract: Pre/Postconditions, Invariants and Assertions Relevant Reading: Object-Oriented Design and.
Class Design CSC 171 FALL 2004 LECTURE 11. READING Read Chapter 7 It’s abstract But it should help with project #1.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
1 Class design guidelines. 2 Encapsulation Classes can be implemented many different ways –each has advantages & disadvantages –improvement/revision always.
Fall 2007CS 2251 Queues Chapter 6. Fall 2007CS 2252 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Odds and Ends Strings (from Chapter 9) StringTokenizer.
Advanced Object Oriented Programming Chapter 3 & 4.
CPSC 2100 University of Tennessee at Chattanooga – Fall 2013 Object-Oriented Design & Patterns 2 nd edition Cay S. Horstmann Chapter 3: Guidelines for.
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
Ranga Rodrigo. Class is central to object oriented programming.
Chapter 3 Introduction to Collections – Stacks Modified
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
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,
Chapter 18 Java Collections Framework
Fall 2007CS 2251 Queues Chapter 6. Fall 2007CS 2252 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in.
LinkedList Many slides from Horstmann modified by Dr V.
CSC 142 D 1 CSC 142 Instance methods [Reading: chapter 4]
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
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
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.
18-1 Queues Data Structures and Design with Java and JUnit © Rick Mercer.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
Object-Oriented Principles Applications to Programming.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
SWE 4743 Abstract Data Types Richard Gesick. SWE Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.
Lect Chap 3 (cont.) 2/2. Chapter 3 The Importance of Encapsulation Even a simple class can benefit from different implementations Users are.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
Iteration Abstraction SWE Software Construction Fall 2009.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
CS 151: Object-Oriented Design September 17 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
CS/ENGRD 2110 FALL 2013 Lecture 3: Fields, getters and setters, constructors, testing 1.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
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.
Reasoning and Design (and Assertions). How to Design Your Code The hard way: Just start coding. When something doesn’t work, code some more! The easier.
Today Encapsulation. Build a fully encapsulated Halloween class, going from Halloween1 to Halloween6 (eventually!): –The final version will have overloaded.
ENCAPSULATION. WHY ENCAPSULATE? So far, the objects we have designed have all of their methods and variables visible to any part of the program that has.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 17 – Specifications, error checking & assert.
Linked Data Structures
ITEC324 Principle of CS III
Guidelines for Class Design
Classes and Objects: Encapsulation
CMPE 135: Object-Oriented Analysis and Design September 14 Class Meeting Department of Computer Engineering San Jose State University Fall 2017 Instructor:
Classes and Objects 2nd Lecture
Design by Contract Fall 2016 Version.
Classes and Objects: Encapsulation
CSC 480 Software Engineering
Classes and Objects Encapsulation
ArrayLists 22-Feb-19.
CSC 143 Java Linked Lists.
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
CMPE 135: Object-Oriented Analysis and Design February 21 Class Meeting Department of Computer Engineering San Jose State University Spring 2019 Instructor:
CMSC 202 Encapsulation Version 9/10.
ITEC324 Principle of CS III
Presentation transcript:

Guidelines for class design Horstmann ch Noter ch.1.1

Guidelines for class design Encapsulation Programming by contract Quality of class interface

Encapsulation no public instance variables accessors and mutators

One class – several implementations Example Day class –Day encapsulates a day in a fixed location –Use Gregorian calendar Answer questions such as –How many days are there between now and the end of the year? –What day is 100 days from now? Class Day: Custom class, for teaching/learning purpose Use the standard library classes, not this class, in your own programs

Designing a Day class Constructor Day(int year, int month, int date) Accessors getYear, getMonth, getDate Other methods –daysFrom computes number of days between two days: int n = today.daysFrom(birthday); –addDays computes a day that is some days away from a given day: Day later = today.addDays(999);

Day class: Implementation 1 Straightforward implementation: private int year private int month private int date addDays/daysFrom tedious to implement –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

Day class: Implementation 2 For greater efficiency, use Julian day number –Used in astronomy –Number of days since Jan. 1, 4713 BCE –May 23, 1968 = Julian Day 2,440,000 private int julian; Greatly simplifies date arithmetic public int daysFrom(Day other) { return julian - other.julian; } But constructor and accessor inefficient

No public instance variables Public instance variables would have blocked change of implementation –Can't just use text editor to replace all d.year with d.getYear() –How about d.year++? d = new Day(d.getYear() + 1, d.getMonth(), d.getDay()) –Ugh--that gets really inefficient in Julian representation Don't use public fields, even for "simple" classes

QUIZ In which lines should visibility modifier be changed? 1.none 2.a 3.b 4.c 5.a+b 6.a+c 7.b+c 8.a+b+c 9.I don’t know public class Measures { a) public static final int SIZE = 3; b) public int length = 2; c) static int height = 4; }

Encapsulation no public instance variables accessors and mutators

Accessors and Mutators Accessor: Reads object state without changing it Mutator: Changes object state Class without mutators is immutable String is immutable

Don't Supply a Mutator for every Accessor Day has getYear, getMonth, getDate accessors Day does not have setYear, setMonth, setDate mutators These mutators would not work well –Example: Day deadline = new Day(2004, 1, 31); deadline.setMonth(2); // ERROR deadline.setDate(28); –Maybe we should call setDate first? Day deadline = new Day(2004, 2, 28); deadline.setDate(31); // ERROR deadline.setMonth(3); Immutability is useful

Sharing Mutable References References to immutable objects can be freely shared Don't share mutable references Example class Employee {... private Date hireDate; public Date getHireDate() { return hireDate; } } Pitfall: Employee harry =...; Date d = harry.getHireDate(); d.setTime(t); // changes Harry's state!!! Remedy: Use clone public Date getHireDate() { return (Date)hireDate.clone(); }

Separating Accessors and Mutators If we call a method to access an object, we don't expect the object to mutate Rule of thumb: Mutators should return void Example of violation: StringTokenizer t =...; String s = t.nextToken(); Yields current token and advances iteration What if I want to read the current token again?

Better interface: String getToken(); void nextToken(); Even more convenient: String getToken(); String nextToken(); // returns current Refine rule of thumb: Mutators can return a convenience value, provided there is also an accessor to get the same value

Side effects Accessor: no change to object Mutator: changes object state Side effect: change to another object –Parameter variable public void print(Employee e) {... e.getHireDate().setTime(t);... } Avoid side effects--they confuse users Good example, no side effect: a.addAll(b) mutates a but not b

QUIZ Which methods/constructors need be removed to make Bucket immutable? 1.none 2.a 3.b 4.c 5.a+b 6.a+c 7.b+c 8.a+b+c 9.I don’t know public class Bucket { private int size = 1; public Bucket(int s) {size=s;} public Bucket() {size++;} public void enlarge() { size++; } public Bucket enlarge() { return new Bucket(size+1); } a) c) b)

Programming by Contract Example: Queue Precondition Assertion Postcondition Implementation Invariant

Queue People waiting to be served in a cafeteria Processes waiting to get CPU-time on a computer food in a supermarket (at least the shop owner wants the customer to take the old stuff first)

Queue a queue consists of some objects placed one after the other (ordered). a new object may be placed in the rear, behind all the others the object in the front may be removed FIFO = First In First Out

Queue implementation 1 using ArrayList public class Queue { public Queue() { elements = new ArrayList (); } public void add(T x) { elements.add(x); } public T remove() { return elements.remove(0); } public T peek() { return elements.get(0); } public int size() { return elements.size(); } private ArrayList elements; }

Queue implementation 2 using Circular Array Circular: head, tail indexes wrap around Avoids inefficient shifting of elements

Programming by Contract Example: Queue Precondition Assertion Postcondition Implementation Invariant

Programming by contracts: Preconditions Caller attempts to remove element from empty Queue What should happen? –Queue can declare this as an error –Queue can tolerate call and return dummy value What is better? –Excessive error checking is costly –Returning dummy values can complicate testing Contract metaphor –Service provider must specify preconditions –If precondition is fulfilled, service provider must work correctly –Otherwise, service provider can do anything When precondition fails, service provider may –throw exception –return false answer –corrupt data

Precondition Precondition for remove: size() > 0 Queue implementation 1 using ArrayList: public T remove() { return elements.remove(0); } What happens if precondition not fulfilled? IndexOutOfBoundsException

Precondition Precondition for remove: size() > 0 Queue implementation 2 using circular array: public T remove() { T r = elements[head]; head = (head + 1) % elements.length; count--; return r; } What happens if precondition not fulfilled? Queue is corrupted

Exceptions in the Contract IllegalArgumentException if queue is empty */ public Message remove() { if (size() == 0) throw new IllegalArgumentException();... } Exception throw part of the contract Caller can rely on behavior Exception throw not result of precondition violation This method has no precondition

QUIZ For which precondition(s) does method peek satisfy its contract? 1.Any (no precondition needed) 2.PRECOND: size()>0; 3.PRECOND: add(…) has been called earlier 4.PRECOND: remove() has never been called 5.Some other distinct precondition 6.None (method is wrong for all preconditions) 7.I don’t know Public class Queue { private ArrayList elements; … element at head of queue */ public T peek() { return elements.get(0); }

Programming by Contract Example: Queue Precondition Assertion Postcondition Implementation Invariant

Assertions Syntax: assert condition; assert condition : explanation; Throws AssertionError if condition false and checking enabled Enable assertion checking during testing with java -ea MyProg Useful for warning programmers about precondition failure

Assertion public Message remove() { assert size() > 0 : "violated precondition size() > 0";... }

Programming by Contract Example: Queue Precondition Assertion Postcondition Implementation Invariant

Postconditions Conditions that the service provider guarantees Example: add size() > 0 Postcondition of one call can imply precondition of another: q.add(m1); m2 = q.remove();

Pre- and post-conditions n n >= integer squareroot a of a*a <= n && n < (a+1)*(a+1) */ public int intSquareRoot(int n) { assert n >= 0; int a = n; int b = 1; while (a>b) { a = (a+b)/2; b = n/a; } assert (a*a <= n && n < (a+1)*(a+1)); return a; } responsibility of caller to ensure precondition responsibility of implementor to ensure postcondition

QUIZ Which code at ? 1.A 2.B 3.C 4.D 5.None of A-D 6.Several of A-D boolean result = true; for (int i=1; i<list.size(); i++) if (list.get(i-1)>list.get(i)) result = false; else result = true; return result; for (int i=1; i<list.size(); i++) if (list.get(i-1)>list.get(i)) return false; else return true; for (int i=1; i<list.size(); i++) if (list.get(i-1)>list.get(i)) return false; return true; for (int i=0; i<list.size(); i++) if (list.get(i)>list.get(i+1)) return false; return true; list of true precisely when list is sorted (in increasing order) */ public boolean sorted(List list) { } A B C D Postcondition! 7. I don’t know

Programming by Contract Example: Queue Precondition Assertion Postcondition Implementation Invariant

Condition that –involves details of particular implementation –is true after every constructor –is preserved by every method (if it's true before the call, it's again true afterwards) example: Box

Implementation invariant: class Box Methods for class Box: –fitsIn : Decide whether box fits within another box under 90 deg rotations –combine : Compute minimum enclosing box of this box and another box

Implementation invariant: class Box /** IMPLEMENTATION INVARIANT: x <= y <= z */ private int x,y,z; Invariant true after constructor public Box(int w, int h, int d) { x = w; y = h; z = d; while (x>y || y>z) { if (x>y) { int temp = x; x = y; y = temp; } if (y>z) { int temp = y; y = z; z = temp; } } sort dimensions to satisfy invariant represents height, width and depth

Implementation invariant: class Box Implementation invariant is true before method call –simplifies implementation public boolean fitsIn(Box b) { return ( x<=b.x && y<=b.y && z<=b.z ); } public Box combine(Box b) { return new Box(Math.max(x,b.x), Math.max(y,b.y), Math.max(z,b.z)); }

Implementation invariant: class Box Without implementation invariant: –Complicated implementation necessary –more error prone public boolean fitsIn(Box b) { return ( (x<=b.x && y<=b.y && z<=b.z) || (x<=b.x && z<=b.y && y<=b.z) || (y<=b.x && x<=b.y && z<=b.z) || (y<=b.x && z<=b.y && x<=b.z) || (z<=b.x && x<=b.y && y<=b.z) || (z<=b.x && y<=b.y && x<=b.z) ); }

QUIZ Which methods satisfy the invariant? 1.a and b 2.a only 3.b only 4.Neither 5.I don’t know public class Time { //INVARIANT 0 <= hour < 24 private int hour; public Time(int h) {hour = h;} public void increment() { hour = (hour+1) % 24; } public void increment2() { if (hour==23) hour = 0; else hour++; } a) b)

QUIZ Where should asserts be used to verify the invariant?? 1.A,B, C,D, E,F,G 2.A,B, C,D, E,G 3.B,D,F 4.B,D,G 5.A,B, C,D 6.B,D 7.Some other combination 8.I don’t know public class Time { //INVARIANT 0 <= hour < 24 private int hour; public Time(int h) { hour = h; } public void increment() { hour = (hour+1) % 24; } public int getMinutes() { int res = 60*hour; return result; } A F E G D C B

Quality of Class Interface Cohesion Completeness Convenience Clarity Consistency

Cohesion Class describes a single abstraction Methods should be related to the single abstraction Bad example: public class Mailbox { public addMessage(Message aMessage) {... } public Message getCurrentMessage() {... } public Message removeCurrentMessage() {... } public void processCommand(String command) {... }... } processCommand does not belong here

Completeness Support operations that are well-defined on abstraction Potentially bad example: Date Date start = new Date(); // do some work Date end = new Date(); How many milliseconds have elapsed? No such operation in Date class Does it fall outside the responsibility? After all, we have before, after, getTime

Convenience A good interface makes all tasks possible... and common tasks simple Bad example: Reading from System.in Why doesn't System.in have a readLine method? After all, System.out has println.

Clarity Confused programmers write buggy code Bad example: Removing elements from LinkedList Reminder: Standard linked list class LinkedList countries = new LinkedList (); countries.add("A"); countries.add("B"); countries.add("C"); Iterate through list: ListIterator iterator = countries.listIterator(); while (iterator.hasNext()) System.out.println(iterator.next());

Clarity Iterator between elements Like blinking caret in word processor add adds to the left of iterator (like word processor): Add X before B: ListIterator iterator = countries.listIterator(); // |ABC iterator.next(); // A|BC iterator.add("France"); // AX|BC To remove first two elements, you can't just "backspace" remove does not remove element to the left of iterator From API documentation: Removes from the list the last element that was returned by next or previous. This call can only be made once per call to next or previous. It can be made only if add has not been called after the last call to next or previous. Not clear

Consistency Related features of a class should have matching –names –parameters –return values –behavior Bad example: –String class s.equals(t) / s.equalsIgnoreCase(t) –But boolean regionMatches (int toffset, String other, int ooffset, int len) boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) –Why not regionMatchesIgnoreCase ?