13.07.2015 Views

Smalltalk Best Practice Patterns Volume 1: Coding - Free

Smalltalk Best Practice Patterns Volume 1: Coding - Free

Smalltalk Best Practice Patterns Volume 1: Coding - Free

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

marriedMenAndUnmarriedWomen^self marriedMen , self unmarriedWomenbut that doesn’t demonstrate this pattern very well. If several layers of methods are involved orseveral objects, it is clearer to modify the sub-methods. Instead of returning a Collection, each addsits objects to a Collection. The code then becomes:marriedMenAndUnmarriedWomen| result |result := OrderedCollection new.self addMarriedMenTo: result.self addUnmarriedWomenTo: result.^resultaddMarriedMenTo: aCollectionself people do: [:each | each isMarried & each isMan ifTrue: [aCollection add: each]]addUnmarriedWomenTo: aCollectionself people do: [:each | each isUnmarried & each isWoman ifTrue: [aCollection add: each]]This code contains fewer lines and is more direct than the original. (If this were production code, Iwould probably continue factoring via Composed Method to concentrate the similarities betweenaddMarriedMenTo: and addUnmarriedWomenTo:.)In general, use an OrderedCollection as the Collecting Parameter. You may use a ConcatenatingStream as the Collecting Parameter if the objects be collected are bytes or Characters. Use a Set ifyou want to avoid duplicates.<strong>Coding</strong> <strong>Patterns</strong> page 58 of 147 9/30/2006

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!