Object-Oriented Analysis and Design Lecture 8 State Space, Behavior, and Type Conformance.

Slides:



Advertisements
Similar presentations
Design by Contract.
Advertisements

Giving a formal meaning to “Specialization” In these note we try to give a formal meaning to specifications, implementations, their comparisons. We define.
The Substitution Principle SWE 332 – Fall Liskov Substitution Principle In any client code, if subtype object is substituted for supertype object,
Design by Contract. Design by contract is the process of developing software based on the notion of contracts between objects, which are expressed as.
Solutions to Review Questions. 4.1 Define object, class and instance. The UML Glossary gives these definitions: Object: an instance of a class. Class:
5/17/2015 OO Design: Liskov Substitution Principle 1.
1 Design by Contract Building Reliable Software. 2 Software Correctness Correctness is a relative notion  A program is correct with respect to its specification.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Object Oriented Design An object combines data and operations on that data (object is an instance of class) data: class variables operations: methods Three.
1 Inheritance. 2 One class inherits from another if it describes a specialized subset of objects Terminology: inheritschild class subclass –the class.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Software Testing and Quality Assurance
Inheritance (notes for 10/26 lecture). Inheritance Inheritance is the last of the relationships we will study this semester. Inheritance is (syntactically)
HST 952 Computing for Biomedical Scientists Lecture 2.
Karlstad University Computer Science Design Contracts and Error Management Design Contracts and Errors A Software Development Strategy (anpassad för PUMA)
Software Testing and Quality Assurance
Copyright 2004 Prentice-Hall, Inc. Essentials of Systems Analysis and Design Second Edition Joseph S. Valacich Joey F. George Jeffrey A. Hoffer Appendix.
Describing Syntax and Semantics
Eiffel Language and Design by Contract Contract –An agreement between the client and the supplier Characteristics –Expects some benefits and is prepared.
Chapter 10 Classes Continued
Modelling classes Drawing a Class Diagram. Class diagram First pick the classes –Choose relevant nouns, which have attributes and operations. Find the.
Implementation classes and developing relational databases IS Development Lecture 9.
Subclasses and Subtypes CMPS Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning.
January 27, 2002 ECEN5033 University of Colorado -- Class Testing 1 Specifying interactions Remainder of slides assume Operations defined by a class are.
Computer Science 340 Software Design & Testing Design By Contract.
Ranga Rodrigo. Class is central to object oriented programming.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
1 Exercise /* A lockbox can be open or closed. If closed, only a valid password will open the box. Once the box is open, the contents can be retrieved.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Copyright 2001 Prentice-Hall, Inc. Essentials of Systems Analysis and Design Joseph S. Valacich Joey F. George Jeffrey A. Hoffer Appendix A Object-Oriented.
CSC 211 Introduction to Design Patterns. Intro to the course Syllabus About the textbook – Read the introduction and Chapter 1 Good attendance is the.
Type Conformance Contravariance, Covariance & Closed behavior CSC 422 Dr. Spiegel.
Design by Contract in Java Concept and Comparison.
111 Protocols CS 4311 Wirfs Brock et al., Designing Object-Oriented Software, Prentice Hall, (Chapter 8) Meyer, B., Applying design by contract,
Low-Level Detailed Design SAD (Soft Arch Design) Mid-level Detailed Design Low-Level Detailed Design Design Finalization Design Document.
Class Design III: Advanced Inheritance Additional References “Object-Oriented Software Development Using Java”, Xiaoping Jia, Addison Wesley, 2002 “Core.
CS 261 – Data Structures Preconditions, Postconditions & Assert.
OO as a language for acm l OO phrase l Mental model of key concepts.
Design by Contract Ranga Rodrigo based on Mark Priestley's Lectures.
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 17: Inheritance & Behavioral.
Type Abstraction Liskov, Chapter 7. 2 Liskov Substitution Principle In any client code, if the supertype object is substituted by a subtype object, the.
Type Abstraction SWE Spring October 05Kaushik, Ammann Substitution Principle “In any client code, if supertype object is substituted.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 1: Introduction Data.
Object Oriented Programming
Lecture 21 - Abstract Classes and Interface. Example Figure –Rectangle –Triangle Figure –Dimensions –Area.
Protocols Software Engineering II Wirfs Brock et al, Designing Object-Oriented Software, Prentice Hall, Mitchell, R., and McKim, Design by Contract,
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
Evaluating an Object-Oriented Design ©SoftMoore ConsultingSlide 1.
SWE 4743 Abstract Data Types Richard Gesick. SWE Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.
CSCI-383 Object-Oriented Programming & Design Lecture 24.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
Object-Oriented Design Concepts University of Sunderland.
6-1 Type Conformance and Closed Behavior Constructing class hierarchiesConstructing class hierarchies –need to be careful –two helpful principles: type.
1 CS 430 Database Theory Winter 2005 Lecture 7: Designing a Database Logical Level.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
BTM 382 Database Management Chapter 5: Advanced Data Modeling
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
CSCI 383 Object-Oriented Programming & Design Lecture 22 Martin van Bommel.
Modern Programming Tools And Techniques-I
Design by Contract Jim Fawcett CSE784 – Software Studio
Design by Contract Jim Fawcett CSE784 – Software Studio
State-Space and Behavior
Type Abstraction SWE Spring 2009.
IMPORTANT NOTE Some parts of these section slides deal with null ints. This was a mistake, as primitives cannot be null. These issues have been corrected.
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Type Abstraction SWE Spring 2013.
Presentation transcript:

Object-Oriented Analysis and Design Lecture 8 State Space, Behavior, and Type Conformance

Goals We’re looking for desirable properties of classes and class hierarchies. Violations of these good properties certainly may compile, but… Think of these properties as being like “normal forms” in database theory.

A Class Should “…represent a uniform abstraction of the properties of the individual objects that belong to the class.” The keys are state space and behavior.

State Space of a Class The state space of a class C is the set of all permitted states of any object of class C. The dimensions of a state space are the coordinates needed to specify the state of an object. Example: Intersections at Forbes and Morewood Forbes and Craig Forbes and Morewood parking lot

Another Example A CD player; what are the legal transitions? Current Track Seconds Remaining … “Point” in state space

State Space The dimensions of a class may be the same as its attributes. But attributes may not be simple types (ints, etc.) so state may be much more complicated. Also, there may be many ways to specify dimensions, e.g., secondsRemaining & totalLength percentFinished & totalLength

State Space of a Subclass If S is a subclass of T, then the state space of S must be contained in the state space of T. S’s state space is “confined” by T’s. Ex: T = Book, attribute numPages S = ChildrensBook 0600 numPages

State Space of a Subclass IF S is a subclass of T, then S’s state space must include at least all the dimensions of T’s. S’s state space can “extend” from T’s numPages numIllustrations Book ChildrensBook

Behavior of Classes & Subclasses A class’s behavior is the set of allowed transitions within its state space. A subclass can have its behavior Confined (can’t add 150 pages to a ChildrensBook), and Extended (we can add illustrations).

Class Invariants A class invariant is a condition that must be satisfied by every object of the class. Ex: Triangle, w/ side lengths a, b, and c. a+b  c b+c  a c+a  b In the 3-D state space, not all points are legitimate. In good designs, invariants are inherited (e.g., isosceles triangles, right triangles, etc.). a b c

Pre and Post Conditions These are for methods. Think of a contract between a user and supplier: Precondition: must be true (of the object) when the method is invoked. If not, the method has no obligation to perform correctly. Postcondition: must be true (of the object) when the method finishes. If not, the method is broken.

Responsibilities The object sending the message is obliged to check the precondition. If it’s satisfied, the object being called must guarantee the postcondition will be true after execution If the sender can’t guarantee the precondition, there is no guarantee of the postcondition. Class invariant and operation precondition Operation executes Class invariant and operation postcondition

Example: Database Insertion Invariant: Unique primary key in table A and foreign key points to existing record in table B. Precondition on insert into table A: Primary key of new record is unique in A Foreign key of new record exists in B Postcondition: New record exists in table A

When Designing New Methods Know the class invariants. Document the method’s pre and postconditions. Decide what to do if precondtions aren’t met. Test all three parts.

What is the True Dimension? Consider the positioning of Rectangle class objects (that can stretch, rotate). The class could hold Four corner points, or Top-left, bottom-right, and orientation, or Center, height, width, orientation. What is the correct number of dimensions? What does this have to do with invariants?

Types Again, the goal is to characterize good classes and class hierarchies. The type of a class includes its purpose invariants attributes operations, and their pre and postconditions, and signatures

Type vs. Class A type is independent of the implementation. There may be several implementations (classes) for a given type.

In Together, We Could Say:

So Far, So Good… But what if we try this? subclass = subtype?? No!

Type Conformance If S is a true subtype of T, then S must conform to T. That is, an object of type S can be provided wherever an object of type T is expected. Now technically, in Java and C++ this will always be so, but finding the diagonal of an Elephant is nonsensical.

Ensuring Type Conformance The invariants of the subclass must be at least as strong (restrictive) as those of the superclass. Every method of the superclass has a corresponding method in the subclass (with the same name and signature). Every subclass method’s precondition is no stronger than that in the superclass. Every subclass method’s postcondition is at least as strong as that in the superclass.

A Classic Example Employee gradeLevel > 0 perfEval  [0, 5] bonusPct  [0, 10] calcBonus(perfEval, bonusPct)  [0, 10] Manager calcBonus(perfEval, bonusPct) overridden What are legitimate ranges for Manager’s gradeLevel? perfEval? bonusPct ? calcBonus()?

A Classic Example (cont.) Manager class invariant: gradeLevel > 10 is OK gradeLevel  [-1, 15] not OK calcBonus() precondition: perfEval  [0, 6] is OK perfEval  [1, 4] not OK calcBonus() output (postcondition): calcBonus()  [0,8] is OK calcBonus()  [-1, 13] not OK

Why? Once again, an object of subtype S must be usable anywhere an object of the supertype T is.

Closed Behavior A final principle: Any operation on an object from class C – including inherited operations – should obey C’s class invariants. Example: the intersection at Forbes and Morewood parking lot. Intersection invariant: “Walk” signal can’t be displayed when any traffic light is green. Maybe we want a “turn right” arrow when the “Walk” signal is displayed?

A Question A Rectangle class, with methods rotate(angle) and scaleHoriz(scaleFactor) Now add subclass Square. Any principles violated? What are the possible solutions?

Another Question What’s wrong here? How to fix it?