Click here to Skip to main content
Licence CPOL
First Posted 22 Aug 2007
Views 12,882
Downloads 96
Bookmarked 14 times

Using the Builder Design Pattern for Multilingual Interaction

By | 22 Aug 2007 | Article
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:

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

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

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)

About the Author

Erol Esen

Software Developer (Senior)

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 22 Aug 2007
Article Copyright 2007 by Erol Esen
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid