DOC PREVIEW
UIUC CS 598 - Polymorphism

This preview shows page 1-2-16-17-18-34-35 out of 35 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

PolymorphismSlide 2Object Model for PayrollExamplesMany classes with same interfacePolymorphic clientSlide 7Polymorphism instead of Case StatementsEliminating CasesClass UndefinedObjectDon’t Test ClassesChoicesAnother choiceMagnitudeNumber HierarchyMagnitude commentSlide 17NumbersNumbers are polymorphicAn Abstract Class ...Uses of inherited methodsTemplate methodUses of PolymorphismPolymorphism, not conditionalsPowerPoint PresentationSlide 26ProblemSolutionMorphicUIManagerMVCUIManagerFirst attemptSlide 32Slide 33Slide 34Designing interfacesObject-oriented Programming and Design1PolymorphismPoly => manyMorph => shapeVariables take on many shapes, or many classes of objects.Object-oriented Programming and Design2PolymorphismPolymorphism 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 Design3Object Model for PayrollEmployeeEmployeeTransactionPayrollSystempost:datePaycheckTimecardSalaryChangesalarypay, taxeshoursWorkedvacationpostTo::abstract classabstract classabstract methodabstract method****Object-oriented Programming and Design4Examplespost: aTransactionaTransaction postTo: self.transactations add: aTransactionnumbers inject: 0 into: [:sum :i | sum + i]Object-oriented Programming and Design5Many classes with same interfaceExamplesCollectionsNumbersMagnitudeEasier to remember method namesPolymorphic clients can use any classObject-oriented Programming and Design6Polymorphic clienttasks := OrderedCollection new.tasks add: Task new.[tasks isEmpty] whileFalse: [task := tasks removeLast.tasks addAll: task computeNewTasks]Object-oriented Programming and Design7Polymorphic clienttasks := 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 Design8Polymorphism instead of Case StatementsSmalltalk 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 Design9Eliminating Casesexp case: 1 do: [...];case: 15 do: [...].Make classes for 1 and 15, with a method called msg.exp msg or (dictionary at: exp) msgObject-oriented Programming and Design10Class UndefinedObjectClass UndefinedObject has one instance -- nil.All variables are initialized to nil.Also used to indicate illegal value.Object-oriented Programming and Design11Don’t Test ClassesUndefinedObject ObjectisNil isNil^ true ^ falsenotNil notNil^ false ^ trueDon’t test classes: use message sending and inheritance.Object-oriented Programming and Design12Choicesbadx class = UndefinedObject ifTrue: [...]OKx = nil ifTrue: [...]bestx isNil ifTrue: [...]Object-oriented Programming and Design13Another choicex ifNil: [ … ]ObjectifNil: aBlock^selfUndefinedObjectifNil: aBlock^aBlock valueObject-oriented Programming and Design14MagnitudeMagnitude ()Number () ....Character ()Date ('day' 'year')Time ('hours' 'minutes' 'seconds')Object-oriented Programming and Design15Number HierarchyNumberFraction ('numerator' 'denominator')Integer ()LargeNegativeInteger ()LargePositiveInteger ()SmallInteger ()...Object-oriented Programming and Design16Magnitude commentThe abstract class Magnitude provides common protocol for objects that have the ability to be compared along a linear dimension, such as dates or times. Subclasses of Magnitude include Date, ArithmeticValue, and Time, as well as Character and LookupKey.Object-oriented Programming and Design17Magnitude commentSubclasses must implement the following messages:comparing<=hashObject-oriented Programming and Design18NumbersNumbers are part of the class hierarchy, not built into compiler.Numbers understand +, -, *, /, <, <=, etc.3 / 6 => 1 / 22 sqrt => 1.414213 + 4 * 2 =>Object-oriented Programming and Design19Numbers are polymorphic(3/4) * 0.45 + 4.6s2 4.9375(0.45 + 4.6s2) / 17 0.297059(0.45 + 4.6s2) truncated / 17 (5/17)Object-oriented Programming and Design20An Abstract Class ...Has "subclassResponsibility" methods.Usually has Template Methods that call abstract subclassResponsibility methodsTemplate Methods are polymorphic; method that is called depends on subclassObject-oriented Programming and Design21Uses of inherited methodsTo be shared by all subclasses• don't redefineDefault• probably redefineAbstract (subclassResponsibility)• must redefineObject-oriented Programming and Design22Template methodCalls code in subclass - polymorphic• rarely redefine• change it by redefining the methods it calls in subclassesObject-oriented Programming and Design23Uses of PolymorphismMethod lookup finds the method that is right for the receiver.• method always knows the class of the receiver• different classes lead to different methodsObject-oriented Programming and Design24Polymorphism, not conditionalsDon’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 Design25openMessageEditString: 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: selflist: #messageListSingletonselected: #indexIsOne changeSelected: #indexIsOne:menu: #messageListMenu:shifted:.messageListView window: (0 @ 0 extent: 200 @ 12).topView addSubView: messageListView.…Object-oriented Programming and Design26openOnClassWithEditString: 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: selflist: #classListSingletonselected: #indexIsOne changeSelected: #indexIsOne:menu:


View Full Document

UIUC CS 598 - Polymorphism

Download Polymorphism
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Polymorphism and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Polymorphism 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?