This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I suppose it depends on what you're reading. For beginners to any subject, it's easier to use a book or tutorial that uses a lot of informal descriptions. I agree that the intent you describe here does not distinguish the Builder pattern from say, the Abstract Factory pattern.
The difference between the two is that an Abstract Factory creates objects in one step, while the Builder creates an object in multiple steps. This is useful for when you can't or don't want to supply all the information needed to create the object in one big ugly step.
Let's take the StringBuilder class as an example. Let's say you have an array full of Strings, which you all want to paste together to make one big String. The first thing you might try is the following:
This will work, but performance will be absolutely horrible. Strings are immutable, and in order to concatenate two Strings, a new String with new memory will be created to hold the information of the first two Strings. This repeats for every String in the array. Instead, it's better to use a StringBuilder. We tell the builder about all the Strings we want to paste together, and at the end tell it to build the String. This way, it will only construct one String at the very end, with enough space to hold all the Strings in the array:
The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Joined: Jul 17, 2008
This is excellent reply, Thanks very much for clarification.