• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Abstract Factory vs Factory pattern

 
author
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I'm going through the GoF patterns chapter.
In my work experience I have used sometimes the Factory
pattern but there I have found a specialized version
of this pattern called Abstract Factory.
I have basically understood how it works but
if I were asked at the exam what's the scenario when
it's best suited one instead of the other....well
I would be in troubles.....
can anybody shed some light on it ?
Thanks a lot
Francesco
 
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm surprised you didn't include the Builder pattern in the bunch. Design Patterns: Elements of Reusable Software includes some very concise summaries in the cover that may help:
  • Abstract Factory: Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
  • Builder: Separate the construction of a complex object from its representation so that the same construction process can create different representations.
  • Factory Method: Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory method lets a class defer instantiation to subclasses.

  • I never understood why the order of these patterns isn't reversed from simplest to most complex.

    You may want to have a look over Creational Patterns: Creating Objects in an OO System. It makes the point that "JDBC (Java database connectivity) uses the Factory Method pattern in many of its interfaces. You can use another JDBC driver as long as the correct driver is loaded." I guess JDBC cannot completely be implemented as an Abstract Factory because implmentation details of the RDBMS must still be known to the "client" to use the database effectively. Note also that the "Abstract Factory is often implemented with factory methods." (GOF p.116).

    The Builder is usually used to build Composites, e.g. you give a configuration string to the builder and it produces an object that contains the necessary parts - or you use the builder to add one part at a time (it knows how to "attach" them) and then extract the finished product. The abstract builder is more interesting because it can return any sub-type of the class or interface - whatever is necessary to get the job done.

    The concrete factories in the abstract factory pattern are usually responsible for returning instances of all the different sub-types of the classes/interfaces that the client uses; a Builder is typically only responsible returning one object. When using the abstract factory pattern you usually implement at least two concrete factories, one concrete factory for each family of subtypes that the client could be using. The client would typically only be using the subtypes from one single family (concrete factory) at a time - usually the subtypes from the different families (concrete factories) don't mix. See Hibernate - Generic Data Access Objects for an example of the use of abstract and concrete factories. HibernateDAOFactory is shown as the only concrete implementation of the abstract DAOFactory; however the idea is that you can code other DAOFactory implementations like the JDBC family (JDBCDAOFactory,ItemDAOJDBC,CategoryDAOJDBC) or JDO family (JDODAOFactory,ItemDAOJDO,CategoryDAOJDO). Meanwhile the client would only use (DAOFactory,ItemDAO,CategoryDAO) - while concrete factory loaded at runtime could be configured in a properties file.
    Or you use different concrete factories at the same time that produce DAOs that can be used tranparently to unify the processing over different sources of data (different schemas, different databases, etc.).

    Note also (Cade p.60): The family of related products is designed to be used together***, and you must enforce this constraint. This is the key point to the pattern, otherwise you could use a Factory Method.

    ***Family 1: (ConcreteFactory1, ProductA1, ProductB1) vs. Family 2: (ConcreteFactory2, ProductA2, ProductB2)

    See also Java GoF Creational Design Patterns
     
    Sheriff
    Posts: 5782
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Well it is kind of the difference of another level of indirection, sort of.

    The Factory Method is a Factory that has a "newInstance()" method, and the Abstract Factory is a Factory that isn't implemented, but the subclasses implement it, so your calling client doesn't really know what type of Factory it is getting. Basically you are providing a Factory interface, but not the implementation, that is reserved for the subclasses, which in terms of GOF is a "Family of related or dependent objects without specifying their concrete classes.
     
    Ajith Kallambella
    Sheriff
    Posts: 5782
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    If you want to read more about this famous question( and a ton of answers ), search the Patterns forum.

    Have fun!
     
    Francesco Marchioni
    author
    Posts: 194
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks for your kind reply.
    Have a good day
    Francesco
     
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Ajith,

    Please give some tips on Design Pattern topic for SCEA Part I exam.

    Thanks in advance,
     
    Ranch Hand
    Posts: 30
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Factory Pattern is used when we don't know which class object to create. It returns the class object.

    Abstract Factory Pattern returns the class object which itself has subclasses and returns that subclasses object.

    In Short Abstract Factory Pattern returns the factory object and this factory object returns the subclass object.

    If i am wrong, please correct me.
     
    Greenhorn
    Posts: 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Okkadu, you are exactly right!!!
     
    Ranch Hand
    Posts: 256
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Okkadu,
    you gave right clarification.

    Cheers!
    Prathap.
     
    Opportunity is missed by most people because it is dressed in overalls and looks like work - Edison. Tiny ad:
    Smokeless wood heat with a rocket mass heater
    https://woodheat.net
    reply
      Bookmark Topic Watch Topic
    • New Topic