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.

Self DelegationYou are using Delegation.How should you implement delegation to an object that needs reference to the delegatingobject?The issues are the same for Self Delegation as for Simple Delegation. Do you need the identity ofthe original delegating object? Do you need state from the delegating object?If the answer to either of these questions is “yes”, Simple Delegation won’t work. The delegateneeds access to the delegating object somehow.One way to give the delegate access is to include a reference from the delegate back to thedelegating object. This approach has a number of drawbacks. The backwards reference introducesadditional programming complexity. Every time the delegate changes, the reference in the olddelegate has to be destroyed and the reference in the new delegate set. More importantly, eachdelegate can only be used by one delegating object at a time. If creating multiple copies of thedelegate is expensive or impossible, this simply won’t work.The other approach, the one suggested here, is to pass the delegating object along as an additionalparameter. This introduces a variant of the original method, which isn’t great, but the additionalflexibility of this approach is worth the cost.Pass along the delegating object in an additional parameter called “for:”The Digitalk Visual <strong>Smalltalk</strong> 3.0 image has an excellent example of Self Delegation. Theimplementation of hashed collections like Sets and Dictionaries is divided into two parts. The firstis the Set or Dictionary, the second is a HashTable. There are variants of HashTable that areefficient in different circumstances. The same collection might delegate to different HashTables atdifferent times, depending on its characteristics (how big, how full, etc.)The hash value of an object is implemented differently for different kinds of Collections.Dictionaries compute hash by sending “hash”, IdentityDictionaries compute it by sending“basicHash”.This is implemented using Self Delegation. When the Collection sends a message to the HashTableto add an element, it passes itself along:Dictionary>>at: keyObject put: valueObjectself hashTableat: keyObjectput: valueObjectfor: selfThe HashTable computes the hash value by sending back a message to the Collection:HashTable>>at: keyObject put: valueObject for: aCollection| hash |hash := aCollection hashOf: keyObject....Dictionaries and IdentityDictionaries implement this message differently:<strong>Coding</strong> <strong>Patterns</strong> page 52 of 147 9/30/2006

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

Saved successfully!

Ooh no, something went wrong!