QmPe
                                              initiative




FACTORY PATTERN explained !




                               Prepared by Annamalai C


         Copyright 2006-2011
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
Factory Pattern in Java




                          Copyright 2006-2011
Real Life vs Java Object




                                                 Factory




                                                 Kitchen




                           Copyright 2006-2011
Real Life vs Java Object




                           Copyright 2006-2011
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
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
THANK YOU


                   Please visit
www.eclipseinclips.com | www.ancitconsulting.com

     Write to us on info@ancitconsulting.com




                  QmPe
                  initiative


                   Copyright 2006-2011

Design patterns - Factory Pattern

  • 1.
    QmPe initiative FACTORY PATTERN explained ! Prepared by Annamalai C Copyright 2006-2011
  • 2.
    Factory Pattern inReal 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
  • 3.
    Factory Pattern inJava Copyright 2006-2011
  • 4.
    Real Life vsJava Object Factory Kitchen Copyright 2006-2011
  • 5.
    Real Life vsJava Object Copyright 2006-2011
  • 6.
    How Factory Patternworks 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 Patternworks 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