SDSU CS 535 Object-Oriented Programming
Fall Semester, 2003
Some OO Terms
Previous    Lecture Notes Index    Next    
© 2003, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 14-Oct-03

Contents of Doc 12, Some OO Terms


References

Ralph Johnson Lecture notes, Lecture 3 Data Abstraction and Encapsulation, http://st-www.cs.uiuc.edu/users/cs497/lectures.html

Object-Oriented Design Heuristics, Riel, Chapter 2


Doc 12, Some OO Terms Slide # 2

Some OO Terms




Doc 12, Some OO Terms Slide # 3

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

Doc 12, Some OO Terms Slide # 4
Heuristic 2.8

A class should capture one and only one key abstraction





Look at nouns in requirements specification or system description

Look at these phrases. Some will be obvious classes, some will be obvious nonsense, and some will fall between obvious and nonsense. Skip the nonsense, keep the rest. The goal is a list of candidate objects. Some items in the list will be eliminated, others will be added later. Finding good objects is a skill, like finding a good functional decomposition.

A refrigerator has a motor, a temperature sensor, a light and a door. The motor turns on and off primarily as prescribed by the temperature sensor. However, the motor stops when the door is opened. The motor restarts when the door is closed if the temperature is too high. The light is turned on when the door opens and is turned off when the door is closed.


Doc 12, Some OO Terms Slide # 5

Encapsulation


Enclosing all parts of an abstraction within a container

Class contains




Heuristic 2.9

Keep related data and behavior in one place



Code that uses a lot of accessing methods of an object should be used to that object

(aPoint x squared + aPoint y squared) sqrt

verses

aPoint r

Doc 12, Some OO Terms Slide # 6

Information Hiding


An object should hide design decisions from its users

Hide




How does Point story its data?

How does OrderedCollection hold elements?

We use the classes without knowing

Doc 12, Some OO Terms Slide # 7
Heuristic 2.1

All data should be hidden within it class



Smalltalk instance variables in can be accessed in:





Most languages have a construct for global access to data






Doc 12, Some OO Terms Slide # 8
Hiding Instance Variables

Some argue that only two methods should access an instance variable

Class BankAccount
Instance variable: balance

balance
   ^balance

balance: aNumber
   balance := anumber

deposit: aNumber
   self balance: (self balance + aNumber)

This protects the class from changes in instance variables


Makes easy to enforce constraints

balance: aNumber
   aNumber < 0 ifTrue: [ NegativeBalanceError raiseSignal].
   balance := aNumber


Doc 12, Some OO Terms Slide # 9
Hiding Instance Variables & Refactoring Browser

Refactoring browser

So don’t worry about hiding instance variables

If later you need to hide them it is easy to do




Doc 12, Some OO Terms Slide # 10
Smalltalk and Private Methods

Private method



All instance methods in Smalltalk are publicly accessible

Put private methods in “private” protocol

Smalltalk programmers know not to use such methods


Doc 12, Some OO Terms Slide # 11
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






Doc 12, Some OO Terms Slide # 12
Two View of a Class: Inside & Outside


Users of a class care about





To a user a class is a black box


Doc 12, Some OO Terms Slide # 13
Inheritance (White Box) Verses Composition (Black Box)

Composition

Smalltalk defineClass: #ComposedWordSteam
   superclass: #{Core.Object}
   indexedType: #none
   private: false
   instanceVariableNames: 'input '
   classInstanceVariableNames: ''
   imports: ''
   category: 'CS535'

Class Methods

on: aCollection
   ^self input: (ReadStream on: aCollection) 

input: aStream
   ^super new setInput: aStream 

Instance Methods

atEnd
   ^input atEnd

next
   "some code to get the next word"
   blah

peek
   ^input peek

setInput: aStream
   input :=aStream


Doc 12, Some OO Terms Slide # 14
Inheritance

Smalltalk.Core defineClass: #WordStream
   superclass: #{Core.ReadStream}
   indexedType: #none
   private: false
   instanceVariableNames: ''
   classInstanceVariableNames: ''
   imports: ''
   category: 'CS535'

next
   "some code to get the next word"
   blah


But what if we don’t want peek changed?


Doc 12, Some OO Terms Slide # 15
Inheritance (White Box) Verses Composition (Black Box)

Inheritance

Need to know how super class works

Hard to change super class

May inherit methods you don’t want

Can replace uses of super class (ReadStream) with new class (WordStream)


Composition

Don’t need to know how composed object (ReadStream) works

Can change type of composed object

Controls which methods of composed object to expose

Forward messages to composed object

In languages that declare types may not be able to replace composed object with new class (WordStream)


Doc 12, Some OO Terms Slide # 16

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


Doc 12, Some OO Terms Slide # 17

Ralph Johnson’s Suggestions for Finding Abstractions




Doc 12, Some OO Terms Slide # 18
Do One Thing

Method should do on thing


      findString:startingAt:
      asNumber
      asUppercase
      dropFinalVowels

Class should be what its name says

      String
      OrderedCollection
      Array
      ReadStream


Break complex classes/methods into simpler ones


Doc 12, Some OO Terms Slide # 19
Eliminate Duplication

(self asInteger - $a asInteger + anInteger) \\ 26 – (self asInteger - $a asInteger)


(self alphabetValue + anInteger) \\ 26 - self alphabetValue.


Doc 12, Some OO Terms Slide # 20
Keep rate of change similar




Doc 12, Some OO Terms Slide # 21
Minimize interfaces

Use the smallest interface you can

Use Number instead of Float

Avoid embedding classes in names

add: instead of addNumber:

Don’t check the class of an object


Doc 12, Some OO Terms Slide # 22
Minimize size of abstractions

Methods should be small


Classes should be small


VW 7.0 Base System


Average
Mean
Variables / class
2.1
1
Methods / class
16.7
9
Carriage returns/method
7.6
5.0


Doc 12, Some OO Terms Slide # 23
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]


Doc 12, Some OO Terms Slide # 24
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


Doc 12, Some OO Terms Slide # 25

Polymorphism

Objects with the same interface can be substituted for each other at run-time

Variables take on many classes of object

Objects will behave according to their type

Code can work with any object that has the right set of methods

In C++ polymorphism requires

In Java polymorphism requires


In Smalltalk polymorphism does not require inheritance



Doc 12, Some OO Terms Slide # 26
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]


Doc 12, Some OO Terms Slide # 27

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



Doc 12, Some OO Terms Slide # 28

Simplistic Example


Bank offers various types of accounts:


Each type has different rules for processing a transaction

Doc 12, Some OO Terms Slide # 29

Banking Classes

Customer

Transaction

Currency




Doc 12, Some OO Terms Slide # 30
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:[ ...]


Doc 12, Some OO Terms Slide # 31
Polymorphism

newCustomer := Bank createNewAccount: type.
newCustomer.processTransaction: amount

Which processTransaction is called?



Adding new types of accounts to program requires:

Adding new subclasses
Changing code that creates objects

Avoid checking the class of an object

Doc 12, Some OO Terms Slide # 32

Linked List Example


Note all the checking at in the methods

Smalltalk defineClass: #LinkedList
   superclass: #{Core.SequenceableCollection}
   indexedType: #none
   private: false
   instanceVariableNames: 'value next '
   classInstanceVariableNames: ''
   imports: ''
   category: 'CS535'

Class Methods

with: anObject
   ^super new setValue: anObject 

Instance Methods

addLast: anObject
   next ifNotNil: [^next addLast: anObject].
   next := LinkedList with: anObject.

includes: anObject
   value = anObject ifTrue:[^true].
   next ifNotNil: [^next includes: anObject].
   ^false


Doc 12, Some OO Terms Slide # 33
printOn: aStream 
   aStream
      print: value;
      nextPutAll: ' '.
   next ifNotNil: [next printOn: aStream]

setValue: anObject
   value := anObject.

size
   next ifNil: [^1].
   ^next size + 1 

Doc 12, Some OO Terms Slide # 34
LinkedList with Poymorphism

A node to represent the end of the list

Smalltalk defineClass: #NilNode
   superclass: #{Core.Object}
   indexedType: #none
   private: false
   instanceVariableNames: ''
   classInstanceVariableNames: ''
   imports: ''
   category: 'CS535'
Instance Methods

addLast: anObject
   self become: (LinkedList with: anObject)

includes: anObject
   ^false

printOn: aStream

size
   ^0 


Doc 12, Some OO Terms Slide # 35
LinkedList with NilNode

Smalltalk defineClass: #LinkedList
   superclass: #{Core.SequenceableCollection}
   indexedType: #none
   private: false
   instanceVariableNames: 'value next '
   classInstanceVariableNames: ''
   imports: ''
   category: 'CS535'
Class Methods

with: anObject
   ^super new setValue: anObject 
Instance Methods

addLast: anObject
   next addLast: anObject

includes: anObject
   value = anObject ifTrue:[^true].
   ^next includes: anObject

printOn: aStream 
   aStream
      print: value;
      nextPutAll: ' '.
   next printOn: aStream


Doc 12, Some OO Terms Slide # 36
setValue: anObject
   value := anObject.
   next := NilNode new.

size
   ^next size + 1 

Copyright ©, All rights reserved.
2003 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
OpenContent license defines the copyright on this document.

Previous    visitors since 14-Oct-03    Next