Builder vs Factory Patterns

Hi,I read about builder and factory patterns. But both of them look quite similar. Can u plz help me to understand the difference between them.Thanking You,Chamal.
[198 byte] By [chamalsla] at [2007-10-2 8:25:47]
# 1

This is the way I think of them. I'm not sure if it's 100% correct.

Factory is more like a virtual constructor. It can return different subtypes depending on user input. (Simple objects; many different return types.)

I think of Builder as a way to assemble a complex Object from its constitutive parts. (Complex object composed of many; single return type.)

I can see how a Builder might use different Factories to assemble that complex object. So the two don't have to be exclusive.

%

duffymoa at 2007-7-16 22:25:43 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

I puzzled over this too. The UML is almost identical for both patterns. A great example of how UML class diagram leave a lot to be desired.

This is my take, which may not be absolutely correct or orthodox.

The difference is that in the abstract factory, the point is to hide the implementation of the abstract type. In the builder it's other way around. The point is to hide the implementation of the callers of the builder or really, what is being built.

So a user of a abstract factory should not know (or care) about what type the abstract factory returns. In the builder pattern, the code could potentially make references to specific builder types (a text builder for debugging or console mode, for example) but the implementation of what is being built is not known to the builder.

You can easily combine the two and get a lot of flexibility.

dubwaia at 2007-7-16 22:25:43 > top of Java-index,Other Topics,Patterns & OO Design...