This presentation is part of the Design Pattern Series. Presentation explains the use of Factory Pattern comparing it against a real life situation and then showcasing the Java implementation of the same.
1. QmPe
initiative
FACTORY PATTERN explained !
Prepared by Annamalai C
Copyright 2006-2011
2. Factory Pattern in Real Life
Kitchen in a Restaurant is similar to a Factory.
It is the Production House of the Restaurant.
-----------------------------------
Customer places orders from a Menu, doesn’t really know how it gets prepared.
In return to the order, gets a fully cooked dish for eating.
Factory Pattern is a Creational Pattern.
It helps in Object Creation without exposing the details of Creation.
Copyright 2006-2011
6. How Factory Pattern works in Real Life ?
1 Orders a Dish from Menu
Receives the Name of the Dish
2 Creates the Dish
3 Delivers the Dish
Copyright 2006-2011
7. How Factory Pattern works in Java ?
public Food getFood(String name) {
if (name.equals("Dosa")) {
return new Dosa();
1 } else if (name.equals("Noodles")) {
return new Noodles();
}
return null;
}
KitchenFactory factory = new KitchenFactory();
Food dosa = factory.getFood("Dosa");
dosa.print();
2
Food noodles = factory.getFood("Noodles");
noodles.print();
Food
3
Dosa Noodles
Copyright 2006-2011
8. THANK YOU
Please visit
www.eclipseinclips.com | www.ancitconsulting.com
Write to us on info@ancitconsulting.com
QmPe
initiative
Copyright 2006-2011