aspose file tools*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Regarding Design Patterns Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Regarding Design Patterns" Watch "Regarding Design Patterns" New topic
Author

Regarding Design Patterns

Anand Sidharth
Ranch Hand

Joined: Dec 17, 2003
Posts: 44
I was going through java design patterns notes.
Particularly the Builder pattern and Factory Pattern (most used).
I am confused about when to go for Factory Pattern and when a Builder pattern since I feel they are similar.
Can somebody help by giving some examples differntiating the two patterns ??
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
I guess your are speaking about Abstract Factory.
The way I understand it, the difference is in how many steps the product is build.
In Abstract Factory, every method is considered to return a finished product. When using a Builder, it will incrementally work on the same product with each method call, until you tell it to give you the finished product. Builder is therefore better suited for the building of complex products, where you need detailed control of the process of building.
Did that help?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
John Ipe
Greenhorn

Joined: Dec 29, 2003
Posts: 25
When you say "when to go for Factory", do you mean Abstract Factory or Factory Method?
As for when to use Builder, if you have different kinds of complex objects that need to be built, but the overall process for building them is the same, then use Builder. Here the variation would be in the individual construction steps for each complex object.
Another take might be that if you need a lot of information to construct an object, and you dont have all of that information to start with, ie. you receive the information in stages, then you could split the construction process into those very same stages in which the information becomes available. Thus you build your complex object in steps, and this process is controlled by the Builder.
I hope that is clear as well as accurate.
Regards
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
John, good points!
John Ipe
Greenhorn

Joined: Dec 29, 2003
Posts: 25
Thank you Ilja!
Still learning, and miles to go! Many, many miles!!
vasu maj
Ranch Hand

Joined: Jul 12, 2001
Posts: 395
Talking of patterns can someone throw some light on strategy pattern?
Thanks,
Vasu


What a wonderful world!
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Strategy is a neat way to let different objects (same class or not) apply different logic to a particular problem. For example my application routes work to another person so they can do the next step in the task. Some times I know the name of the next person, some times I use a lookup table for some field in my data to find the right person, some times I look at the work load for each person in the unit and send work to someone who is idle right now. These are several different strategies for routing. We could make a WorkRouter interface with three implementations and just plug in the appropriate implementation. How I decide which strategy to use and when I do it is flexible. I might say one subclass of WorkItem always uses the LookupRouter, or I might use different routers during different parts of the day. Does that help?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by John Ipe:
Still learning, and miles to go! Many, many miles!!

Trust me - it never ends! The journey is the reward...
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Moving to the Pattens forum.


"JavaRanch, where the deer and the Certified play" - David O'Meara
vasu maj
Ranch Hand

Joined: Jul 12, 2001
Posts: 395
Thnks Stan. That was a good explanation !
Vasu
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Just for grins ... State pattern is very similar. This time I choose which type of behavior I want based on the state of the object. For example babies crawl, toddlers bumble around, children run and jump, adults walk, seniors go a bit slower on stairs, etc. If I had a Sims kinda simulation I might set a person object's age and it would configure itself with the appropriate movement behavior object for its state, maybe based on age and other abilities.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Regarding Design Patterns
 
Similar Threads
Building a Decorator Question
abstract method puzzler
When to use Factory and when to use AbstractFactory ?
Java Design Pattern
about a very BIG confusion in design pattern.