Design Patterns
From SlateWiki
| Table of contents |
Overview
Here are some pointers and notes to see how Slate provides support in the use of *design patterns>http://hillside.net/patterns/*.
See the book The Design Patterns Smalltalk Companion (http://www.amazon.com/exec/obidos/tg/detail/-/0201184621/002-0537198-4024851?v=glance&vi=customer-reviews) for the Smalltalk perspective.
Peter Norvig (http://norvig.com/) has a web-ified version of a presentation he gave about Design Patterns in Dynamic Programming (http://norvig.com/design-patterns/) from the Common Lisp and Dylan perspective.
Specific Examples
Prototype
Pervasive. Nearly all Slate object-creation methods are based on using some existing object as a template. There's just no overhead at all for it.
Double-dispatch
Pervasive. Dispatches can be added to any method in any position with the "@"-notation. Although sometimes a double-dispatch-like setup will be used where a single-argument method is kept separate since it gets re-used so often, and on its own. (It's generally assumed that the compiler can (eventually) optimize this out.)
Singleton
Trivial. Simply derive from Oddball or override clone to answer its receiver.
Visitor
Multiple dispatch helps with this, but also a module system that is multi-dispatch oriented is the key here, not defaulting to a method being "owned" by an object it's defined on.
Decorator
Slot-based delegation is the key here. Define a new type as a sibling of the existing type to be decorated (derive it from the first type's parent), and remove its state. Then specify a delegation slot to hold an object of the decorated type, and then a constructor method on the old type to make a new decorator for it. Finally, define the new methods and override old ones as necessary to get the desired behavior.
Factory
The factory object holds a prototype in one of its slots. Sending the appropriate message to the factory can then just call #clone or #new or some other appropriate method on the prototype and answer the result.
