stoop ed-unit ofreuse
DESCRIPTION
TRANSCRIPT
![Page 1: Stoop ed-unit ofreuse](https://reader033.vdocuments.mx/reader033/viewer/2022061204/547ea7975806b5e55e8b46ff/html5/thumbnails/1.jpg)
Stéphane Ducasse 1
Stéphane [email protected]://stephane.ducasse.free.fr/
Elements of Design -Unit of Reuse
![Page 2: Stoop ed-unit ofreuse](https://reader033.vdocuments.mx/reader033/viewer/2022061204/547ea7975806b5e55e8b46ff/html5/thumbnails/2.jpg)
S.Ducasse 2
Methods are Units of Reuse
Dynamic binding + methods = reuse in subclasses
![Page 3: Stoop ed-unit ofreuse](https://reader033.vdocuments.mx/reader033/viewer/2022061204/547ea7975806b5e55e8b46ff/html5/thumbnails/3.jpg)
S.Ducasse 3
Methods are Unit of Reuse
![Page 4: Stoop ed-unit ofreuse](https://reader033.vdocuments.mx/reader033/viewer/2022061204/547ea7975806b5e55e8b46ff/html5/thumbnails/4.jpg)
S.Ducasse 4
Example: Forced to Duplicate!
Node>>computeRatioForDisplay| averageRatio defaultNodeSize |averageRatio := 55.
defaultNodeSize := mainCoordinate /maximiseViewRatio.self window add:
(UINode new with: (bandWidth * averageRatio / defaultWindowSize)
…•We are forced to copy the complete method! SpecialNode>>computeRatioForDisplay
|averageRatio defaultNodeSize|averageRatio := 55.defaultNodeSize := mainCoordinate + minimalRatio /
maximiseViewRatio.self window add:
(UINode new with: (self bandWidth * averageRatio / defaultWindowSize)
…
![Page 5: Stoop ed-unit ofreuse](https://reader033.vdocuments.mx/reader033/viewer/2022061204/547ea7975806b5e55e8b46ff/html5/thumbnails/5.jpg)
S.Ducasse 5
Self sends: Plan for Reuse
Node>>computeRatioForDisplay| averageRatio defaultNodeSize |averageRatio := 55.defaultNodeSize := self defaultNodeSize.self window add:
(UINode new with: (bandWidth * averageRatio /
defaultWindowSize)Node>>defaultNodeSize^ mainCoordinate / maxiViewRatioSpecialNode>>defaultNodeSize^ mainCoordinate+minimalRatio/maxiViewRatio
![Page 6: Stoop ed-unit ofreuse](https://reader033.vdocuments.mx/reader033/viewer/2022061204/547ea7975806b5e55e8b46ff/html5/thumbnails/6.jpg)
S.Ducasse 6
Do not Hardcode Class Names
Node>>computeRatioForDisplay|averageRatio defaultNodeSize|averageRatio := 55.defaultNodeSize := mainWindowCoordinate / maximiseViewRatio.self window add:
(UINode new with: (bandWidth * averageRatio / defaultWindowSize).
...•We are forced to copy the method! SpecialNode>>computeRatioForDisplay
|averageRatio defaultNodeSize|averageRatio := 55.defaultNodeSize := mainWindowCoordinate / maximiseViewRatio.self window add:
(ExtendedUINode new with: (bandWidth * averageRatio / defaultWindowSize).
![Page 7: Stoop ed-unit ofreuse](https://reader033.vdocuments.mx/reader033/viewer/2022061204/547ea7975806b5e55e8b46ff/html5/thumbnails/7.jpg)
S.Ducasse 7
Class Factories
Node>>computeRatioForDisplay| averageRatio |averageRatio := 55.self window add:
self UIClass new with: (self bandWidth * averageRatio / self
defaultWindowSize)...
Node>>UIClass^ UINode
SpecialNode>>UIClass^ ExtendedUINode
![Page 8: Stoop ed-unit ofreuse](https://reader033.vdocuments.mx/reader033/viewer/2022061204/547ea7975806b5e55e8b46ff/html5/thumbnails/8.jpg)
S.Ducasse 8
Hook and Template
![Page 9: Stoop ed-unit ofreuse](https://reader033.vdocuments.mx/reader033/viewer/2022061204/547ea7975806b5e55e8b46ff/html5/thumbnails/9.jpg)
S.Ducasse 9
Hook and Template Methods
• Hooks: place for reuse• Template: context for reuse
![Page 10: Stoop ed-unit ofreuse](https://reader033.vdocuments.mx/reader033/viewer/2022061204/547ea7975806b5e55e8b46ff/html5/thumbnails/10.jpg)
S.Ducasse 10
Hook and Template Methods
• Templates: Context reused by subclasses• Hook methods: holes that can be specialized• Hook methods do not have to be abstract, they may
define default behavior or no behavior at all.• This has an influence on the instantiability of the
superclass.
![Page 11: Stoop ed-unit ofreuse](https://reader033.vdocuments.mx/reader033/viewer/2022061204/547ea7975806b5e55e8b46ff/html5/thumbnails/11.jpg)
S.Ducasse 11
Hook / Template Example: Printing
Object>>printString"Answer a String whose characters are a description of the receiver."
| aStream |aStream := WriteStream on: (String new: 16).self printOn: aStream.^aStream contents
![Page 12: Stoop ed-unit ofreuse](https://reader033.vdocuments.mx/reader033/viewer/2022061204/547ea7975806b5e55e8b46ff/html5/thumbnails/12.jpg)
S.Ducasse 12
Hook
Object>>printOn: aStream "Append to the argument aStream a sequence of characters that describes the receiver."
| title |title := self class name.aStream nextPutAll: ((title at: 1) isVowel ifTrue: ['an '] ifFalse: ['a ']).aStream print: self class
![Page 13: Stoop ed-unit ofreuse](https://reader033.vdocuments.mx/reader033/viewer/2022061204/547ea7975806b5e55e8b46ff/html5/thumbnails/13.jpg)
S.Ducasse 13
Overriding the Hook
Array>>printOn: aStream "Append to the argument, aStream, the elements of
the Array enclosed by parentheses."
| tooMany |tooMany := aStream position + self maxPrint.aStream nextPutAll: '#('.self do: [:element |
aStream position > tooMany ifTrue: [ aStream nextPutAll: '...(more)...)'. ^self ].
element printOn: aStream] separatedBy: [aStream space].aStream nextPut: $)
![Page 14: Stoop ed-unit ofreuse](https://reader033.vdocuments.mx/reader033/viewer/2022061204/547ea7975806b5e55e8b46ff/html5/thumbnails/14.jpg)
S.Ducasse 14
Overriding
False>>printOn: aStream "Print false."
aStream nextPutAll: 'false'
![Page 15: Stoop ed-unit ofreuse](https://reader033.vdocuments.mx/reader033/viewer/2022061204/547ea7975806b5e55e8b46ff/html5/thumbnails/15.jpg)
S.Ducasse 15
Specialization of the Hook
The class Behavior that represents a class extends the default hook but still invokes the default one.
Behavior>>printOn: aStream "Append to the argument aStream a
statement of whichsuperclass the receiver descends from."
aStream nextPutAll: 'a descendent of '.superclass printOn: aStream
![Page 16: Stoop ed-unit ofreuse](https://reader033.vdocuments.mx/reader033/viewer/2022061204/547ea7975806b5e55e8b46ff/html5/thumbnails/16.jpg)
S.Ducasse 16
Another Example: Copying
Complex (deepCopy, veryDeepCopy...)Recursive objectsGraph of connected objectsEach object wants a different copy of itselfNo up-front solution
![Page 17: Stoop ed-unit ofreuse](https://reader033.vdocuments.mx/reader033/viewer/2022061204/547ea7975806b5e55e8b46ff/html5/thumbnails/17.jpg)
S.Ducasse 17
Hook Example: Copying
Object>>copy" Answer another instance just like the receiver. Subclasses normally override the postCopy message, but some objects that should not be copied override copy. "
^self shallowCopy postCopy
Object>>shallowCopy"Answer a copy of the receiver which shares the receiver's instance variables."
<primitive: 532>
![Page 18: Stoop ed-unit ofreuse](https://reader033.vdocuments.mx/reader033/viewer/2022061204/547ea7975806b5e55e8b46ff/html5/thumbnails/18.jpg)
S.Ducasse 18
postCopy
Object>>postCopy"Finish doing whatever is required, beyond
a shallowCopy, to implement 'copy'. Answer the receiver. This message is only intended to be sent to the newly created instance. Subclasses may add functionality, but they should always do super postCopy first. "
^self
![Page 19: Stoop ed-unit ofreuse](https://reader033.vdocuments.mx/reader033/viewer/2022061204/547ea7975806b5e55e8b46ff/html5/thumbnails/19.jpg)
S.Ducasse 19
Sounds Trivial?
![Page 20: Stoop ed-unit ofreuse](https://reader033.vdocuments.mx/reader033/viewer/2022061204/547ea7975806b5e55e8b46ff/html5/thumbnails/20.jpg)
S.Ducasse 20
Hook Specialisation
Bag>>postCopy"Make sure to copy the contents fully."
| new |super postCopy.new := contents class new: contents
capacity.contents keysAndValuesDo: [:obj :count | new at: obj put: count]. contents := new.
![Page 21: Stoop ed-unit ofreuse](https://reader033.vdocuments.mx/reader033/viewer/2022061204/547ea7975806b5e55e8b46ff/html5/thumbnails/21.jpg)
S.Ducasse 21
Guidelines for Creating Template MethodsSimple implementation. Implement all the code in one method.Break into steps. Comment logical subpartsMake step methods. Extract subparts as methodsCall the step methods Make constant methods, i.e., methods doing nothing else than returning.Repeat steps 1-5 if necessary on the methods created