CSC/ECE 517 Fall 2010/ch7 7a ed
Novel Implementations of Design Patterns in Dynamic Languages
Dynamic languages such as Ruby and Javascript allow many novel implementations of the design patterns we cover in previous chapters, which would not be possible or feasible in complied languages such as Java and C. Language features only available in dynamic languages give developers an increased amount of flexibility in how they can implement design patterns. These language features and the some examples of the implementations they allow are detailed in this section.
Language Features
"These behaviors can be emulated in nearly any language of sufficient complexity, but dynamic languages provide direct tools to make use of them" [1]. Some of the most helpful tools are as follows:
Closures
Closures are a feature that allow blocks of code to be passed around as arguments to functions. Closures are also known as lambdas or procs in Ruby. The code in a closure has access to all variables in the scope where it was created, and it can have additional data passed in as parameters to the closure from the caller. [2]
Metaprogramming
Dynamic languages allow metaprogramming, or the ability of the program to modify itself, creating or adding to classes. This is explored further in section 4G.
eval
In dynamic languages, any arbitrary code can be executed dynamically at runtime. This can include code that is generated from user input. This is a subset of metaprogramming. Eval is explored further in section 3E.
References
- Wikipedia. Dynamic programming language. 2 October 2010. http://en.wikipedia.org/wiki/Dynamic_programming_language
- Cornford, Richard. Javascript Closures. March 2004. http://jibbering.com/faq/notes/closures/