wood burning stoves*
The moose likes Architect Certification (SCEA/OCMJEA) and the fly likes Patterns: Abstract Factory Vs Factory Method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Architect Certification (SCEA/OCMJEA)
Bookmark "Patterns: Abstract Factory Vs Factory Method" Watch "Patterns: Abstract Factory Vs Factory Method" New topic
Author

Patterns: Abstract Factory Vs Factory Method

Susan
Ranch Hand

Joined: Apr 26, 2005
Posts: 54
Hi,

looking closely at the abstract factory & factory method patterns I'm sort of wondering whats the difference between them is.

As, I could easily change the factory method pattern to be the abstract factory pattern by implementing a new factory (deriving from the 'Creator' interface) and implementing a new object hierarchy (deriving from the interface returned from the factory) ?? Note that changing from factory method to abstract factory would not change any existing implementation.

Finally, the factory method pattern defines an interface for creating an object but lets the subclass decide which class to create -- this is also what the abstract factory pattern does.

thanks,

Susan
Santiago Urrizola
Ranch Hand

Joined: Apr 27, 2006
Posts: 172
remember this .. abstract factory create a family of related objects ... there is the diference ...


Santiago Urrizola : La Plata - Argentina<br />SCEA (89%-92%)<br /><a href="http://gpitech.wordpress.com/" target="_blank" rel="nofollow">http://gpitech.wordpress.com/</a>
Michel Bertrand
Greenhorn

Joined: Jan 31, 2003
Posts: 26
Regarding these two creational patterns, does home interface (EJBHome) fit better as Abstract Factory or Factory Method ?
John Waugh
Greenhorn

Joined: May 01, 2006
Posts: 6
Hi,

EJBHome is an example of Factory Pattern.

Regards,
B.Sathish
Ranch Hand

Joined: Aug 18, 2005
Posts: 372
Its debatable. I can argue that EJBHome is an example of Abstract Factory. The client composes the home interface and calls a create method to get the component interface. Because composition is used, one can say it is an Abstract Factory. The home interface is the abstract factory and the EJB container creates the implementation. The component interface is the abstract product and the implementation is again created by the container. The client composes the abstract factory (home interface) and calls a create method on the abstract factory to create the product. Because composition is used, this is a case of an abstract factory that happens to have only 1 product in its family (the EJBObject implementation)
John Waugh
Greenhorn

Joined: May 01, 2006
Posts: 6
So, this can be interprated as abstract factory pattern is also factory pattern but factroy pattern is not nessarily abstract factory one.
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2922
    
    5
See also related discussion under Builder Pattern Question.

Originally posted by Santiago Urrizola:
.. abstract factory creates a family of related objects ...


Correct. Also keep in mind that the factory method is a building block in the Abstract Factory Pattern.
Furthermore Factory Method sometimes refers to a looser concept than initially defined in GOF. You see an example of this in Fowler's "Replace Constructor with Factory Method". Notice that the factory method does not "defer instantiation to subclasses", nor is there any sign of an interface method that must be implemented, nor could it be because here the factory method is a (static) class method.

Originally posted by John Waugh:
EJBHome is an example of Factory Pattern.


It would be more accurate to state that the EJB create method is an example of the Factory Method Pattern. However GOF purist could nitpick and say that is not a Factory Method because the create method isn't part of the EJBHome interface - it only exists due to conventions set up by the EJB specification. So really, the EJB create method isn't a Factory Method in the strict GOF sense. However the intent of the EJB create method is consistent with Fowler's usage of the term Factory Method.

Originally posted by B Sathish:
Because composition is used, one can say it (EJBHome) is an Abstract Factory.


Actually the fact that the returned object is a composite has not bearing in the Abstract Factory pattern. Ultimately EJBHome is about the creation of a representation of a single type of object � the bean that it serves. The finder methods simply return collections of the same representation and in my book that does not constitute "creating families of related or dependent objects". The remaining methods are helper methods that have nothing to do with creation (we are talking about creational patterns here). So I would not cite EJBHome as an example of an Abstract Factory.

A better candidate for an Abstract Factory is the JDBC driver interface as described by the specification. The driver most definitely creates "families of related or dependent objects". However most people never notice because they actually implement against a particular vendor implementation using a vendor dialect of SQL. However it is possible to only use the functions as specified by the specification to achieve "plug-ability" of various drivers implementing the same level of the specification. Of course you would need another Abstract Factory to provide you with the vendor-dependent SQL renditions (and dependent objects) for that to work.
[ May 06, 2006: Message edited by: Peer Reynders ]
Thomas Taeger
Ranch Hand

