Object-oriented Programming and Design 1 Polymorphism Poly => many Morph => shape Variables take on many shapes, or many classes of objects.

Slides:



Advertisements
Similar presentations
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Advertisements

S.Ducasse Stéphane Ducasse 1 Numbers.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 23: Inheritance.
Zero Exponent? Product or quotient of powers with the same base? Simplify Negative Exponents.
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
© Oscar Nierstrasz ST — Introduction ST 1.1 A Word about Primitives  For optimization, if a primitive fails, the code following is executed. 
Stéphane Ducasse«ChapterNr».1 Abstract Classes Should not be instantiated (abstract in Java) But can defined complete methods Defines a protocol common.
Stéphane Ducasse 1 The Taste of Smalltalk.
22-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Object-oriented Programming Concepts
03G-1 Everything is an Object Examining builtin classes All these are classes in Little Smalltalk:  Object  Class  Method  Block  Boolean True False.
Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Stéphane Ducasse5.1 Smalltalk in a Nutshell OO Model in a Nutshell Syntax in a Nutshell.
Stéphane Ducasse 1 Smalltalk in a Nutshell.
S.Ducasse Stéphane Ducasse 1 The Taste of Smalltalk.
Please open your laptops, log in to the MyMathLab course web site, and open Daily Quiz 18. You will have 10 minutes for today’s quiz. The second problem.
Multiplying, Dividing, and Simplifying Radicals
The Game of Algebra or The Other Side of Arithmetic The Game of Algebra or The Other Side of Arithmetic © 2007 Herbert I. Gross by Herbert I. Gross & Richard.
1 Object-Oriented Software Engineering CIS 375 Bruce R. Maxim UM-Dearborn.
1 Advanced Object Oriented Systems (CM0318) Lecture 9 (Last modified Thursday 21st February 2002)
Section 1.1 Numbers and Their Properties.
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
S.Ducasse Stéphane Ducasse 1 Smalltalk Coding Idioms.
1 Object-Oriented Programming: Polymorphism 10.1 Introduction 10.2 Relationships Among Objects in an Inheritance Hierarchy Invoking Superclass Methods.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Encapsulation and Data Abstraction Testing Applications in Smalltalk.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
Centralized vs. Decentralized Interpreter Pattern Visitor Pattern.
Refactoring Deciding what to make a superclass or interface is difficult. Some of these refactorings are helpful. Some research items include Inheritance.
Smalltalk (and Squeak) Aida Dungan and Rick Shreve.
Object-oriented programming and design 1 Object Identity = vs. == copying objects Value Object ValueWithHistory.
SECTION 1.4 EXPONENTS. PRODUCT OF POWERS When you multiply two factors having the same base, keep the common base and add the exponents.
Algebraic Fractions  Know your rules  Anything raised to the 0 power = 1  Negative exponents can be moved to the opposite and made positive (that is,
1 Programming for Engineers in Python Autumn Lecture 6: More Object Oriented Programming.
Smalltalk in a.NET World How to write a Smalltalk compiler without writing a VM John Brant
Object-Oriented Programming Chapter Chapter
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
3C-1 Purity Typing Language semantics Inheritance model  Single vs. Multiple inheritance  Common root Modular mechanisms Generics Object Oriented Languages.
ISBN Object-Oriented Programming Chapter Chapter
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Nathanael Schärli (SCG, University of Bern) and Andrew P. Black (CSE, Oregon Health & Science University) Nathanael Schärli (SCG, University of Bern) and.
3C-1 Identifiers Variables Literal Objects  Numbers  Characters  Strings Control Structure  Blocks  Conditionals  Loops Messages Defining New Classes.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Exponents and Radicals
S.Ducasse Stéphane Ducasse 1 Smalltalk in a Nutshell.
S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Smalltalk in a Nutshell.
CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14 – Smalltalk, Fall CSE3302 Programming Languages, UT-Arlington ©Chengkai.
STROUD Worked examples and exercises are in the text 1 STROUD Worked examples and exercises are in the text Programme F2: Introduction to algebra PROGRAMME.
Stéphane Ducasse 1 A Little Journey in the Smalltalk Syntax.
1 Primitive Types n Four integer types:  byte  short  int (most common)  long n Two floating-point types:  float  double (most common) n One character.
Stéphane Ducasse 1 Abstract Classes.
Administrative Issues Lecture 22 – Prolog (III), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, HW3 (due at March 18 th ) Essay.
FractionsDecimalsPowers Answer  This is how you add fractions.
Command Pattern. Intent encapsulate a request as an object  can parameterize clients with different requests, queue or log requests, support undoable.
S.Ducasse 1 Instance Initialization How to ensure that an instance is well initialized? Automatic initialize Lazy initialization Proposing the right interface.
Class 22: Inheritance CS150: Computer Science University of Virginia
Java Primer 1: Types, Classes and Operators
Programming Language Concepts (CIS 635)
Lecture 21: Inheritance CS200: Computer Science University of Virginia
Smalltalk – a Pure (and first) OOPL
Introduction to C++ Programming
Copyright 2000, Georgia Tech
Introduction to Smalltalk
Java Programming Language
Section 7.2 Rational Exponents
CSE 3302 Programming Languages
Presentation transcript:

Object-oriented Programming and Design 1 Polymorphism Poly => many Morph => shape Variables take on many shapes, or many classes of objects.

Object-oriented Programming and Design 2 Polymorphism Polymorphism in OOP is caused by late- binding of procedure calls (message lookup). Program can work with any object that has the right set of methods.

Object-oriented Programming and Design 3 Object Model for Payroll Employee EmployeeTransaction PayrollSystem post: date Paycheck Timecard SalaryChange salary pay, taxes hoursWorked vacation : postTo: abstract class abstract method * *

Object-oriented Programming and Design 4 Examples post: aTransaction aTransaction postTo: self. transactations add: aTransaction numbers inject: 0 into: [:sum :i | sum + i]

Object-oriented Programming and Design 5 Many classes with same interface Examples Collections Numbers Magnitude Easier to remember method names Polymorphic clients can use any class

Object-oriented Programming and Design 6 Polymorphic client tasks := OrderedCollection new. tasks add: Task new. [tasks isEmpty] whileFalse: [ task := tasks removeLast. tasks addAll: task computeNewTasks]

Object-oriented Programming and Design 7 Polymorphic client tasks := SortedCollection sortBlock: [:first :second | first priority > second priority]. tasks add: Task new. [tasks isEmpty] whileFalse: [ task := tasks removeLast. tasks addAll: task computeNewTasks]

Object-oriented Programming and Design 8 Polymorphism instead of Case Statements Smalltalk has no case statement. OO programmers tend to replace case statements with message sending. Instead of making a new case, add a subclass.

Object-oriented Programming and Design 9 Eliminating Cases exp case: 1 do: [...]; case: 15 do: [...]. Make classes for 1 and 15, with a method called msg. exp msg or (dictionary at: exp) msg

Object-oriented Programming and Design 10 Class UndefinedObject Class UndefinedObject has one instance -- nil. All variables are initialized to nil. Also used to indicate illegal value.

Object-oriented Programming and Design 11 Don’t Test Classes UndefinedObjectObject isNil ^ true^ false notNil ^ false^ true Don’t test classes: use message sending and inheritance.

Object-oriented Programming and Design 12 Choices bad x class = UndefinedObject ifTrue: [...] x = nil ifTrue: [...] x isNil ifTrue: [...] x ifNil: […] best

Object-oriented Programming and Design 13 x ifNil: [ … ] Object ifNil: aBlock ^self UndefinedObject ifNil: aBlock ^aBlock value

Object-oriented Programming and Design 14 Magnitude Magnitude () Number ().... Character () Date ('day' 'year') Time ('hours' 'minutes' 'seconds')

Object-oriented Programming and Design 15 Number Hierarchy Number Fraction ('numerator' 'denominator') Integer () LargeNegativeInteger () LargePositiveInteger () SmallInteger ()...

Object-oriented Programming and Design 16 Numbers Numbers are part of the class hierarchy, not built into compiler. Numbers understand +, -, *, /, <, <=, etc. 3 / 6 => 1 / 2 2 sqrt => * 2 =>

Object-oriented Programming and Design 17 Numbers are polymorphic (3/4) * s ( s2) / ( s2) truncated / 17 (5/17)

Object-oriented Programming and Design 18 Polymorphism, not conditionals Don’t explicitly check class of argument. Use polymorphism to make computation depend on class of argument. aThing class = A ifTrue: [self doStuffWith: aThing] ifFalse: [self doSomethingElseWith: aThing]

Object-oriented Programming and Design 19 Original in Number raisedTo: aNumber "Answer the receiver raised to aNumber." aNumber isInteger ifTrue: ["Do the special case of integer power" ^ self raisedToInteger: aNumber]. self < 0 ifTrue: [ self error: self printString, ' raised to a non-integer power' ]. aNumber = 0 ifTrue: [^ 1]."Special case of exponent=0" (self = 0) | (aNumber = 1) ifTrue: [^ self]."Special case of exponent=1" ^ (aNumber * self ln) exp"Otherwise use logarithms"

Object-oriented Programming and Design 20 Original in Number raisedToInteger: operand "Answer the receiver raised to the power operand, an Integer." | count result | operand = 0 ifTrue: [^ self class one]. operand = 1 ifTrue: [^ self]. operand < 0 ifTrue: [^ (self raisedToInteger: operand negated) reciprocal]. count := 1. [(count := count + count) < operand] whileTrue. result := self class one. [count > 0] whileTrue: [result := result * result. (operand bitAnd: count) = 0 ifFalse: [result := result * self]. count := count bitShift: -1]. ^ result

Object-oriented Programming and Design 21 Refactoring raisedTo: raisedTo: power "Answer the receiver raised to a power." self = 1 ifTrue: [^ self]. ^ power timesMultiply: self

Object-oriented Programming and Design 22 Integer timesMultiply: aNumber | count result | self = 0 ifTrue: [^ aNumber class one]. self < 0 ifTrue: [^ (self negated timesMultiply: aNumber) reciprocal]. count := 1. [(count := count + count) < self] whileTrue. result := 1. [count > 0] whileTrue: [result := result * result. (self bitAnd: count) = 0 ifFalse: [result := result * aNumber]. count := count bitShift: -1]. ^ result

Object-oriented Programming and Design 23 Number timesMultiply: aNumber | truncated | aNumber < 0.0 ifTrue: [(truncated := self truncated) = self ifTrue: ["self is a whole number." ^ (truncated timesMultiply: aNumber) * self class one]. ArithmeticError signal: aNumber printString, ' : negative number raised to a non-integer power']. ^ (self * aNumber ln) exp

Double-dispatching: the Problem Float Smallinteger Fraction FixedPoint Matrix Fr Sc Fi FlM * * * * * Fl Fl Fl FlFl Fr Fr Fl Fi Fi Sc Sc Sc Sc Sc Sc Sc Fi Fi

Arithmetic Number * Matrix Multiply each element by number Matrix * Number Multiply each element by number Matrix * Matrix Standard matrix multiplication

Arithmetic Integer * IntegerPrimitive int operations Integer * FloatConvert int to float Float * IntegerConvert int to float Float * FloatPrimitive float operation

Double dispatching: the Solution l Primary method (+, *, etc) sends a second message to argument, encoding the class of the receiver. l Second message knows class of both its argument and its receiver.

Primary operations Send a second message, encoding the class of the original receiver in the name of the message. + anArg  anArg sumFromInteger: self

Double Dispatching Methods Knows the class of receiver and argument. sumFromInteger: anInteger ^anInteger asFloat + self

The first message dispatch SmallInteger + anArg ^ anArg sumFromInteger: self 8.9 sumFromInteger: 37

The second message dispatch 8.9 addSmallInteger: 37 Float addSmallIngeter: anArg ^ anArg asFloat + self

Finishing Up Float + anArg ^aNumber sumFromFloat: self

Multiplication Fraction has two instance variables, numerator and denominator. * aNumber "Result is a new Fraction unless the argument is a Float, in which case the result is a Float." ^aNumber productFromFraction: self

Fraction productFromFraction: aFraction ^(self species numerator: aFraction numerator * numerator denominator: aFraction denominator * denominator) reduced

Integer productFromFraction: aFraction ^(aFraction species numerator: aFraction numerator * self denominator: aFraction denominator) reduced

Float productFromFraction: aFraction ^aFraction asFloat * self

Float * aNumber "Answer a Float that is the result of multiplying the receiver by the argument, aNumber. The primitive fails if it cannot coerce the argument to a Float" ^aNumber productFromFloat: self

Fraction productFromFloat: aFloat ^aFloat * self asFloat

Shared Responsibilities Sometimes need to select a method based on class of several objects: Displaying object -- depends on both the kind of object and the windowing system Arithmetic -- depends on the types of both arguments

Double dispatching Three kinds of mesages primary operations double dispatching methods forwarding operations Implement inheriting from superclass of argument Implement commutativity

Cost of Double Dispatching Adding a new class requires adding a message to each of the other classes. Worst case is N*N methods for N classes. However, total lines of code is not much larger.

Object-oriented Programming and Design 42 openMessageEditString: aString "Create a pluggable version of the views for a Browser that just shows one message." | messageListView browserCodeView topView annotationPane underPane y | Smalltalk isMorphic ifTrue: [^ self openAsMorphMessageEditing: aString]. topView := (StandardSystemView new) model: self. topView borderWidth: 1. "label and minSize taken care of by caller" messageListView := PluggableListView on: self list: #messageListSingleton selected: #indexIsOne changeSelected: #indexIsOne: menu: #messageListMenu:shifted:. messageListView window: 0 extent: 12). topView addSubView: messageListView. …

Object-oriented Programming and Design 43 openOnClassWithEditString: aString "Create a pluggable version of all the views for a Browser, including views and controllers." | classListView messageCategoryListView messageListView browserCodeView topView switchView annotationPane underPane y optionalButtonsView | Smalltalk isMorphic ifTrue: [^ self openAsMorphClassEditing: aString]. topView := (StandardSystemView new) model: self. topView borderWidth: 1. "label and minSize taken care of by caller" classListView := PluggableListView on: self list: #classListSingleton selected: #indexIsOne changeSelected: #indexIsOne: menu: #classListMenu:shifted: keystroke: #classListKey:from:. classListView window: 0 extent: 12). topView addSubView: classListView.

Object-oriented Programming and Design 44 Problem l Two user interface frameworks, Morphic and MVC l Application knows its UI frameworks l Refactor so app doesn’t know its UI

Object-oriented Programming and Design 45 Solution Have method delegate to an object that might be either a Morphic expert or a MVC expert openMessageEditString: aString ^UIManager default openMessageEditString: aString for: self

Object-oriented Programming and Design 46 MorphicUIManager openMessageEditString: aString for: aBrowser ^aBrowser openAsMorphMessageEditing: aString

Object-oriented Programming and Design 47 MVCUIManager openMessageEditString: aString for: aBrowser | topView messageListView … | topView := (StandardSystemView new) model: aBrowser. topView borderWidth: 1. "label and minSize taken care of by caller" messageListView := PluggableListView on: aBrowser list: #messageListSingleton selected: #indexIsOne changeSelected: #indexIsOne: menu: #messageListMenu:shifted:. messageListView window: 0 extent: 12). topView addSubView: messageListView.

Object-oriented Programming and Design 48 First attempt To get rid of isMorphic Change method to delegate to UIManager default Copy old method into MVCUIManager and MorphicUIManager Replace “isMorphic” with “true” or “false” Simplify

Object-oriented Programming and Design 49 Problem UIManager subclasses will accumulate application-specific code. Methods related to application should be in application package.

Object-oriented Programming and Design 50 Uses of Polymorphism Methods often depend radically on class of receiver. isNil ifTrue:ifFalse: double dispatching

Object-oriented Programming and Design 51 Double dispatch l Sometimes method needs to depend on class of receiver AND class of argument l Solution: l First method sends message to argument, encoding type of original receiver in the message name l Second method knows type of both receiver and argument

Object-oriented Programming and Design 52 Double dispatch in browser Browser>>displayWith: aUIManager aUIManager displayBrowser: self MorphicUIManager>>displayBrowser: aBrowser …

Object-oriented Programming and Design 53 Polymorphism More significant than inheritance. Requires standard interfaces. Many different uses: Template methods standard interfaces eliminating conditionals …