Tutorial List
Home
Interview Questions
Interview
Interview Questions
Links
Web Home
About Us

Factory Method Pattern - Choose Your Object

Factory method pattern is another powerful design pattern, which not only reduces the complexity of your application but make your design more flexible also.
The definition of factory stats: "Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses."

Well this sounds little confusing, I understand that so we will explain this little better in our codes. For time being, just remember following,
  • We have a base abstract class
  • A Factory class can create and returns any child of that abstract class
Lets Walk through an example:
You like Pizza, well most of us do...we go to Dominos (or Pizza Hut, we are not advertising them) and order pizza based on catalog. We never bother about how they are creating different type of Pizzas. We order them and based on our order they made Pizza and return to us...Well, Thats all about Factory pattern. Isn't it simple. Dominos is our Pizza factory and they are returns us a subclass(like margarita, Farm Fresh, MushroomDelight...Yummy) of type Pizza (abstract class).
***********************************************************************
--------------------Creating Base Class Type------------------------------------
abstract class Pizza {
int getPrice();
}
--------------------Creating Actual Class/Product------------------
/* Margarita Pizza recipes*/
class MargaritaPizza extends Pizza {
public int getPrice() {
return 850;
}
}

/* Farm Fresh Pizza recipes*/
class FarmFreshPizza extends Pizza {
public int getPrice() {
return 1050;
}
}


/* Mushroom Delight Pizza recipes*/
class MushroomDelightPizza extends Pizza {
public int getPrice() {
return 1150;
}
}
------------------Creating Factory------------------------------
/*This is actual Pizza Factory*/
class PizzaFactory {
/*Create Pizza Type*/
public enum PizzaType {
Margarita,
FarmFresh,
MushroomDelight
}
/*create Pizza on orders*/
public static Pizza createPizza(PizzaType pizzaType) {
switch (pizzaType) {
case Margarita:
return new MargaritaPizza();
case FarmFresh:
return new FarmFreshPizza();
case MushroomDelight:
return new MushroomDelightPizza();
}
}
}
***********************************************************************
I guess codes are pretty simple and you must have gotten the real idea behind factory pattern. First you need to create a abstract class. then you need to extend that in your child class. Now all you need to do is create a Factory class which returns any of your child instance based on some type. You can use this Factory class to any where in your application to get Pizza according to your order type.
Here you can replace Pizza with any thing and your factory pattern for the scenario is implemented instantly.

When to use Factory Pattern,

  • A class cannot anticipate which kind of class of objects it must create.
  • A class uses its subclasses to specify which objects it creates.
  • You want to localize the knowledge of which class gets created.
There are several similar variations on the factory pattern to recognize.
  • The base class is abstract and the pattern must return a complete working class.
  • The base class contains default methods and is only subclassed for cases where the default methods are insufficient.
  • Parameters are passed to the factory telling it which of several class types to return. In this case the classes may share the same method names but may do something quite different
Thats all for Factory Pattern. Hope you enjoyed the Factory Pattern and built the concrete concept over it. We will continue with next Design Patten in coming posts.
In case of any query regarding this, Please feel free to email me at mohit.amour@gmail.com


Thanks.
The Interpreter

2 comments:

DHEERENDRA said...

This block is very helpful for understanding Factory pattern ... in an easy language and with example code it is made clear what actually it is.

Anonymous said...

Very Good artical on Factory pattern..Thanks for providing great resource....