What is the difference between Abstract Factory Pattern and factory Pattern?

What is the difference between Abstract Factory Pattern and factory Pattern? When would you use each of them?

Showing Answers 1 - 6 of 6 Answers

LakshmiKanth

  • Feb 14th, 2006
 

Abstract Factory:- provide an interface to create a family of related or  dependant classes without specifying their concrete classes

Factory Method :-  another creational pattern, Defines an interface for creating An Object, but let sub class decide which class to instantiate

  Was this answer useful?  Yes

gcharan

  • Mar 13th, 2006
 

Abstract Factory : As the name suggest, its not a concrete factory, infact it acts as the factory for factories. For e.g. suppose we have some factories for creating AWT components and each factory works on a particular OS. In this case we can write an Abstract Factory which has some abstract methods to be implemented by all subclass concerete factories and this abstract factory also has a static method to get the appropriate factory based on some arguments.

Here is a small example for the same.

public abstract class AbstractFactory implements FactoryInterface {

      private static FactoryInterface factory;

      public static FactoryInterface getFactory(int index) {

            if (index == 1) {

                 factory = new Factory1();

            } else {

                 factory = new Factory2();

            }

      }

}

public class Factory1 extends AbstractFactory {

//implements methods in FactoryInterface

}

public class Factory2 extends AbstractFactory {

//implements methods in FactoryInterface

}

Hope it will help to understand the concept.

Regards,

Ashish.

rkj2ee2005

  • Mar 19th, 2006
 

Abstract factory pattern is typically used for giving implementation to specification (eg., jdbc, servlet specifications etc...). Factory pattern is used when you want to defer object creation to later stages (when concrete implementation of that class/interface is not known or there could be multiple ways to implement and want to give flexibility over it), but keep continuing developing framework/rest of the application.RegardsRama

  Was this answer useful?  Yes

Abstract factory pattern is same as like factory pattern but in abstract factory pattern we  prepare a base class as abstract and we provide the implementation classes to the base class. In the client side program we create reference to the base abstract class and we write one method in such a way that it returns the required object according to the the data provided by the client. Here advantage is same as factory pattern we are providing some data for required object but in Abstract factory pattern we are not saying any thing about the implementation classes so that the client side may not know which method and which implemented class the server is calling. In this way we are providing the abstraction to the code. So compare with factory pattern with abstract factory pattern provides more security.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions