• 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

Builder & Strategy Patterns?

 
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't find the difference between Builder pattern and Strategy pattern in Gamma's Design Patterns. Both of them have similar UML represetation. Could anyone tell me how to distinguish Builder from Strategy?
Thanks.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One major difference is where they are used. Builder is a creational pattern and is used to create complex objects. Builder knows how to create( and sometimes assemble) objects and is used to encapsulate the complexity from the clients.
Strategy on the other hand is a behavioral pattern ie., it is to do different "things" with different objects at runtime. More specifically strategy helps you dynamically choose a processing algorithm and encapsulate such dynamics from the client.
 
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When we think of a design pattern, we would be looking at its intent, i.e., the problem at hand it is going to address, as in this case complex creation logic, vs choosing appropriate algorithm at runtime. The UML representation of class diagrams(static pictures) may be similar, but they don't talk about when the objects get created, and all such dynamic details.
 
Steve Taiwan
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear
Thank you for the prompt reply.
This is the way I am thinking in. Suppose we have a TextEditor and it is trying to read a unknow format text which was edited by another application. The TextEditor we now are using doesn't know how to create an object of this unknow format text for presenting it to users. Therefore, we could use "Builder Pattern" to create the "complex" object for futher use, because our TextEditor has no way to know the format which the other application are using. However, we might need process lots of algorithms in order to create this object( now we need "Strategy Pattern" to do this) because we have to do a plug-in function by ourselves to parse the unknow format.
And then both 2 patterns are being used at the same time.
That's why I am very confused.
Do I think in a wrong way???
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Steve,
very good example...but i tend to disagree with u on builder part. See, when u already have strategy pattern to formulate diff algo for diff text formats u really wont go for builder here. I mean to say, consider a real time example of say 100 text formats of which say 90 r generally known. So whenever my application come across such formats which are not known, i have an algo to deal with. Introducing a builder will might make the system unneccesarily heavier for the remaining 90 known ones....ur opinion awaited..
 
Steve Taiwan
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Nishant Anshul
I think you are right but I would like to add some points to your solution.
In fact, as time goes by, I believe that more and more unknown text formats could appear and it is hard for us to catch up with such many different text formats. So in the first release, we might use "strategy patterns" to have one algorithm to uniformally handle 10% unknow text formats. After second version or third version, we might introduce different new parsers, which use "Builder patter" to fit our customers' requirements and to correctly parse these unknow text formats as many as possible.
What do you think?
 
Nishant Anshul
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Steve its getting more complex i think now...
While I totally agree with Ajith and Jayadev in generics of this matter, i must say that taking this particular example of text editor...one may have many solutions and resons to claim. My design would be ---
Start with a strategy pattern... which will check if the incoming format is registered with my system then the object will be of type Strgy_Known class (say for example) else Strgy_Unknown class, both implementing a common interface. Now how to create that object. For known formats, if it is a simple call then "Strgy_Known"'s own function will handle , and for all unknown formats another different strategy pattern.
Advt of second line strategy pattern is, with course of time when formats become acceptable u can easily move them to front line strategy handling.
Also u can a common algo to reject the input when its junk for u. Plus if u go for builder as second line, u really dont know if u have the same process for creation for unkown objects..
Disadvt is its complexity. But its interesting to have such nested strategy patterns.. as long as u have provided cleaner logic for handling unforseen situations.
thnx n rgds
 
reply
    Bookmark Topic Watch Topic
  • New Topic