design patterns - factory pattern

8

Click here to load reader

Upload: annamalai-c

Post on 20-Jun-2015

1.074 views

Category:

Technology


0 download

DESCRIPTION

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.

TRANSCRIPT

Page 1: Design patterns - Factory Pattern

FACTORY PATTERN explained !

Prepared by Annamalai C

Copyright 2006-2011

QmPeinitiative

Page 2: Design patterns - Factory Pattern

Copyright 2006-2011

Factory Pattern in Real LifeKitchen 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.

Page 3: Design patterns - Factory Pattern

Copyright 2006-2011

Factory Pattern in Java

Page 4: Design patterns - Factory Pattern

Copyright 2006-2011

Real Life vs Java Object

Factory

Kitchen

Page 5: Design patterns - Factory Pattern

Copyright 2006-2011

Real Life vs Java Object

Page 6: Design patterns - Factory Pattern

Copyright 2006-2011

How Factory Pattern works in Real Life ?

1 Orders a Dish from Menu

2Receives the Name of the DishCreates the Dish

3 Delivers the Dish

Page 7: Design patterns - Factory Pattern

How Factory Pattern works in Java ?

Copyright 2006-2011

KitchenFactory factory = new KitchenFactory();Food dosa = factory.getFood("Dosa");dosa.print();

Food noodles = factory.getFood("Noodles");noodles.print();

public Food getFood(String name) { if (name.equals("Dosa")) { return new Dosa(); } else if (name.equals("Noodles")) { return new Noodles(); } return null;}

1

3

Food

Dosa Noodles

2

Page 8: Design patterns - Factory Pattern

Copyright 2006-2011

QmPeinitiative

THANK YOU

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

Write to us on [email protected]