Professional Documents
Culture Documents
Factory
Factory
Factory
Factory Pattern
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