Swaraj Patil
What is difference between Factory and Abstract factory design pattern?
By Swaraj Patil in OOP/OOD on Jun 12 2014
  • Abodh Nok
    Jul, 2015 5

    Try to understand the two design patter with some simple examples factory design pattern : You have some row materiel (Input) , you provided it to one factory (factory class) and the factory gives you a product (object) so based on your raw material and facorty implementation you get a product this is a simple illustraction of facotory design pattern .Abstract factory design : Its like you have raw material , you provided it to one factory as input but instead of processing your input this factory delagates the task to create product to its sub factories . Simply abstract factory means multiple factories.

    • 3
  • Manju lata Yadav
    Jul, 2014 9

    Factory design Pattern:- In this pattern we define an interface which will expose a method which will create objects for us. Return type of that method is never be a concrete type rather it will be some interface (or may be an abstract class) Abstract Factory Design Pattern:- In Abstract Factory we define an interface which will create families of related or dependent objects. In simple words, interface will expose multiple methods each of which will create some object. Again, here method return types will be generic interfaces. All this objects will together become the part of some important functionality. • First of all both of them falls under Creational category and it means it will solves the problem pertaining to object creation. • Factory Method and abstract factory pattern all are about creating objects.

    • 3
  • Shiv Ratan Kumar
    Jun, 2019 19

    FactoryImagine you are constructing a house and you approach a carpenter for a door. You give the measurement for the door and your requirements, and he will construct a door for you. In this case, the carpenter is a factory of doors. Your specifications are inputs for the factory, and the door is the output or product from the factory.Abstract FactoryNow, consider the same example of the door. You can go to a carpenter, or you can go to a plastic door shop or a PVC shop. All of them are door factories. Based on the situation, you decide what kind of factory you need to approach. This is like an Abstract Factory.

    • 1
  • Adwait Churi
    Sep, 2015 9

    To understand Abstract Factory and Factory Method design patterns, we need to understand Factory in real terms.What is Factory? In general terms, Factory is a manufacturing unit which does processing to deliver specific output. Example – Fiat Automobiles which develops most efficient engines in the world.What is the relation between real term Factory and Factory best practices in Software Development? Factory in Real Terms – Factory is being setup to create the specific output based on specific input. As well factory has specific algorithm using which it develops the specific output. The same factory cannot use to achieve different purposes. Conclusion of Factory in Real Terms - As per the statement, it is clear that factory is stringent and has specific demand as well specific behaviour. Factory Best Practice in Software Development –Factory in software design pattern is stringent and used only to create instances of the specific class. That is why Factory pattern is listed under Creation Pattern. What problem factory pattern solves? - Factory pattern helps to solve the dependency problem. Example – You have a requirement to develop Employee module for an IT organisation. The Employee module consists of employee details and address details. The simplest and classic structural implementation would be Public class Employee {Public Emplyee() { }Public string EmployeeId(){Return “ID1”;}Public string AddressLine1(){Return “AddressLine1”;}Public string AddressLine2(){Return “AddressLine2”;}Public string State(){Return “State”;} }Conclusion on this implementation – Employee class consists of address details. If business requirement is restricted to employee id details with employee residency details, then this implementation is sufficient and will be faster. Problem introduced using above example – Is employee details limited to employee id only? Employee details may also consists of Plant Address where Employee is working. The Address details which are common for employee residency and employee plant location generates code redundancy. As well introduces code dependency that is, any changes later identified for Address, requires modification in Employee Class and retesting. Solution to the problem – Separation of Address class and object referring in Employee Class using Factory.The simplest object oriented implementation using Factory would bePublic class Address {Public Address () { }Public string AddressLine1(){Return “AddressLine1”;}Public string AddressLine2(){Return “AddressLine2”;}Public string State(){Return “State”;} } Public class Employee {Public Emplyee() { }Public string EmployeeId(){Return “ID1”;}Public Address GetPlantAddress(){Address clsAddress = new Address();Return clsAddress();}Public Address GetResidencyAddress(){Address clsAddress = new Address();Return clsAddress();} }How to implement Factory method? – Many a times the implementation is done where the caller knows the child class to call. This type of implementation comes under Factory Method.Example – As per the Factory example GetPlantAddress() and GetResidencyAddress() methods knows they are calling Address Class internally to get the respective details. There is no separate implementation for PlantAddress and ResidencyAddress. Such implementation are the candidates for Factory Method.What is Abstract Factory? - Abstract Factory is an abstraction over factory method. What is the difference between Factory Method and Abstract Factory? - In Factory Method, Caller knows the child classes to call where as in Abstract Factory caller does not know child classes to call. Abstraction plays important roll to hide calling implementation from caller and actual object assigning is done at run time. Abstract Factory performs Inversion of control. That is Caller does not depends on child classes. Child classes depends on abstraction and abstraction does not depends on child classes.The simplest object oriented implementation using Abstract Factory would bePublic class Address {Public Address (string addressType){If (addressType == “Residency”){Return new ResidencyAddress();}else (addressType == “Plant”){Return new PlantAddress (); }}Public virtual string AddressLine1(){Return “AddressLine1”;}Public virtual string AddressLine2(){Return “AddressLine2”;}Public virtual string State(){Return “State”;} }Public class PlantAddress : Address {Public PlantAddress () { }Public override string AddressLine1(){Return “AddressLine1”;}Public override string AddressLine2(){Return “AddressLine2”;}Public override string State(){Return “State”;} }Public class ResidencyAddress : Address {Public PlantAddress () { }Public override string AddressLine1(){Return “AddressLine1”;}Public override string AddressLine2(){Return “AddressLine2”;}Public override string State(){Return “State”;} }Public class Employee {Public Emplyee() { }Public string EmployeeId(){Return “ID1”;}Public Address GetAddress(string addressType){Address clsAddress = new Address(addressType);Return clsAddress();} }

    • 1
  • Adwait Churi
    Sep, 2015 9

    In Factory Method, Caller knows the child classes to call where as in Abstract Factory caller does not know child classes to call. Abstraction plays important roll to hide calling implementation from caller and actual object assigning is done at run time.Abstract Factory performs Inversion of control. That is Caller does not depends on child classes. Child classes depends on abstraction and abstraction does not depends on child classes.

    • 1
  • Munesh Sharma
    Oct, 2014 1

    With the Factory pattern, you produce implementations (Apple, Banana, Cherry, etc.) of a particular interface -- say, IFruit.With the Abstract Factory pattern, you produce implementations of a particular Factory interface -- e.g., IFruitFactory. Each of those knows how to create different kinds of fruit.

    • 1
  • Simi Tkd
    Jul, 2014 1

    factory design pattern give interface for creating object let the subclasses decide which one to be instantiate.....(Virtual Constructor)however abstract factory design pattern is a type of super factory that create of other factories... both of them comes under creational design pattern

    • 1


Most Popular Job Functions


MOST LIKED QUESTIONS