0% found this document useful (0 votes)
182 views

Design Patterns - in Dynamic Languages Presentation

"Design patterns" define common problems dynamic languages give you better tools. Redux iterator Provide a way to access the elements of an aggregate object sequentially. Closures encapsulate a request as an object, thereby letting you parameterize clients.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
182 views

Design Patterns - in Dynamic Languages Presentation

"Design patterns" define common problems dynamic languages give you better tools. Redux iterator Provide a way to access the elements of an aggregate object sequentially. Closures encapsulate a request as an object, thereby letting you parameterize clients.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 143

ThoughtWorks

“design patterns”
in dynamic languages
NEAL FORD software architect / meme wrangler

ThoughtWorks
[email protected]
3003 Summit Boulevard, Atlanta, GA 30319
www.nealford.com
www.thoughtworks.com
memeagora.blogspot.com
ThoughtWorks
pattern solutions from weaker languages

have more elegant solutions


blended whisky
is made from
several
single malts
cask strength
Making
C++
Suck
Less
patterns define common
problems

dynamic languages give you


better tools to solve those
problems
“Go To Statement
Considered Harmful”
Edsger W. Dijkstra
Communications of the ACM, Vol. 11, No. 3, March 1968, pp. 147-148.
Copyright © 1968, Association for Computing Machinery, Inc.
Your biological and technological distinctiveness will
be added to our own. Resistance is futile.
the gof patterns redux
iterator
Provide a way to access the elements of an aggregate
object sequentially without exposing its underlying
representation.
ceremony

vs.
essence
internal vs. external
iterators
.each in groovy & ruby are internal iterators

groovy provides external iterators via


iterator()

ruby 1.8 has no default syntax for external


iterators

ruby 1.9 adds enumerators on collections


external iterator

1.9
command
Encapsulate a request as an object, thereby letting you
parameterize clients with different requests, queue or log
requests, and support undoable operations.
commands == closures
Encapsulate a request as an object, thereby letting you
parameterize clients with different requests, queue or log
requests, and support undoable operations.
command design pattern is
built into languages
with closures

add structure as needed


strategy
Define a family of algorithms, encapsulate each one, and
make them interchangeable. Strategy lets the algorithm
vary independently from clients that use it.
why bother with
extra structure?
Execution in
the Kingdom
of Nouns

steve
yegge
builder
Separate the construction of a complex object from its
representation so that the same construction process
can create different representations.
“traditional” builder
add dynamic behavior: ad
hoc combinations
in computer_builder:
template
Define the skeleton of an algorithm in an operation,
deferring some steps to subclasses. Template Method
lets subclasses redefine certain steps of an algorithm
without changing the algorithm’s structure.
what’s dynamic?

so far, this is the “traditional” template-method

why not blocks as placeholders


what about protection?
interpreter
Given a language, define a representation for its
grammar along with an interpreter that uses the
representation to interpret sentences in the language.
building domain specific
languages atop groovy
the goal
expando property
open classes for recipes
recipe redux
of

why
grams & not
lbs?
of redux
who returns what?
BigDecimal Ingredient

Integer Ingredient
type transmogrification
transform types as needed as part of a
fluent interface call
embedded interpreter
what is this?
method “bubble” word

1st parameter 2nd parameter(s)


warning! do not try to parse text using
regular expressions!
internal dsl’s == embedded interpreter
factory
Define an interface for creating an object, but let
subclasses decide which class to instantiate. Factory
method lets a class defer instantiation to subclasses.
search exists on Stack

but not on ArrayList


or List!
strong
type? nice try,
java guy
decorator
Attach additional responsibilities to an object
dynamically. Decorators provide a flexible alternative to
subclassing for extending functionality.
“traditional” decorator
step 1: make it more
dynamic
step 2: decorate in place
decoration via
smart dispatch
the ultimate decorator:
the recorder pattern
adapter
Convert the interface of a class into another interface
clients expect. Adapter lets classes work together that
couldn’t otherwise because of incompatible interfaces.
step 1: “normal” adaptor
why bother with extra
adaptor class?
what if open class added
adaptor methods clash with
existing methods?
interface helper
compilation ==
premature optimization
dynamic
language

patterns
null object
The null object pattern uses a special object
representing null, instead of using an actual null. The
result of using the null object should semantically be
equivalent to doing nothing.
java.lang.NullPointerException
this pattern doesn't
exist in ruby

NilClass is already
defined
aridifier
dry

don’t
repeat
yourself
ceremonious languages
generate floods
essence
languages
allow
aridification
moist
tests?
drier tests
“traditional” design patterns rely
heavily on structure to solve problems
dynamic languages use
language facilities to create
simpler solutions
understand patterns
for what they are...

descriptions of
common problems
implement solutions that
take advantage of your tools
ThoughtWorks

questions?
please fill out the session evaluations
slides & samples available at nealford.com

NEAL FORD software architect / meme wrangler

ThoughtWorks
This work is licensed under the Creative Commons
[email protected]
Attribution-Noncommercial-Share Alike 2.5 License. 3003 Summit Boulevard, Atlanta, GA 30319
www.nealford.com
http://creativecommons.org/licenses/by-nc-sa/2.5/ www.thoughtworks.com
memeagora.blogspot.com
ThoughtWorks

resources

Execution in the Kingdom of Nouns Steve Yegge


http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html

Design Patterns in Groovy on groovy.codehaus.org


http://groovy.codehaus.org/Design+Patterns+with+Groovy
Design Patterns in Ruby
Russ Olsen

You might also like