design patterns - factory pattern

Post on 20-Jun-2015

1.074 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

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

FACTORY PATTERN explained !

Prepared by Annamalai C

Copyright 2006-2011

QmPeinitiative

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.

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

2Receives the Name of the DishCreates the Dish

3 Delivers the Dish

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

Copyright 2006-2011

QmPeinitiative

THANK YOU

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

Write to us on info@ancitconsulting.com

top related