SlideShare uma empresa Scribd logo
1 de 21
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
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.
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)
…
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
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).
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
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 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.
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
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
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: $)
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 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
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
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>
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
S.Ducasse 19
Sounds Trivial?
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.
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

Mais conteúdo relacionado

Destaque (20)

Stoop 303-advanced blocks
Stoop 303-advanced blocksStoop 303-advanced blocks
Stoop 303-advanced blocks
 
Stoop sed-class initialization
Stoop sed-class initializationStoop sed-class initialization
Stoop sed-class initialization
 
Stoop sed-class initialization
Stoop sed-class initializationStoop sed-class initialization
Stoop sed-class initialization
 
10 - OOP - Inheritance (a)
10 - OOP - Inheritance (a)10 - OOP - Inheritance (a)
10 - OOP - Inheritance (a)
 
Stoop 400 o-metaclassonly
Stoop 400 o-metaclassonlyStoop 400 o-metaclassonly
Stoop 400 o-metaclassonly
 
11 bytecode
11 bytecode11 bytecode
11 bytecode
 
15 - Streams
15 - Streams15 - Streams
15 - Streams
 
Stoop ed-subtyping subclassing
Stoop ed-subtyping subclassingStoop ed-subtyping subclassing
Stoop ed-subtyping subclassing
 
01 intro
01 intro01 intro
01 intro
 
Stoop 431-visitor
Stoop 431-visitorStoop 431-visitor
Stoop 431-visitor
 
Stoop 300-block optimizationinvw
Stoop 300-block optimizationinvwStoop 300-block optimizationinvw
Stoop 300-block optimizationinvw
 
Stoop sed-sharing ornot
Stoop sed-sharing ornotStoop sed-sharing ornot
Stoop sed-sharing ornot
 
Stoop ed-some principles
Stoop ed-some principlesStoop ed-some principles
Stoop ed-some principles
 
5 - OOP - Smalltalk in a Nutshell (c)
5 - OOP - Smalltalk in a Nutshell (c)5 - OOP - Smalltalk in a Nutshell (c)
5 - OOP - Smalltalk in a Nutshell (c)
 
08 refactoring
08 refactoring08 refactoring
08 refactoring
 
11 - OOP - Numbers
11 - OOP - Numbers11 - OOP - Numbers
11 - OOP - Numbers
 
Stoop 440-adaptor
Stoop 440-adaptorStoop 440-adaptor
Stoop 440-adaptor
 
Stoop 430-design patternsintro
Stoop 430-design patternsintroStoop 430-design patternsintro
Stoop 430-design patternsintro
 
Stoop 436-strategy
Stoop 436-strategyStoop 436-strategy
Stoop 436-strategy
 
Stoop 414-smalltalk elementsofdesign
Stoop 414-smalltalk elementsofdesignStoop 414-smalltalk elementsofdesign
Stoop 414-smalltalk elementsofdesign
 

Semelhante a Stoop ed-unit ofreuse

From Java to Scala - advantages and possible risks
From Java to Scala - advantages and possible risksFrom Java to Scala - advantages and possible risks
From Java to Scala - advantages and possible risksSeniorDevOnly
 
Programming python quick intro for schools
Programming python quick intro for schoolsProgramming python quick intro for schools
Programming python quick intro for schoolsDan Bowen
 
Pydiomatic
PydiomaticPydiomatic
Pydiomaticrik0
 
High-Performance Haskell
High-Performance HaskellHigh-Performance Haskell
High-Performance HaskellJohan Tibell
 
Intro to Reactive Thinking and RxJava 2
Intro to Reactive Thinking and RxJava 2Intro to Reactive Thinking and RxJava 2
Intro to Reactive Thinking and RxJava 2JollyRogers5
 
4 - OOP - Taste of Smalltalk (Tamagoshi)
4 - OOP - Taste of Smalltalk (Tamagoshi)4 - OOP - Taste of Smalltalk (Tamagoshi)
4 - OOP - Taste of Smalltalk (Tamagoshi)The World of Smalltalk
 
error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docx
error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docxerror 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docx
error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docxSALU18
 
Python programing
Python programingPython programing
Python programinghamzagame
 
Simple, fast, and scalable torch7 tutorial
Simple, fast, and scalable torch7 tutorialSimple, fast, and scalable torch7 tutorial
Simple, fast, and scalable torch7 tutorialJin-Hwa Kim
 
slide-keras-tf.pptx
slide-keras-tf.pptxslide-keras-tf.pptx
slide-keras-tf.pptxRithikRaj25
 

Semelhante a Stoop ed-unit ofreuse (20)

Double Dispatch
Double DispatchDouble Dispatch
Double Dispatch
 
Stoop ed-class forreuse
Stoop ed-class forreuseStoop ed-class forreuse
Stoop ed-class forreuse
 
