Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

Using the Builder Design Pattern for Multilingual Interaction

2.50/5 (3 votes)
22 Aug 2007CPOL2 min read 1   271  
Description of C++ code that illustrates building of a simple sentence in English and Turkish

Introduction

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:

C++
AmericanHelloBuilder americanHelloBuilder;
speaker.SetHelloBuilder(&americanHelloBuilder);
speaker.ConstructHello()->getHello()->ShowHello();

The code that talks to its Turkish friend might look something like this:

C++
TurkishHelloBuilder turkishHelloBuilder;
speaker.SetHelloBuilder (&turkishHelloBuilder);
speaker.ConstructHello()->getHello()->ShowHello();

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:

Screenshot - BuilderDesignPattern.jpg

The abstract class 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: AmericanHelloBuilder and 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.

Executable Output

If we run the executable, we will see the proper greetings bilingually:

Screenshot - Result.jpg

References

  • 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)