[ENH] Collection>>storeOn: refactoring
Richard A. O'Keefe
ok at atlas.otago.ac.nz
Fri Apr 27 03:18:09 UTC 2001
I have a subclass of OrderedCollection and wanted to provide storeOn:
for it. It seemed silly to copy the whole method when I only wanted to
fill in a few instance variables. With this refactoring, a subclass of
OrderedCollection that adds 'foo' and 'bar' instance variables with
appropriate setters can do
storeEmptyOn: aStream
super storeEmptyOn: aStream.
aStream nextPutAll: ' foo: '.
foo storeOn: aStream.
aStream nextPutAll: '; bar: '.
bar storeOn: aStream.
^false
and the "new" expression and the "add:"s will still come from Collection.
'From Squeak3.0 of 4 February 2001 [latest update: #3552] on 27 April 2001 at 3:20 pm'!
!Collection methodsFor: 'printing' stamp: 'raok 4/27/2001 15:09'!
storeEmptyOn: aStream
"Write to Stream an expression that creates an empty collection of my class that is as
like me as possible, except for my elements. storeOn: will take care of writing the
elements out. Return a Boolean to say whether the expression ends with a message
send so that storeOn: knows to write a semicolon. This is provided so that subclasses
can override it to write out attributes of various kinds. They will typically call
this method via a super call."
aStream nextPut: $(; nextPutAll: self class name; nextPutAll: ' new)'.
^true! !
!Collection methodsFor: 'printing' stamp: 'raok 4/27/2001 15:08'!
storeOn: aStream
"Refer to the comment in Object|storeOn:."
| noneYet |
aStream nextPut: $(.
noneYet _ self storeEmptyOn: aStream.
self
do: [:each |
noneYet
ifTrue: [noneYet _ false]
ifFalse: [aStream nextPut: $;].
aStream nextPutAll: ' add: '.
aStream store: each].
noneYet
ifFalse: [aStream nextPutAll: '; yourself'].
aStream nextPut: $)! !
More information about the Squeak-dev
mailing list
|