none
Difference between Abstract factory and Factory

    Question

  • Hi,

    If anyone provide difference between abstract factory and Factory pattern with example it could be helpful

    Thanks,

    Wednesday, June 02, 2010 4:44 PM

Answers

  • The Abstract Factory pattern is very similar to the Factory Method pattern. One difference between the two is that with the Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition whereas the Factory Method pattern uses inheritance and relies on a subclass to handle the desired object instantiation.


    Regards
    Wednesday, June 02, 2010 4:53 PM
  • Here is an interesting look at a

    Abstract Factory Design Pattern (By Judith Bishop)  

    Also see here


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Wednesday, June 02, 2010 5:23 PM
  • 1. Factory Pattern is used when we don't know which class object to create. It returns the class object.

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

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


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com

     

    Layman's Terms.

    1.  The Factory finds and returns an instance of the class that we need.  You walk into a restaurant to order a fancy dinner.  The kitchen factory in the back produces an instance of that fancy dinner for you.  The final design is complex, but it is usually pre-determined at compile time.

    2.  The Abstract Factory constructs complex objects based upon a set of parameters that define our desired object.  That is what John's car factory example does.  It takes a set of specifications and constructs a custom car.  The final design is complex, but it is usually not determined until run time.


    Mark the best replies as answers. "Fooling computers since 1971."
    • Marked as answer by Liliane Teng Wednesday, June 09, 2010 6:10 AM
    Wednesday, June 02, 2010 7:48 PM
    Moderator
  • Or one of my favorite examples:

     

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace AbstractFactory
    {
     class Program
     {
      static void Main(string[] args)
      {
       Console.Title = "Abstract Factory Example";
       FirebirdFactory firebird = new FirebirdFactory();
       AssemblyLine assemblyLine = new AssemblyLine(firebird);
       assemblyLine.AssembleCars();
    
       TransAmFactory transAm = new TransAmFactory();
       assemblyLine = new AssemblyLine(transAm);
       assemblyLine.AssembleCars();
       Console.ReadLine();
      }
     }
    }
    


    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace AbstractFactory
    {
    
     #region Interfaces
    
     public interface IEngine
     {
      void Install();
     }
    
     public interface ITransmission
     {
      void Install();
     }
    
     public interface IAutoFactory
     {
      ITransmission GetTransmission();
      IEngine GetEngine();
      void BeginAssembly();
     }
    
     #endregion
    
     #region Transmissions
    
     public class Automatic : ITransmission
     {
      #region ITransmission Members
    
      public void Install()
      {
       Console.WriteLine("- Automatic Transmission Installed Successfully");
      }
    
      #endregion
     }
    
     public class Manual : ITransmission
     {
      #region ITransmission Members
    
      public void Install()
      {
       Console.WriteLine("- Manual Transmission Installed Successfully");
      }
    
      #endregion
     }
    
     #endregion
    
     #region Engines
    
     public class HP300 : IEngine
     {
      #region IEngine Members
    
      public void Install()
      {
       Console.WriteLine("- HP300 Engine Installed Successfully");
      }
    
      #endregion
     }
    
     public class HP340 : IEngine
     {
      #region IEngine Members
    
      public void Install()
      {
       Console.WriteLine("- HP340 Engine Installed Successfully");
      }
    
      #endregion
     }
    
     #endregion
    
     #region AssemblyLine Template
    
     public class AssemblyLine
     {
      private ITransmission transmission;
      private IEngine engine;
      private IAutoFactory factory;
    
      public AssemblyLine(IAutoFactory factory)
      {
       this.factory = factory;
       transmission = factory.GetTransmission();
       engine = factory.GetEngine();
      }
    
      public void AssembleCars()
      {
       factory.BeginAssembly();
       transmission.Install();
       engine.Install();
      }
     }
    
     #endregion
    
     #region Concrete Cars
    
     public class FirebirdFactory : IAutoFactory
     {
      #region IAutoFactory Members
    
      public ITransmission GetTransmission()
      {
       return new Automatic(); 
      }
    
      public IEngine GetEngine()
      {
       return new HP300();
      }
    
      public void BeginAssembly()
      {
       Console.WriteLine("-----------------------");
       Console.WriteLine("BEGIN FIREBIRD ASSEMBLY");
       Console.WriteLine("-----------------------");
      }
    
      #endregion
     }
    
     public class TransAmFactory : IAutoFactory
     {
      #region IAutoFactory Members
    
      public ITransmission GetTransmission()
      {
       return new Manual();
      }
    
      public IEngine GetEngine()
      {
       return new HP340();
      }
    
      public void BeginAssembly()
      {
       Console.WriteLine("----------------------");
       Console.WriteLine("BEGIN TRANSAM ASSEMBLY");
       Console.WriteLine("----------------------");
      }
    
      #endregion
     }
    
     #endregion
    
    }
    

     


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    • Marked as answer by Liliane Teng Wednesday, June 09, 2010 6:09 AM
    Wednesday, June 02, 2010 5:44 PM
  • It could be helpful  if you explain how to match this example with abstract factory.

     

    Thanks,


    Did you read the pdf link I gave you which explains in detail the differences you were asking about?

    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 provide 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.

    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.


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Wednesday, June 02, 2010 7:35 PM
  • Hello senthilkr,
    Thanks for your post.
    Check the following discussions and examples about Abstract Factory Vs Factory Method. Hope they could make you understand more clear.
    http://www.coderanch.com/t/99780/patterns/Factory-Vs-Abstract-Factory-Pattern
    http://c2.com/cgi/wiki?AbstractFactoryVsFactoryMethod
    (Factory Vs Abstract Factory Pattern)

    Best regards,
    Liliane


    Please mark the replies as answers if they help and unmark them if they provide no help. Thanks
    • Marked as answer by Liliane Teng Wednesday, June 09, 2010 6:10 AM
    Monday, June 07, 2010 8:27 AM
  • Hello,

    The reason it is called the Abstract Factory is because the things that it is intended to build are themselves defined by abstractions ('DisplayDriver', ' SoundDriver') not because we have an abstract factory class at the implementation level (see below), and so if we need various clients to have families of related subject, we may consider this pattern.

    Say you need to handle Drivers, and we have Display driver, and Sound driver, and each may have high, and low quality renderers, now, none of this should be the client concern, the abstract factory advise us to define an interface that will represent each driver's family ('DisplayDriver', ' SoundDriver') , and let our abstract factory class be responsible to access our drivers through their appropriate interface.

    The factory class has to be abstract here, as we yet to implement 'LowQualityFactory', and 'HighQualityFactory' which will be used to implement the level of quality for each driver.

    The Factory Method on the other hand is called as such because a method involved in creating an object.

    If we have have an application that may use multiple DataBase systems due to various reasons and we want to perform CRUD actions, we want to have one interface to work with, well, this is a great candidate to solve the issue.

    Say the interface is 'DBQuery ', and have 'SqlQuery', 'MySqlQuery' implements this interface, now the concrete classes overrides a protected method that should return an instance of the class it implements, and this is used latter in the implementation of the CRUD operations.


    Eyal, Regards.

    blog.eyalsh.net
    • Marked as answer by Liliane Teng Wednesday, June 09, 2010 6:13 AM
    Monday, June 07, 2010 12:39 PM
    Moderator

