none
Factory method VS Abstract Factory

    Question

  • As per GoF

    Abstract Factory: provide an interface for creating families of releated or dependent objects without specifiying their concreate classes.

     

    Factory method: Define an interface for creating an object, but let sublcasses decide which class to instantiate.

     

    What I understood:

    1. Factory in general is to "take out the responsibility of object creation and initalization out from client code to a common place".

     

    2. As with any pattern, there can be variations based on actual need. There can be various ways in which factory based solution can be implemented.

     

    3. Static factory creator method should not be used, because being static it can't be changed/modified/extended. But if the static method picks info from some other source(like config file), it is OK.

     

    4. Abstract Factory is also factory pattern, the only difference being that in Abstract Factory clients code against a Interface/Abstract class instead of a concreate instance of factory.

     

    5. Not necessary that there should be requirement for "creating families of releated product", for decidint to use Abstract factory. Even in case if there is a single Type of object to be created, Abstract Factory should be used.

     

    so below code is also an Abstract Factory Pattern???

    client(IFactoryInstance instance)  //constructor

    {

    _factory = instance;

    _factory.CreateInstance();  //only 1 type of object returned by factory

    }

     

    6. if the only difference is using concreate factory VS Abstract instanc of factory, why these are 2 separate pattern?

     

    Regards,

     

     

    Friday, May 16, 2008 7:21 AM

Answers

  •  

    Hi singhhome,

    both are partially the same but -I'm answering this with the GoF book in my hands- while the idea is an only one

     

    • An Abstract Factory creates any kind of objects (in the GoF example, a same factory -or actually its concrete descendants- creates a window or a scroll bar as well)
    • A class having an abstract Factory Method, instead, has particularly any other kinds of responsibilities (other than creating objects) implemented on itself, while the concrete creation of the factorized class is relegated to descendants of this class (which, presumably, only implements that only method

    In terms of cohesiveness, an abstract factory is a general creator (thru its implementing classes) while a factory-method class has its cohesiveness in the sense that performs general operations with a unique, given class, except create it

     

     

     

    PS. Well, actually I had my hands on the keyboard and the GoF book beside on my desk but, you got it

    Tuesday, June 17, 2008 10:57 PM

All replies

  • In my mind, the difference is, Factory method = Dependency Injection (Method) and Abstract Factory is without DI...

     

    Dont know if others share the same opinion.

     

    Friday, May 16, 2008 9:28 PM
  •  

    Hi singhhome,

    both are partially the same but -I'm answering this with the GoF book in my hands- while the idea is an only one

     

    • An Abstract Factory creates any kind of objects (in the GoF example, a same factory -or actually its concrete descendants- creates a window or a scroll bar as well)
    • A class having an abstract Factory Method, instead, has particularly any other kinds of responsibilities (other than creating objects) implemented on itself, while the concrete creation of the factorized class is relegated to descendants of this class (which, presumably, only implements that only method

    In terms of cohesiveness, an abstract factory is a general creator (thru its implementing classes) while a factory-method class has its cohesiveness in the sense that performs general operations with a unique, given class, except create it

     

     

     

    PS. Well, actually I had my hands on the keyboard and the GoF book beside on my desk but, you got it

    Tuesday, June 17, 2008 10:57 PM
  • As I understand it a factory method allows you to delegate the creation of an object to something else.  In other words it you can decouple your client code from the creation of an object.  This is an example of dependancy inversion.

     

    An abstract factory on the other hand allows you to group the creation of groups of objects. 

     

    For example, a framework could use a factory to create user interface elements like buttons, windows, icons, scrollbars etc.

     

    The framework could provide a WindowsWidgetFactory that creates interface elements for Microsoft Windows and a different factory called LinuxWidgetFactory that creates ones for Linux operating systems.

    Thursday, June 19, 2008 9:16 AM