Joined: Dec 16, 2002
Posts: 307
Hello Peer,

thanks for the great clarification (and the time you might have spent on it!). Great!

Originally posted by Peer Reynders:
Factory Method sometimes refers to a looser concept than initially defined in GOF. You see an example of this in Fowler's "Replace Constructor with Factory Method". Notice that [there] the factory method does not "defer instantiation to subclasses", nor is there any sign of an interface method that must be implemented, nor could it be because here the factory method is a (static) class method.

This differenc might be the reason for getting this question again and again.

First, as a Java developer, I thought of static factory methods only and did not understand the world anymore when reading "the" changed definition like "defer instantiation to subclasses".

The static method understanding is the more intuitive one, is not wrong but not the official whole, so everybody should define which strategy of this pattern he is talking about.

It is sad that the authors of "defer instantiation to subclasses" did not mention the strategy of a static method too.

Thomas


www.classic-and-class.com - www.evalulearn.com
Interfaces are the glue of OO.
Rajesh Velicheti
Greenhorn

Joined: Jan 09, 2005
Posts: 4
Can some one please clarify this notion of saying FactoryMethod --> Inheritence, AbstractFactory --> Composition.
I read this in a lot of books/articles infact HeadFirst suggests this in one of its Bean Interviews...

I was trying to digest this for long now but could really get to it. I can understand FactoryMethod --> Inheritence but how about AbstractFactory --> Composition.

Any pointers..??


Rajesh.<br />------------------------<br />SCJP 1.4
Santiago Urrizola
Ranch Hand

Joined: Apr 27, 2006
Posts: 172
Hi, i post an image that helps me a lot to undertand the diference betwen this two patterns.



My notes to undertand when i was study for the first part(perhaps not the best):
Method Factory: only one create method, and create only object of one family
Abstract Factory: * creational methods, each of one create object of an especific family.
So an ejbhome contains * create method, that is the form that i undertand wy the ejbobjetc is an AbstractFactory.
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2922
    
    5
Originally posted by Rajesh Velicheti:
Can some one please clarify this notion of saying FactoryMethod --> Inheritence, AbstractFactory --> Composition.


Head First sticks to the GoF-purist notion of a Factory Method. So the "inheritance part" is based on the idea that each class that implements the Factory Method separately and that each may return a distinct subclass (of the base class/interface identified by the Factory Method's return type) from their concrete method. However with Fowler's Factory Method the "inheritance part" is simply based on the fact that the concrete method will return an unknown subclass(/implementing class) from the inheritance hierarchy(/all the classes implementing the interface) of the class(/interface) specified as the factory method's return type.

Now the Abstract Factory -> "composition part" is a popular notion but ultimately misleading . If a class has a Factory Method that returns an object that is a composition (whole-part) but there is no other factory method on that class that may return some other part ("part" rather then the "whole") then you are not dealing with an Abstract Factory � you are still only dealing with a Factory Method. The Abstract Factory's primary concern is the "creation" of different object instances belonging to different classes (belonging to different inheritance hierarchies) of a family of classes that may be created and returned separately. Composition is usually a secondary effect because these parts usually fit together in some way � but that�s not a necessary condition for the Abstract Factory. Furthermore the Factory has to be abstract, i.e. the concrete factory actually has to implement an interface (or extend an abstract class) that specifies the "contract" of the Abstract Factory.
So really it should be Abstract Factory -> "instances from a family of classes/interfaces".

Originally posted by Santiago Urrizola:
So an ejbhome contains * create method, that is the form that i undertand wy the ejbobjetc is an AbstractFactory.


What are the classes in the family of classes of which the EJBHome creates instances? The creational methods are not part of the EJBHome interface; they are part of the bean's home interface! The create method(s) on the home interface only return objects of one polymorphic type - the component interface (collections of instances from classes implementing the component interface returned by the finder methods don't count as a family of classes).
  • The create method(s) of the bean's home interface are the Factory Methods. There are no Factory Methods on the EJBHome interface.
  • The home interface create methods return instances implementing the component interface (and collections thereof), not "objects from a family of classes". Therefore the bean's home interface doesn't qualify as an Abstract Factory.


  • Furthermore neither EJBObject nor a bean's component interface contain any creational methods.
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: Patterns: Abstract Factory Vs Factory Method
     
    Similar Threads
    J2EE Design patterns
    EJB Home uses Abstract factory or Factory Method?
    Design Patterns Explained: what it's about
    What is the need for a pattern like factory Pattern?
    difference between factory pattern , abstract factory pattern and command pattern