All replies

  • The Abstract Factory pattern is very similar to the Factory Method pattern. One difference between the two is that with the Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition whereas the Factory Method pattern uses inheritance and relies on a subclass to handle the desired object instantiation.


    Regards
    Wednesday, June 02, 2010 4:53 PM
  • Here is an interesting look at a

    Abstract Factory Design Pattern (By Judith Bishop)  

    Also see here


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Wednesday, June 02, 2010 5:23 PM
  • The Abstract Factory pattern is very similar to the Factory Method pattern. One difference between the two is that with the Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition whereas the Factory Method pattern uses inheritance and relies on a subclass to handle the desired object instantiation.


    Regards

    Do you have a code example that you could post for the OP?

    Mark the best replies as answers. "Fooling computers since 1971."
    Wednesday, June 02, 2010 5:34 PM
    Moderator
  • Or one of my favorite examples:

     

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace AbstractFactory
    {
     class Program
     {
      static void Main(string[] args)
      {
       Console.Title = "Abstract Factory Example";
       FirebirdFactory firebird = new FirebirdFactory();
       AssemblyLine assemblyLine = new AssemblyLine(firebird);
       assemblyLine.AssembleCars();
    
       TransAmFactory transAm = new TransAmFactory();
       assemblyLine = new AssemblyLine(transAm);
       assemblyLine.AssembleCars();
       Console.ReadLine();
      }
     }
    }
    


    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace AbstractFactory
    {
    
     #region Interfaces
    
     public interface IEngine
     {
      void Install();
     }
    
     public interface ITransmission
     {
      void Install();
     }
    
     public interface IAutoFactory
     {
      ITransmission GetTransmission();
      IEngine GetEngine();
      void BeginAssembly();
     }
    
     #endregion
    
     #region Transmissions
    
     public class Automatic : ITransmission
     {
      #region ITransmission Members
    
      public void Install()
      {
       Console.WriteLine("- Automatic Transmission Installed Successfully");
      }
    
      #endregion
     }
    
     public class Manual : ITransmission
     {
      #region ITransmission Members
    
      public void Install()
      {
       Console.WriteLine("- Manual Transmission Installed Successfully");
      }
    
      #endregion
     }
    
     #endregion
    
     #region Engines
    
     public class HP300 : IEngine
     {
      #region IEngine Members
    
      public void Install()
      {
       Console.WriteLine("- HP300 Engine Installed Successfully");
      }
    
      #endregion
     }
    
     public class HP340 : IEngine
     {
      #region IEngine Members
    
      public void Install()
      {
       Console.WriteLine("- HP340 Engine Installed Successfully");
      }
    
      #endregion
     }
    
     #endregion
    
     #region AssemblyLine Template
    
     public class AssemblyLine
     {
      private ITransmission transmission;
      private IEngine engine;
      private IAutoFactory factory;
    
      public AssemblyLine(IAutoFactory factory)
      {
       this.factory = factory;
       transmission = factory.GetTransmission();
       engine = factory.GetEngine();
      }
    
      public void AssembleCars()
      {
       factory.BeginAssembly();
       transmission.Install();
       engine.Install();
      }
     }
    
     #endregion
    
     #region Concrete Cars
    
     public class FirebirdFactory : IAutoFactory
     {
      #region IAutoFactory Members
    
      public ITransmission GetTransmission()
      {
       return new Automatic(); 
      }
    
      public IEngine GetEngine()
      {
       return new HP300();
      }
    
      public void BeginAssembly()
      {
       Console.WriteLine("-----------------------");
       Console.WriteLine("BEGIN FIREBIRD ASSEMBLY");
       Console.WriteLine("-----------------------");
      }
    
      #endregion
     }
    
     public class TransAmFactory : IAutoFactory
     {
      #region IAutoFactory Members
    
      public ITransmission GetTransmission()
      {
       return new Manual();
      }
    
      public IEngine GetEngine()
      {
       return new HP340();
      }
    
      public void BeginAssembly()
      {
       Console.WriteLine("----------------------");
       Console.WriteLine("BEGIN TRANSAM ASSEMBLY");
       Console.WriteLine("----------------------");
      }
    
      #endregion
     }
    
     #endregion
    
    }
    

     


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    • Marked as answer by Liliane Teng Wednesday, June 09, 2010 6:09 AM
    Wednesday, June 02, 2010 5:44 PM
  • Thanks for the reply. could you pls explain this example to match Abstract factory?
    Wednesday, June 02, 2010 6:24 PM
  • It could be much easier if it is a simple example...

    Thanks,

    Wednesday, June 02, 2010 6:25 PM
  • That is a basic example.  Use the Debugger to step through the code line by line, start to finish.

    Mark the best replies as answers. "Fooling computers since 1971."
    Wednesday, June 02, 2010 6:28 PM
    Moderator
  • I chose this because it was "simple".

    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Wednesday, June 02, 2010 6:40 PM
  • It could be helpful  if you explain how to match this example with abstract factory.

     

    Thanks,

    Wednesday, June 02, 2010 7:19 PM
  • It could be helpful  if you explain how to match this example with abstract factory.

     

    Thanks,


    Did you read the pdf link I gave you which explains in detail the differences you were asking about?

    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 provide 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.

    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.


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Wednesday, June 02, 2010 7:35 PM
  • 1. Factory Pattern is used when we don't know which class object to create. It returns the class object.

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

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


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com

     

    Layman's Terms.

    1.  The Factory finds and returns an instance of the class that we need.  You walk into a restaurant to order a fancy dinner.  The kitchen factory in the back produces an instance of that fancy dinner for you.  The final design is complex, but it is usually pre-determined at compile time.

    2.  The Abstract Factory constructs complex objects based upon a set of parameters that define our desired object.  That is what John's car factory example does.  It takes a set of specifications and constructs a custom car.  The final design is complex, but it is usually not determined until run time.


    Mark the best replies as answers. "Fooling computers since 1971."
    • Marked as answer by Liliane Teng Wednesday, June 09, 2010 6:10 AM
    Wednesday, June 02, 2010 7:48 PM
    Moderator
  • Hello senthilkr,
    Thanks for your post.
    Check the following discussions and examples about Abstract Factory Vs Factory Method. Hope they could make you understand more clear.
    http://www.coderanch.com/t/99780/patterns/Factory-Vs-Abstract-Factory-Pattern
    http://c2.com/cgi/wiki?AbstractFactoryVsFactoryMethod
    (Factory Vs Abstract Factory Pattern)

    Best regards,
    Liliane


    Please mark the replies as answers if they help and unmark them if they provide no help. Thanks
    • Marked as answer by Liliane Teng Wednesday, June 09, 2010 6:10 AM
    Monday, June 07, 2010 8:27 AM
  • Hello,

    The reason it is called the Abstract Factory is because the things that it is intended to build are themselves defined by abstractions ('DisplayDriver', ' SoundDriver') not because we have an abstract factory class at the implementation level (see below), and so if we need various clients to have families of related subject, we may consider this pattern.

    Say you need to handle Drivers, and we have Display driver, and Sound driver, and each may have high, and low quality renderers, now, none of this should be the client concern, the abstract factory advise us to define an interface that will represent each driver's family ('DisplayDriver', ' SoundDriver') , and let our abstract factory class be responsible to access our drivers through their appropriate interface.

    The factory class has to be abstract here, as we yet to implement 'LowQualityFactory', and 'HighQualityFactory' which will be used to implement the level of quality for each driver.

    The Factory Method on the other hand is called as such because a method involved in creating an object.

    If we have have an application that may use multiple DataBase systems due to various reasons and we want to perform CRUD actions, we want to have one interface to work with, well, this is a great candidate to solve the issue.

    Say the interface is 'DBQuery ', and have 'SqlQuery', 'MySqlQuery' implements this interface, now the concrete classes overrides a protected method that should return an instance of the class it implements, and this is used latter in the implementation of the CRUD operations.


    Eyal, Regards.

    blog.eyalsh.net
    • Marked as answer by Liliane Teng Wednesday, June 09, 2010 6:13 AM
    Monday, June 07, 2010 12:39 PM
    Moderator
  • Hi,

    i tried to answer differences betwen 3 factories pattern here: factory family

    Tuesday, November 12, 2013 11:20 AM