dcsimg
Kindly differentiate the FactoryMethod, Abstract Factory and Builder
3 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   James_Varkey
Posted On:   Thursday, October 31, 2002 01:47 AM


Can someone give me difference between FactoryMethod, Abstract Factory and Builder patterns of GoF with UML class and interaction diagrams and javacode(if possible).


I tried Design potterns by Erich Gamma et.al But could not digest the difference. Thank you. The FAQ is really great

Re: Kindly differentiate the FactoryMethod, Abstract Factory and Builder

Posted By:   Joe_Felchlin  
Posted On:   Friday, December 6, 2002 02:43 AM

I'm not going to provide diagrams or much code, but briefly:




Abstract Factory: You want the Client to be able to interact with a family of related Products while having a very loose coupling to the concrete factories which are creating products and concrete products that it is really using. The AbstractFactory class often uses a Factory Method to create concrete products (see Factory Method).




Factory Method: Let subclasses decide which class to instantiate. Here's an example:




public abstract class Creator

{

  public abstract Product FactoryMethod();

  public void AnOperation() {

    product = FactoryMethod();

  }

}



public class ConcreteCreatorA

  extends Creator

{

  public Product FactoryMethod() {

    return new ConcreteProductA();

  }

}




public class ConcreteCreatorB

  extents Creator

{

  public Product FactoryMethod() {

    return new ConcreteProductB();

  }

}




Here, ConcreteProductA and ConcreteProductB are both subclasses of the Product abstract class.



So you see, the Factory Method pattern is utilized by the Abstract Factory pattern (and by many others as well).




Builder: Describes a way of isolating the algorithm of building a complex object from the discrete steps of building the parts of that object.



The key players of Builder are the Director and Builder.
The scheme is as follows:



1 - A client creates a Director object, passing it the desired concrete builder.

2 - The director notifies the builder whenever a part of the product should be built.

3 - The builder handles requests from the director and adds parts to the product.

4 - The client retrieves the complete part from the builder.




I will not provide code here since it can be be lengthy and can have several implementations.




In summary, the Builder pattern is concerned with the separation of responsibilites in the construction of a complex object into Director and Builder classes. It is used when the algorithm for creating a complex object should be independent of the parts that make up the object and how they're assembled. Neither the Abstract Factory nor the Factory Method concern themselves with building objects part-by-part nor specify any particular strategy for multi-step object construction. The Builder pattern may indeed be used in conjunction with Abstract Factory and/or Factory Method when complex objects must be built.

Re: Kindly differentiate the FactoryMethod, Abstract Factory and Builder

Posted By:   David_Liu  
Posted On:   Friday, November 1, 2002 07:39 AM

I have never read the other java design book, but I own and like this one. It's a good complement to the orginal GOF pattern book.


Patterns in Java: A Catalog of Reusable Design Patterns Illustrated with UML, 2nd Edition, Volume 1
by Mark Grand

Re: Kindly differentiate the FactoryMethod, Abstract Factory and Builder

Posted By:   James_Varkey  
Posted On:   Thursday, October 31, 2002 01:53 AM

I'm sorry . I repeated the question by mistake ( I am a novice to FAQs). But Could anyone give me a clear difference with UML diagrams.Please..
About | Sitemap | Contact