1 Class design guidelines. 2 Encapsulation Classes can be implemented many different ways –each has advantages & disadvantages –improvement/revision always.

Slides:



Advertisements
Similar presentations
Exceptions CSE301 University of Sunderland Harry Erwin, PhD.
Advertisements

11-Jun-14 The assert statement. 2 About the assert statement The purpose of the assert statement is to give you a way to catch program errors early The.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Designing Classes Chapter 8. Classes Collection of objects Objects are not actions Class names – Nouns Method names – Verbs What Makes a Good Class Represent.
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 Goals To learn how to choose appropriate classes to implement
Chapter 3 (Horstmann’s Book) Guidelines for Class Design Hwajung Lee.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Software Testing and Quality Assurance
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
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.
Information Hiding and Encapsulation
Chapter 1 Principles of Programming and Software Engineering.
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.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
CPSC 2100 University of Tennessee at Chattanooga – Fall 2013 Object-Oriented Design & Patterns 2 nd edition Cay S. Horstmann Chapter 3: Guidelines for.
Computer Science 340 Software Design & Testing Design By Contract.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
07 Coding Conventions. 2 Demonstrate Developing Local Variables Describe Separating Public and Private Members during Declaration Explore Using System.exit.
SWE 619 © Paul Ammann Procedural Abstraction and Design by Contract Paul Ammann Information & Software Engineering SWE 619 Software Construction cs.gmu.edu/~pammann/
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,
CMSC 202 Classes and Objects: Reusing Classes with Composition.
Guidelines for class design Horstmann ch Noter ch.1.1.
(c) University of Washington01-1 CSC 143 Java Programming as Modeling Reading: Ch. 1-6.
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
Linked List. Iterators Operation to find a link, deleting, and inserting before or after a specified link, also involve searching through the list to.
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.
CMSC 202 Classes and Objects: Reusing Classes with Composition.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
© 2006 Pearson Addison-Wesley. All rights reserved 2-1 Chapter 2 Principles of Programming & Software Engineering.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
SWE 4743 Abstract Data Types Richard Gesick. SWE Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.
CSE 143 Lecture 4 More ArrayIntList : Pre/postconditions; exceptions; testing reading: slides created by Marty Stepp and Hélène Martin
Building Java Programs Chapter 15 Lecture 15-2: testing ArrayIntList; pre/post conditions and exceptions reading:
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.
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
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.
COMP Information Hiding and Encapsulation Yi Hong June 03, 2015.
OOP Details Constructors, copies, access. Initialize Fields are not initialized by default:
Principles of Programming & Software Engineering
ITEC324 Principle of CS III
Guidelines for Class Design
External Scope CECS 277 Mimi Opkins.
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:
Chapter 3: Using Methods, Classes, and Objects
Classes and Objects 2nd Lecture
Design by Contract Fall 2016 Version.
Computer Science II Exam 1 Review.
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.
Classes and Objects: Encapsulation
CSC 480 Software Engineering
Classes and Objects Encapsulation
Object initialization: constructors
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:
Computer Science 340 Software Design & Testing
CMSC 202 Encapsulation Version 9/10.
ITEC324 Principle of CS III
Review for Midterm 3.
Classes and Objects Systems Programming.
Presentation transcript:

1 Class design guidelines

2 Encapsulation Classes can be implemented many different ways –each has advantages & disadvantages –improvement/revision always a possibility Encapsulation facilitates change in implementation without affecting client code

3 Information Hiding Public interface of object & private representation of object usually kept distinct –allows certain aspects of object to be hidden from view –distinguishes ability to perform a task from specific steps needed to do so –public interface of object consists of operations other objects can request the object to perform

4 Why private data fields? Public instance fields box us in –anything public can be, and probably will be, used by client programmers –any changes made to class must accommodate this dependency For example, if the Day class had public members representing month, day and year, a switch to julian representation would involve either invalidating this code or significant (and cumbersome) redundancy

5 The Importance of Encapsulation  Even a simple class can benefit from different implementations  Users are unaware of implementation  Public instance variables would have blocked improvement in Day class

6 Day class example - public data fields year, day, month  Suppose we switch to julian representation  Can't just wave magic wand to replace all d.year with d.getYear() in client code  How about d.year++? d = new Day(d.getDay(), d.getMonth(), d.getYear() + 1)  Gets really inefficient in Julian representation  Don't use public fields, even for "simple" classes

7 Advantages of encapsulation Restricts range of program affected by any change to the methods of the class Allows for software evolution over time without useless baggage carried into next program version

8 Accessors & mutators Accessor methods report but do not change object state Mutator methods modify object state Classes without mutators are immutable –String class is an example; can assign a new object reference to a String variable, but can’t make changes to the object

9 Don't Supply a Mutator for every Accessor  Day has getYear, getDate and getMonth accessors, but does not have setYear, setMonth,setDate mutators  These mutators would not work well; for example: Day deadline = new Day(2001, 1, 31); deadline.setMonth(2); // ERROR deadline.setDate(28);

10 Example continues  Maybe we should call setDate first? Day d = new Day(2001, 2, 28); d.setDate(31); // ERROR d.setMonth(3);  GregorianCalendar implements confusing rollover - silently gets the wrong result instead of error:  suppose date initially set to January 31, then month set to February  instead of error message, get date set to March 3

11 Sharing Mutable References  References to immutable objects can be freely shared  Don't share mutable references: example class Employee {... public String getName() { return name; } public double getSalary() { return salary; } public Date getHireDate() { return hireDate; } // note: Date objects are mutable... }

12 Example continued Pitfall: Employee harry =...; Date d = harry.getHireDate(); d.setTime(t); // changes Harry's state Remedy: Use clone() public Date getHireDate() { return (Date)hireDate.clone(); }

13 Final Instance Fields  Good idea to mark immutable instance fields as final: private final int day ;  final object reference can still refer to mutating object: private final ArrayList elements;  elements can't refer to another array list  The contents of the array list can change

14 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 (from Java API): StringTokenizer t =...; String s = t.nextToken(); Yields current token and advances iteration - what if I want to read the current token again?

15 Separating Accessors and Mutators 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

16 Side Effects Accessor: no change to object Mutator: changes object state Side effect: change to another object –Parameter variable –static object Avoid side effects--they confuse users

17 Side Effects Avoid modifying static objects Example: don't print error messages to System.out: if (newMessages.isFull()) System.out.println("Sorry--no space"); Your classes may need to run in an environment without System.out; throw exception instead

18 Law of Demeter A method should only use objects that are –instance fields of its class –parameters –objects that it constructs with new Shouldn't use an object that is returned from a method call A method should not ask another object to give it part of its internal state to work on

19 Analyzing Quality of Class Interface Customers: Programmers using the class Criteria: –Cohesion –Completeness –Convenience –Clarity –Consistency Engineering activity: make tradeoffs

20 Cohesion  Class describes a single abstraction  Methods should be related to the single abstraction  If a class has unrelated responsibilities, split it up

21 Completeness  Support all 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?

22 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 analogous to System.out.println?  Why can't I make a BufferedReader from an input stream when I can make a PrintWriter from an output stream?

23 Clarity  Confused programmers write buggy code  Bad example: Removing elements from LinkedList: 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());

24 Bad example continued  Iterator between elements, like blinking caret in word processor; add() adds to the left of iterator (like word processor): ListIterator iterator = countries.listIterator(); // |ABC iterator.next(); // A|BC iterator.add("X"); // AX|BC  To remove first two elements, you can't just "backspace"

25 Bad example continued  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.  Huh?

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

27 Programming by Contract  Spell out responsibilities:  of caller  of implementor  Increase reliability  Increase efficiency

28 Preconditions 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

29 Preconditions  When precondition fails, service provider may  throw exception  return false answer  corrupt data

30 Assertions  Mechanism for warning programmers; can be turned off after testing  Syntax: assert condition; //note: no parentheses assert condition : explanation;  Throws AssertionError if condition false and checking enabled During testing, run with java -enableassertions MyProg

31 Exceptions in the contract  Exception throw part of the contract  Caller can rely on behavior  Exception throw not result of precondition violation  This method has no precondition

32 Example IllegalArgumentException if queue is empty */ public Message removeFirst() { if (count == 0) throw new IllegalArgumentException(); Message r = elements[head];... }

33 Postconditions  Conditions that the service provider guarantees  Every method promises  Sometimes, can assert additional useful condition

34 Class Invariants Condition that is –true after every constructor (no invalid objects can be created) –preserved by every method (if it's true before the call, it's again true afterwards) –Useful for checking validity of operations

35 Class invariants As long as instance fields are private, you have complete control over object modification Can guarantee certain values are within legal range, certain references are not null Invariant is appropriate tool for documenting such a guarantee

36 Interface vs. Implementation Invariants Interface invariants are conditions that involve only public interface of class –of interest to class user –give behavior guarantee for class objects Implementation invariants involve details of particular implementation –of interest to class implementor –can be used to ensure implementation correctness