0% found this document useful (0 votes)
17 views

Factory

The Factory Pattern allows code to remain closed for modification but open for extension. An abstract class is used to define relationships between objects. Concrete subclasses extend the abstract class and can implement behaviors without modifying the abstract class. For example, an abstract Pizza class could be extended by CheesePizza and GreekPizza subclasses that implement specific behaviors without changing the Pizza class. A PizzaFactory creates different pizza objects based on a string type, keeping creation logic separate from the object types themselves.

Uploaded by

Shohanur Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Factory

The Factory Pattern allows code to remain closed for modification but open for extension. An abstract class is used to define relationships between objects. Concrete subclasses extend the abstract class and can implement behaviors without modifying the abstract class. For example, an abstract Pizza class could be extended by CheesePizza and GreekPizza subclasses that implement specific behaviors without changing the Pizza class. A PizzaFactory creates different pizza objects based on a string type, keeping creation logic separate from the object types themselves.

Uploaded by

Shohanur Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 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