Design 102 : Why do we need Factory Method Pattern?

Public Class SimplePizzaFactory {
Public Pizza createPizza(String type, String place) {
Pizza pizza = null;

if(place == "india") {
if(type == "cheese") {
pizza = new IndianCheesePizza();
} else if(type == "veg") {
pizza = new IndianVegPizza();
} else if(type == "chocochip") {
pizza = new IndianChocoChipPizza();
} else {
pizza = new BasicPizza();
}
} else if(place == "china") {
//..
}
return pizza;
}
}
public abstract Class PizzaStore {    Public Pizza OrderPizza(String type) {
Pizza pizza = createPizza(type);

pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;
}
// This is the abstract factory method
Protected abstract Pizza createPizza(String type);
}
public Class IndianPizzaStore extends PizzaStore {
@override
Protected Pizza createPizza(String type) {
Pizza pizza = null;
if(type == "cheese") {
pizza = new IndianCheesePizza();
} else if(type == "veg") {
pizza = new IndianVegPizza();
} else if(type == "chocochip") {
pizza = new IndianChocoChipPizza();
} else {
pizza = new BasicPizza();
}
return pizza;
}
}
// A lot of small subclasses like this 
public Class IndianCheesePizzaStore extends PizzaStore {
@override
Protected Pizza createPizza() {
return new IndianCheesePizza();
}
}
public Class IndianVegPizzaStore extends PizzaStore {
@override
Protected Pizza createPizza() {
return new IndianVegPizza();
}
}

--

--

--

Cooking Dreams | Engineering at Disney+ Hotstar Github.Handle:https://github.com/LuGO0

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to Convert Case Styles in Python

Introducing Roslyn analyzers for Sitecore

Final Proejct: AR Race Game

Flask + Amazon DynamoDB = CRUD Application.

Product Manager vs Product Owner

The single most useful Python Decorator @cache

CI/CD Pipeline With Gitlab CI and Docker (Part 2)

Best practices for developing on Kubernetes

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Saurabh Kumar

Saurabh Kumar

Cooking Dreams | Engineering at Disney+ Hotstar Github.Handle:https://github.com/LuGO0

More from Medium

Design 101 : Why do we need Abstract Factory Pattern now?

Is Java dying ? .. maybe not!!

Introduction to Sealed Classes

Which implementation of Singleton Pattern do I need?