Groovy for Domain-specific Languages - Second Edition

(nextflipdebug2) #1

Power Groovy DSL Features


[ 146 ]

The builder design pattern


It would be useful to have a means of constructing objects such that the method
of construction was hidden. Enter the builder design pattern. The concept of a
design pattern comes originally from the architectural profession in the late 1970s.
In building architecture, a design pattern refers to the reuse of design solutions
for similar problems. In office complexes, the locating of stairwells, elevators,
and bathrooms around central service columns is a typical design pattern.


Using such a design pattern, architects designing large office buildings can
quickly lay out floor after floor of the building by repeating the layouts around the
service columns on each floor. This leaves more time and effort to be expended on
developing the functional work areas and aesthetics of the building. This not only
benefits the architect but also benefits the user of the building.


No matter where we travel, whether it is to Bangkok, San Francisco, Paris, or
London, it's usually not too difficult to find a bathroom, presuming we can
remember how to find our way back to the elevator that we came up in.
When we do, we are benefiting from the application of a design pattern.


Design patterns are seldom invented. Instead, they are usually observed in existing
buildings and catalogued. Good design evolves over time and is repeated again and
again. By exploiting existing design patterns, the architect can rely on the experience
of generations of previous building projects and be sure that at least these elements
of the building will work as expected.


Design patterns began to be observed and catalogued in software engineering in
the late eighties. Ward Cunningham and Kent Beck wrote one of the first conference
papers on the subject at OOPSLA 1987. By 1994, the seminal work by Eric Gamma,
Richard Helm, Ralph Johnson, and John Vlissides, also known as the Gang of Four—
Design Patterns: Elements of Reusable Object-Oriented Software, Addison Wesley—was
published, which catalogued over 20 reusable design patterns, including the Model
View Controller (MVC), and Factory and Builder patterns.


These authors were not claiming to have invented all of these patterns. For each of
the patterns, they listed source systems where the patterns could be observed in use.
Some came from ET++, an object-oriented framework developed by Gamma and
others at Taligent. Many of the patterns, including MVC and Builder, originated
from the Smalltalk language and framework.


http://www.ebook3000.com
Free download pdf