This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Architect Certification (SCEA/OCMJEA) and the fly likes Builder & Strategy Patterns? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Architect Certification (SCEA/OCMJEA)
Bookmark "Builder & Strategy Patterns?" Watch "Builder & Strategy Patterns?" New topic
Author

Builder & Strategy Patterns?

Steve Taiwan
Ranch Hand

Joined: Jul 01, 2003
Posts: 166
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.


Steve Taiwan<br />SCJP 1.2, SCJD 1.4, SCWCD 1.3, SCBCD 1.3, OCP 8i DBA, SCEA
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
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.


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Jayadev Pulaparty
Ranch Hand

Joined: Mar 25, 2002
Posts: 662
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

Joined: Jul 01, 2003
Posts: 166
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???
Nishant Anshul
Ranch Hand

Joined: Sep 10, 2003
Posts: 72
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..


SCEA-I, BCJP<p>IBM (India)
Steve Taiwan
Ranch Hand

Joined: Jul 01, 2003
Posts: 166
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

Joined: Sep 10, 2003
Posts: 72
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
 
wood burning stoves
 
subject: Builder & Strategy Patterns?
 
Similar Threads
Factory Vs Abstract Factory Pattern
Regarding Design Patterns
Design Patterns & J2EE
about a very BIG confusion in design pattern.
Builder and Composite Patterns?