SLIDE 1
Syntax analysis Definition keywords: (method select: (aBlock) - - PowerPoint PPT Presentation
Syntax analysis Definition keywords: (method select: (aBlock) - - PowerPoint PPT Presentation
Syntax analysis Definition keywords: (method select: (aBlock) [locals temp] (set temp ((self class) new)) (self do: [block (x) ((aBlock value: x) ifTrue: (temp add: x))]) temp) Syntax analysis Method defined: (method select: (aBlock)
SLIDE 2
SLIDE 3
Syntax analysis
Variables defined:
(method select: (aBlock) [locals temp] (set temp ((self class) new)) (self do: [block (x) ((aBlock value: x) ifTrue: (temp add: x))]) temp)
SLIDE 4
Syntax analysis
Expression form—set:
(method select: (aBlock) [locals temp] (set temp ((self class) new)) (self do: [block (x) ((aBlock value: x) ifTrue: (temp add: x))]) temp)
SLIDE 5
Syntax analysis
Expression form—block:
(method select: (aBlock) [locals temp] (set temp ((self class) new)) (self do: [block (x) ((aBlock value: x) ifTrue: (temp add: x))]) temp)
SLIDE 6
Syntax analysis
Expression form—messages sent:
(method select: (aBlock) [locals temp] (set temp ((self class) new)) (self do: [block (x) ((aBlock value: x) ifTrue: (temp add: x))]) temp)
SLIDE 7
Method dispatch
To answer a message:
- 1. Consider the class of the receiver
- 2. Is the method with that name defined?
- 3. If so, use it
- 4. If not, repeat with the superclass
Run out of superclasses? “Message not understood”
SLIDE 8
Class mechanisms
Superclass
- Its methods can answer messages sent to me
Instance variables
- Parts from which I’m formed
- Visible only to me (“private”)
Methods
- Activated whenever I receive a message
- By mechanism, “public” (anyone can send)
- By convention, some are private
– Sent to myself, or to an instance I create (Simplest thing that could possibly work.)
SLIDE 9
Each class has one of two roles
Abstract class
- Meant to be inherited from
- Some (
- Examples: Boolean, Shape, Collection
Regular (“concrete”) class
- Meant to be instantiated
- No subclassResponsibility methods
- Examples: True, Triangle, List
SLIDE 10
“Number hierarchy”
Object Magnitude Number Fraction Float Integer
SLIDE 11
“Extended Number hierarchy”
Object Magnitude Natural Number Fraction Float Integer SmallInteger LargeInteger LargePositiveInteger LargeNegativeInteger
SLIDE 12
Instance protocol for Magnitude
= aMagnitude equality (like Magnitudes) < aMagnitude comparison (ditto) > aMagnitude comparison (ditto) <= aMagnitude comparison (ditto) >= aMagnitude comparison (ditto) min: aMagnitude minimum (ditto) max: aMagnitude maximum (ditto) Subclasses: Date, Natural
- Compare Date with Date, Natural w/Natural, . . .
SLIDE 13
Implementation of Magnitude: Reuse
(class Magnitude ; abstract class [subclass-of Object] (method = (x) (self subclassResponsibility)) ; may not inherit = from Object (method < (x) (self subclassResponsibility)) (method > (y) (y < self)) (method <= (x) ((self > x) not)) (method >= (x) ((self < x) not)) (method min: (aMag) ((self < aMag) ifTrue:ifFalse: {self} {aMag})) (method max: (aMag) ((self > aMag) ifTrue:ifFalse: {self} {aMag})) )
SLIDE 14
Instance protocol for Number
negated reciprocal abs absolute value + aNumber addition
- aNumber
subtraction * aNumber multiplication / aNumber division (may answer a Fraction!) isNegative sign check isNonnegative sign check isStrictlyPositive sign check
SLIDE 15
More instance protocol for Number
coerce: aNumber class of receiver, value of argument asInteger conversion asFraction conversion asFloat conversion
SLIDE 16
Your turn: Object-oriented design
Given Magnitude, which of these methods can be implemented in terms of others? (poll) negated * coerce: reciprocal / asInteger abs isNegative asFraction + isNonnegative asFloat
- isStrictlyPositive
SLIDE 17
Number methods
(method - (y) (self + (y negated))) (method abs () ((self isNegative) ifTrue:ifFalse: {(self negated)} {self})) (method / (y) (self * (y reciprocal))) (method isNegative () (self < (self coerce: 0))) (method isNonnegative () (self >= (self coerce: 0))) (method isStrictlyPositive () (self > (self coerce: 0)))
SLIDE 18
Example class Fraction: initialization
(class Fraction [subclass-of Number] [ivars num den] ;; representation (concrete!) ;; invariants by signReduce, divReduce (class-method num:den: (a b) ((self new) initNum:den: a b)) (method initNum:den: (a b) ; private (self setNum:den: a b) (self signReduce) (self divReduce)) (method setNum:den: (a b) (set num a) (set den b) self) ; private .. other methods of class Fraction ... )
SLIDE 19
Information revealed to self
“Instance variables” num and den
- Directly available
- Always and only go with self
Object knows its own representation, invariants, private methods:
(method asFraction () self) (method print () (num print) (’/ print) (den print) self) (method reciprocal () (((Fraction new) setNum:den: den num) signReduce))
SLIDE 20
Information revealed to self: your turn
How would you implement coerce:? (Value of argument, representation of receiver)
(method asFraction () self) (method print () (num print) (’/ print) (den print) self) (method reciprocal () (((Fraction new) setNum:den: den num) signReduce)) (method coerce: (aNumber) ...)
SLIDE 21