File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding Design Patterns

 
Anand Sidharth
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
John Ipe
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John, good points!
 
John Ipe
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ilja!
Still learning, and miles to go! Many, many miles!!
 
vasu maj
Ranch Hand
Posts: 396
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Talking of patterns can someone throw some light on strategy pattern?
Thanks,
Vasu
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to the Pattens forum.
 
vasu maj
Ranch Hand
Posts: 396
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thnks Stan. That was a good explanation !
Vasu
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic