design pattern intro+ factory method pattern
DESCRIPTION
An example of factory method pattern.TRANSCRIPT
Design Patterns
What are they?
• Repeatable solution to commonly occurring problems
• Language-independent strategies for solving common object oriented design problems
• They provide a lingo
Is-A Relationship(Inheritance)
Animal
Has-A Relationship(Composition)
Animal a = new Animal()
Dog d = new Dog()
Animal aDog = new Dog()
Pizza Store
Prepare
Bake
Cut
Box
Create
Bake
Cut
Box
Menu
Cheese Pizza
Veggie Pizza
Spicy Chicken Pizza
Barbeque Chicken Pizza
Cheese Pizza
Veggie Pizza
Spicy Chicken Pizza
Barbeque Chicken Pizza
Pizza
Is-A Relationship
MumbaiThin crustFresh VeggiesLess sauce
PuneThick crustFrozen VeggiesLots of sauce and cheese
What kinda pizza’s do people in Mumbai and Pune like?
Store Locations
Mumbai Pune
How would the customer’s order for a pizza be?
Choose store location
Choose pizza
Mumbai
Cheese Pizza
public static void main(String[] a){ PizzaStore p = new PizzaStore(); p.orderPizza(“Pune”,”Cheese”);}
class Main {
}
class PizzaStore{
Pizza createPizza(store, pizzaType){ if(store==“Mumbai”){ if(type==“Cheese”) return new MumbaiStyleCheesePizza(); if(type==“SpicyChickenPizza”) return new MumbaiStyleSpicyChickenPizza(); …. }else if(store==“Pune”){ if(type==“Cheese”) ….}
void orderPizza(store, pizzaType){ pizza = createPizza(store,pizzaType); pizza.bake(); pizza.cut(); pizza.box();}
}
Methods in the Pizza class or its sub-classes
if(store==“Mumbai”){ if(type==“Cheese”) return new MumbaiStyleCheesePizza(); if(type==“SpicyChickenPizza”) return new MumbaiStyleSpicyChickenPizza(); ….}else if(store==“Pune”){ if(type==“Cheese”) ….}
Is this good code?
What if we open a new store in Gurgaon?What if we add a new type of Pizza?What if we stop making a pizza?
if (store==“Gurgaon”){…}
If(type==“Margherita” new margheritaPizza()
If(type==“Veggie”) new veggiePizza()
Design Principle
Classes should be open for extension but closed for modification
class PizzaStore{
}
….
Design Principle
Separate out what changes from what remains the same
void createPizza(store, pizzaType){..}
void orderPizza(store,pizzaType){..}
How do we do that?
MumbaiStore PuneStore
PizzaStore
GurgaonStore ChennaiStore
abstract class PizzaStore{
abstract Pizza createPizza(type);
void orderPizza(pizzaType){ pizza = createPizza(pizzaType); pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box();}
}
Pizza pizza;
class MumbaiStore extends PizzaStore{
public Pizza createPizza(String pizzaType){ if(pizzaType.equals(“Cheese”)) return new MumbaiStyleCheesePizza(); if(pizzaType.equals(“Veggie”)) return new MumbaiStyleVeggiePizza();
….}
}
MumbaiStore PuneStore
PizzaStore
Code
PizzaStore.java
//Has reference to pizza (Pizza pizza;)
// Has orderPizza(String type) method which internally calls:// pizza = createPizza(type)// pizza.bake()// pizza.cut()// pizza.box()
//Has an// abstract createPizza(String type) method that returns Pizza
PuneStore.java
//extends PizzaStore
// Pizza createPizza(String type) method//returns pizza object based on the type
MumbaiStore.java
//extends PizzaStore
// Pizza createPizza(String type) method//returns pizza object based on the type
Factory Method
abstract Pizza createPizza(type);
abstract Product factoryMethod(String type);
A factory method is abstract so that the subclasses handle the object creation.
A factory method returns a product that is typically used within methods defined in the super class.
A factory method isolates the client code in the superclass from knowing what kind of concrete product is actually created.
A factory method may be parameterised to select among several variations of a product.
Customers!!!
Joel’s order
I need Pune style Pizza.PizzaStore puneStore = new PuneStore();
I want Cheese PizzapuneStore.orderPizza(“Cheese”);
Internally within the orderPizza methodPizza pizza = createPizza(“Cheese”);pizza.bake();pizza.cut();pizza.box();
MainClass.java
//public static void main(String[] a)
PizzaStore puneStore = new PuneStore();puneStore.orderPizza(“Cheese”);
MumbaiStyleCheesePizza PuneStyleCheesePizza
Pizza
Pizza.java
// Has bake() method// Has cut() method// Has box() method
MumbaiStyleCheesePizza.java//extends Pizza class
// Overrides bake() method
PuneStyleCheesePizza.java//extends Pizza class
// Overrides bake() method
Factory Method Pattern
Factory Pattern encapsulates the object creation by letting subclasses decide what objects to create.
Creator Classes
MumbaiStore PuneStore
PizzaStore
GurgaonStore ChennaiStore
Product Classes
Cheese Pizza
Veggie Pizza
Spicy Chicken Pizza
Barbeque Chicken Pizza
Pizza
Product
Concrete Product
Creator
Concrete Creator
factoryMethod()otherMethods()
factoryMethod()
Thank you!