aspose file tools*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Understanding Builder design pattern? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Understanding Builder design pattern?" Watch "Understanding Builder design pattern?" New topic
Author

Understanding Builder design pattern?

Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
It is not really tough to understand patterns. The real challenge comes when you have to distinct all the 23 patterns one from each other just by intent.

So now I'm stumbling on Builder design pattern.


Intent

Defines an instance for creating an object but letting subclasses decide which class to instantiate
Refers to the newly created object through a common interface

If you look above intent, this is pretty general statement, and it almost applies to all design patterns since almost all use abstract implementation.

In short I do see going thru diagrams only make me really understand the pattern otherwise it's all cramming business.

May you can put your insight forward, which I can use as a way to get the DP's in to my brain.

In short I want to say what is represented in diagram is not one to one with the intent described. Do you agree with this?



SCJP 1.4, SCWCD 5, SCBCD 5, OCPJWSD 5,SCEA-1, Started Assignment Part 2
My blog- http://rkydesigns.blogspot.com
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3611
    
  14

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:
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
This is excellent reply, Thanks very much for clarification.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Understanding Builder design pattern?