Translate

Thursday 18 February 2016

Factory pattern Vs Abstract Factory pattern

Factory

Definition:

Define an interface for creating an object, but let the classes that implement the interface decide which class to instantiate.

Real Life Example,

Imagine 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 Factory

Definition:

Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

Real Life Example,


Now, 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.