Factory

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 7

Factory Pattern

Factory Pattern

 Sometimes it is important to keep the code


close for modification but open for extension.
 For this purpose factory pattern is needed.
Characteristic:
Keeping the code close for modification but
open for extension.
Factory Pattern (continues…)

 Implementation:
 An abstract class is used for this purpose.
 We use abstract classes to define and
maintain relationship between objects.
 When we use a abstract class we can not
make any object for this and we can not
modify the abstract class rather we can extend
it by its subclasses.
Factory Pattern(continues…)

 Implementation:
 For example we make abstract class pizza.
 Pizza can be of different types – cheese pizza,
geek pizza, Indian pizza etc.
 The types of pizza can extend the super class
pizza but can not modify pizza.
Factory Pattern(continues…)
Pizza

Prepare()
Bake()
Cut()
Box()

Cheese Greek

Mix() Mix()
AddFlavour() AddFlavour()
Factory Pattern(continues…)
Code
Public Class pizzaFactory Public Class pizzaStore
{ {
Pizza pizza; pizzaFactory factory;
Public create_Pizza(String type) pizzaStore(pizzaFactory factory)
{this.factory=factory;}
{ Public Place_order(String type)
if(type.equal(“cheese”)) {
{pizza=new cheese(); } pizza=factory.create_Pizza(type);
else if(type.equal(“greek”)) pizza.prepare()
{pizza=new greek();} pizza.bake();
else pizza.cut();
{pizza=new indian();} pizza.box();
} }
return pizza; }
}
THANK YOU

You might also like