CSC/ECE 517 Fall 2010/ch7 7a ed

From Expertiza_Wiki
Jump to navigation Jump to search

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]

Object Runtime Evaluation

Dynamic languages allow metaprogramming, or the ability of the program to modify itself, creating or adding to objects and 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.

Reflection

Reflection is the ability of a language to inspect the different methods exist on an object. Reflection is explored further in section 1C.

Dynamically Named Methods

Dynamic languages often allow objects to handle method calls to methods that do not exist. In ruby, method_missing provides this functionality.

References

  1. Wikipedia. Dynamic programming language. 2 October 2010. http://en.wikipedia.org/wiki/Dynamic_programming_language
  2. Cornford, Richard. Javascript Closures. March 2004. http://jibbering.com/faq/notes/closures/