9 - OOP - Smalltalk Classes (a)
9 - OOP - Smalltalk Classes (a)9 - OOP - Smalltalk Classes (a)
9 - OOP - Smalltalk Classes (a)
 
9 - OOP - Smalltalk Classes (c)
9 - OOP - Smalltalk Classes (c)9 - OOP - Smalltalk Classes (c)
9 - OOP - Smalltalk Classes (c)
 
9 - OOP - Smalltalk Classes (b)
9 - OOP - Smalltalk Classes (b)9 - OOP - Smalltalk Classes (b)
9 - OOP - Smalltalk Classes (b)
 
Stoop 413-abstract classes
Stoop 413-abstract classesStoop 413-abstract classes
Stoop 413-abstract classes
 
Solid scala
Solid scalaSolid scala
Solid scala
 
8 - OOP - Syntax & Messages
8 - OOP - Syntax & Messages8 - OOP - Syntax & Messages
8 - OOP - Syntax & Messages
 
From Java to Scala - advantages and possible risks
From Java to Scala - advantages and possible risksFrom Java to Scala - advantages and possible risks
From Java to Scala - advantages and possible risks
 
Programming python quick intro for schools
Programming python quick intro for schoolsProgramming python quick intro for schools
Programming python quick intro for schools
 
Pydiomatic
PydiomaticPydiomatic
Pydiomatic
 
Python idiomatico
Python idiomaticoPython idiomatico
Python idiomatico
 
High-Performance Haskell
High-Performance HaskellHigh-Performance Haskell
High-Performance Haskell
 
Intro to Reactive Thinking and RxJava 2
Intro to Reactive Thinking and RxJava 2Intro to Reactive Thinking and RxJava 2
Intro to Reactive Thinking and RxJava 2
 
4 - OOP - Taste of Smalltalk (Tamagoshi)
4 - OOP - Taste of Smalltalk (Tamagoshi)4 - OOP - Taste of Smalltalk (Tamagoshi)
4 - OOP - Taste of Smalltalk (Tamagoshi)
 
error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docx
error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docxerror 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docx
error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docx
 
Tt subtemplates-caching
Tt subtemplates-cachingTt subtemplates-caching
Tt subtemplates-caching
 
Python programing
Python programingPython programing
Python programing
 
Simple, fast, and scalable torch7 tutorial
Simple, fast, and scalable torch7 tutorialSimple, fast, and scalable torch7 tutorial
Simple, fast, and scalable torch7 tutorial
 
slide-keras-tf.pptx
slide-keras-tf.pptxslide-keras-tf.pptx
slide-keras-tf.pptx
 

Mais de The World of Smalltalk (17)

05 seaside canvas
05 seaside canvas05 seaside canvas
05 seaside canvas
 
99 questions
99 questions99 questions
99 questions
 
13 traits
13 traits13 traits
13 traits
 
12 virtualmachine
12 virtualmachine12 virtualmachine
12 virtualmachine
 
09 metaclasses
09 metaclasses09 metaclasses
09 metaclasses
 
07 bestpractice
07 bestpractice07 bestpractice
07 bestpractice
 
06 debugging
06 debugging06 debugging
06 debugging
 
05 seaside
05 seaside05 seaside
05 seaside
 
04 idioms
04 idioms04 idioms
04 idioms
 
03 standardclasses
03 standardclasses03 standardclasses
03 standardclasses
 
02 basics
02 basics02 basics
02 basics
 
Stoop sed-smells
Stoop sed-smellsStoop sed-smells
Stoop sed-smells
 
Stoop metaclasses
Stoop metaclassesStoop metaclasses
Stoop metaclasses
 
Stoop ed-lod
Stoop ed-lodStoop ed-lod
Stoop ed-lod
 
Stoop ed-frameworks
Stoop ed-frameworksStoop ed-frameworks
Stoop ed-frameworks
 
Stoop ed-dual interface
Stoop ed-dual interfaceStoop ed-dual interface
Stoop ed-dual interface
 
Stoop 450-s unit
Stoop 450-s unitStoop 450-s unit
Stoop 450-s unit
 

Último

MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKJago de Vreede
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfOverkill Security
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 

Último (20)

MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 

Stoop ed-unit ofreuse

  • 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. S.Ducasse 2 Methods are Units of Reuse Dynamic binding + methods = reuse in subclasses
  • 3. S.Ducasse 3 Methods are Unit of Reuse
  • 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. 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. 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. 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
  • 9. S.Ducasse 9 Hook andTemplate Methods • Hooks: place for reuse • Template: context for reuse
  • 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. 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. 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. 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. S.Ducasse 14 Overriding False>>printOn: aStream "Print false." aStream nextPutAll: 'false'
  • 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. 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. 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. 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
  • 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. 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