There must be a million examples of software code using the builder pattern, a term coined in the GoF book [Gamma, et al.]. This is a creational pattern and its purpose is to "separate the construction of a complex object from its representation so that the same construction process can create different representations." Not too many things are more complex to a human being than a natural language, especially an unknown human language. In this article, I wish to demonstrate how a simple computer program says hello to its American and Turkish friends.
The high-level code must be simple to do this, much like a person who is bilingual and could say hello in two different languages without even thinking about the details of the languages themselves.
Using the Code
The code that talks to its American friend might look something like this:
The code that talks to its Turkish friend might look something like this:
Although there are differences in data types, or representations, the same construction process is used to show the results.
There is one speaker and two different languages. The speaker is capable of building sentences in these languages. This is how the two entities in the system are associated.
If we dig a little deeper into this code, we'll see the inner working of the builder pattern:
Builder is part of the speaker, in other words the speaker has the capability of building a language utterance in the form of a product called
Hello. Looking at the design above, the system can produce the product
Hello in two varieties:
TurkishHelloBuilder. Each of these varieties, or concrete builders must be aware of the product
Hello so that its properties can be set appropriately. In this case, the product's properties are an interjection and a noun. Both languages have these properties, but their values are different.
The order of the properties of a sentence can be different between two languages. It must follow the rules of the specific language's grammar. In the example, they happen to be a perfect match.
If we run the executable, we will see the proper greetings bilingually:
- Gamma, Erich, and et al. "Design Patterns: Elements of Reusable Object-Oriented Software"
- Addison-Wesley Professional. 1995. Note: This book is commonly referred as the Gang of Four book (GoF) because there are four authors who wrote this book.
- http://en.wikipedia.org/wiki/Builder_pattern accessed on Aug. 22, 2007 is also a very helpful resource to understand the builder design pattern.