• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Understanding Builder design pattern?

 
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?


 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is excellent reply, Thanks very much for clarification.
 
reply
    Bookmark Topic Watch Topic
  • New Topic