Stéphane Ducasse 1
Stéphane Ducasse
stephane.ducasse@inria.fr
http://stephane.ducasse.free.fr/
Elements of Design -
Unit o...
S.Ducasse 2
Methods are Units of Reuse
Dynamic binding + methods
= reuse in subclasses
S.Ducasse 3
Methods are Unit of Reuse
S.Ducasse 4
Example: Forced to Duplicate!
Node>>computeRatioForDisplay
| averageRatio defaultNodeSize |
averageRatio := 55...
S.Ducasse 5
Self sends: Plan for Reuse
Node>>computeRatioForDisplay
| averageRatio defaultNodeSize |
averageRatio := 55.
d...
S.Ducasse 6
Do not Hardcode Class Names
Node>>computeRatioForDisplay
|averageRatio defaultNodeSize|
averageRatio := 55.
de...
S.Ducasse 7
Class Factories
Node>>computeRatioForDisplay
| averageRatio |
averageRatio := 55.
self window add:
self UIClas...
S.Ducasse 8
Hook and Template
S.Ducasse 9
Hook andTemplate Methods
• Hooks: place for reuse
• Template: context for reuse
S.Ducasse 10
Hook andTemplate Methods
• Templates: Context reused by subclasses
• Hook methods: holes that can be speciali...
S.Ducasse 11
Hook / Template Example: Printing
Object>>printString
"Answer a String whose characters are a description of ...
S.Ducasse 12
Hook
Object>>printOn: aStream
"Append to the argument aStream a sequence of characters
that describes the rec...
S.Ducasse 13
Overriding the Hook
Array>>printOn: aStream
"Append to the argument, aStream, the elements of the Array
enclo...
S.Ducasse 14
Overriding
False>>printOn: aStream
"Print false."
aStream nextPutAll: 'false'
S.Ducasse 15
Specialization of the Hook
The class Behavior that represents a class extends
the default hook but still invo...
S.Ducasse 16
Another Example: Copying
Complex (deepCopy, veryDeepCopy...)
Recursive objects
Graph of connected objects
Eac...
S.Ducasse 17
Hook Example: Copying
Object>>copy
" Answer another instance just like the receiver. Subclasses
normally over...
S.Ducasse 18
postCopy
Object>>postCopy
"Finish doing whatever is required, beyond a
shallowCopy, to implement 'copy'.Answe...
S.Ducasse 19
Sounds Trivial?
S.Ducasse 20
Hook Specialisation
Bag>>postCopy
"Make sure to copy the contents fully."
| new |
super postCopy.
new := cont...
S.Ducasse 21
Guidelines for CreatingTemplate Methods
Simple implementation.
Implement all the code in one method.
Break in...
Upcoming SlideShare
Loading in...5
×

Stoop ed-unit ofreuse

292

Published on

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
292
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Stoop ed-unit ofreuse

  1. 1. Stéphane Ducasse 1 Stéphane Ducasse stephane.ducasse@inria.fr http://stephane.ducasse.free.fr/ Elements of Design - Unit of Reuse Unit of Reuse
  2. 2. S.Ducasse 2 Methods are Units of Reuse Dynamic binding + methods = reuse in subclasses
  3. 3. S.Ducasse 3 Methods are Unit of Reuse
  4. 4. 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) …
  5. 5. 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 / maxiViewRatio SpecialNode>>defaultNodeSize ^ mainCoordinate+minimalRatio/maxiViewRatio
  6. 6. 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).
  7. 7. 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
  8. 8. S.Ducasse 8 Hook and Template
  9. 9. S.Ducasse 9 Hook andTemplate Methods • Hooks: place for reuse • Template: context for reuse
  10. 10. S.Ducasse 10 Hook andTemplate 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.
  11. 11. 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
  12. 12. 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
  13. 13. 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: $)
  14. 14. S.Ducasse 14 Overriding False>>printOn: aStream "Print false." aStream nextPutAll: 'false'
  15. 15. 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 which superclass the receiver descends from." aStream nextPutAll: 'a descendent of '. superclass printOn: aStream
  16. 16. S.Ducasse 16 Another Example: Copying Complex (deepCopy, veryDeepCopy...) Recursive objects Graph of connected objects Each object wants a different copy of itself No up-front solution
  17. 17. 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>
  18. 18. 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
  19. 19. S.Ducasse 19 Sounds Trivial?
  20. 20. 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.
  21. 21. S.Ducasse 21 Guidelines for CreatingTemplate Methods Simple implementation. Implement all the code in one method. Break into steps. Comment logical subparts Make step methods. Extract subparts as methods Call the step methods Make constant methods, i.e., methods doing nothing else than returning. Repeat steps 1-5 if necessary on the methods created
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×