CS 535 Object-Oriented Programming Spring Semester, 2003 Some OO Terms |
||
---|---|---|
© 2003, All Rights Reserved, SDSU & Roger Whitney San Diego State University -- This page last updated 03-Mar-03 |
Some OO Terms
Abstraction
“Extracting the essential details about an item or group of items, while ignoring the unessential details.”
Edward Berard
“The process of identifying common patterns that have systematic variations; an abstraction represents the common pattern and provides a means for specifying which variation to use.”
Richard Gabriel
Example
Pattern: Priority queue Essential Details: length items in queue operations to add/remove/find item Variation: link list vs. array implementation stack, queue
Heuristic 2.8
A class should capture one and only one key abstraction
Key abstraction
Encapsulation
Enclosing all parts of an abstraction within a container
Class contains
(aPoint x squared + aPoint y squared) sqrt
aPoint r
Information Hiding
An object should hide design decisions from its users
Hide
Heuristic 2.1
All data should be hidden within it class
Smalltalk instance variables in can be accessed in:
Engineering Heuristics, Absolutes & Beginners
All design decisions involve trade offs
Heuristics are design decisions that are nearly always true
No heuristic is correct all the time
Beginners violate heuristics because
Smalltalk and Private Methods
Private method
Two View of a Class: Inside & Outside
Users of a class care about
Coupling
Strength of interaction between objects in system
How tangled together the classes are
Cohesion
Degree to which the tasks performed by a single module are functionally related
Ralph Johnson’s Suggestions for Finding Abstractions
Do One Thing
Method should do on thing
findString:startingAt: asNumber asUppercase dropFinalVowels
String OrderedCollection Array ReadStream
Eliminate Duplication
(self asInteger - $a asInteger + anInteger) \\ 26 – (self asInteger - $a asInteger)
(self alphabetValue + anInteger) \\ 26 - self alphabetValue.
Keep rate of change similar
Minimize interfaces
Use the smallest interface you can
Minimize size of abstractions
Methods should be small
|
Average |
Mean |
Variables
/ class
|
2.1 |
1 |
Methods
/ class
|
16.7 |
9 |
Carriage
returns/method
|
7.6 |
5.0 |
Code used to generate Numbers
Variables Per Class
classes :=Smalltalk allClasses reject: [:each | each isMeta] variablesInClass :=classes collect: [:each | each instVarNames size]. average :=((variablesInClass fold: [:sum :each | sum + each] )/ variablesInClass size) asFloat. mean := variablesInClass asSortedCollection at: variablesInClass size // 2. max := variablesInClass fold: [:partialMax :each | partialMax max: each]
Methods Per Class
classes :=Smalltalk allClasses reject: [:each | each isMeta] methodsInClass :=classes collect: [:each | each selectors size]. average :=((methodsInClass fold: [:sum :each | sum + each] )/ methodsInClass size) asFloat. mean := methodsInClass asSortedCollection at: methodsInClass size // 2. max := methodsInClass fold: [:partialMax :each | partialMax max: each]
Minimize number of abstractions
A class hierarchy 6-7 levels deep is hard to learn
Break large system into subsystems, so people only have to learn part of the system at a time
Polymorphism
Example
Counter>>printOn: aStream aStream nextPutAll: ‘Counter(‘; nextPutAll: count printString; nextPutAll: ‘)’
aStream can be any object that implements nextPutAll:
Note we do not write:
Counter>>printOn: aStream aStream class = FileStream ifTrue:[ write to file ]. aStream class = WriteStream ifTrue: [write to write stream] aStream class = TextCollector ifTrue: [write to Transcript]
Avoid Case Statements
Smalltalk has no case statement
OO programers send a message to object instead
Each type of object handles the message according to its type
Case statements make it harder to add new cases
Simplistic Example
Bank offers various types of accounts:
Banking Classes
Customer
Transaction
Currency
Processing a Transaction
Using Case Statment
newCustomer := Bank createNewAccount: type.
Etc.
newCustomer class = Checking ifTrue:[ ...]
newCustomer class = Savings ifTrue:[ ...]
newCustomer class = CD ifTrue:[ ...]
newCustomer class = Jonior ifTrue:[ ...]
Polymorphism
newCustomer := Bank createNewAccount: type.
newCustomer.processTransaction: amount
Which processTransaction is called?
Adding new types of accounts to program requires: