This chapter covers
- The builder concept
- Using common builders bundled with the Groovy distribution
- Writing your own builders
Art is the imposing of a pattern on experience, and our aesthetic enjoyment is recognition of the pattern.
Alfred North Whitehead
As software developers, everything we do day in and day out is building: we build graphical applications, command-line tools, data stores, and a lot of other, often invisible products. To this end we make use of components and frameworks as building blocks assembled on a fundamental base. We build by following the rules of the architecture and the best practices of our trade.
Not surprisingly, the general task of building faces us with recurring activities and structures. Over time, developer experience has led to proven standard solutions for repetitive building tasks captured in terms of patterns. One such pattern is the Builder pattern. In this pattern, a builder object is used to help build a complex object, called the Product. It encapsulates the logic of how to assemble the product from given pieces.
Products can be complex because they maintain a tricky internal state (think of a parser object) or because they’re built of numerous objects with interdependencies. The latter case is frequently seen when there are treelike structures that you find everywhere in the world of software: