Design Patterns
What are they?
• Repeatable solution to commonly
occurring problems
• Language-independent strategies for
solving common o...
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
Mumbai
Thin crust
Fresh Veggies
Less sauce
Pune
Thick crust
Frozen Veggies
Lots of sauce and cheese
What kinda pizza’s do ...
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 MumbaiStyleChees...
if(store==“Mumbai”){
if(type==“Cheese”)
return new MumbaiStyleCheesePizza();
if(type==“SpicyChickenPizza”)
return new Mumb...
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 orderPiz...
How do we do that?
MumbaiStore PuneStore
PizzaStore
GurgaonStore ChennaiStore
abstract class PizzaStore{
abstract Pizza createPizza(type);
void orderPizza(pizzaType){
pizza = createPizza(pizzaType);
p...
class MumbaiStore extends PizzaStore{
public Pizza createPizza(String pizzaType){
if(pizzaType.equals(“Cheese”))
return ne...
MumbaiStore PuneStore
PizzaStore
Code
PizzaStore.java
//Has reference to pizza (Pizza pizza;)
// Has orderPizza(String type) method which internally calls:
// p...
PuneStore.java
//extends PizzaStore
// Pizza createPizza(String type) method
//returns pizza object based on the type
Mumb...
Factory Method
abstract Pizza createPizza(type);
abstract Product factoryMethod(String type);
A factory method
is abstract...
Customers!!!
Joel’s order
I need Pune style Pizza.
PizzaStore puneStore = new PuneStore();
I want Cheese Pizza
puneStore.orderPizza(“Ch...
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
...
Factory Method Pattern
Factory Pattern encapsulates the object creation by
letting subclasses decide what objects to creat...
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! 
Upcoming SlideShare
Loading in …5
×

Design pattern intro+ factory method pattern

522 views

Published on

An example of factory method pattern.

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
522
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Design pattern intro+ factory method pattern

  1. 1. Design Patterns
  2. 2. What are they? • Repeatable solution to commonly occurring problems • Language-independent strategies for solving common object oriented design problems • They provide a lingo
  3. 3. Is-A Relationship (Inheritance) Animal
  4. 4. Has-A Relationship (Composition)
  5. 5. Animal a = new Animal() Dog d = new Dog() Animal aDog = new Dog()
  6. 6. Pizza Store Prepare Bake Cut Box
  7. 7. Create Bake Cut Box
  8. 8. Menu Cheese Pizza Veggie Pizza Spicy Chicken Pizza Barbeque Chicken Pizza
  9. 9. Cheese Pizza Veggie Pizza Spicy Chicken Pizza Barbeque Chicken Pizza Pizza Is-A Relationship
  10. 10. Mumbai Thin crust Fresh Veggies Less sauce Pune Thick crust Frozen Veggies Lots of sauce and cheese What kinda pizza’s do people in Mumbai and Pune like?
  11. 11. Store Locations Mumbai Pune
  12. 12. How would the customer’s order for a pizza be? Choose store location Choose pizza Mumbai Cheese Pizza
  13. 13. public static void main(String[] a){ PizzaStore p = new PizzaStore(); p.orderPizza(“Pune”,”Cheese”); } class Main { }
  14. 14. 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
  15. 15. 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()
  16. 16. Design Principle Classes should be open for extension but closed for modification class PizzaStore{ } ….
  17. 17. Design Principle Separate out what changes from what remains the same void createPizza(store, pizzaType){..} void orderPizza(store,pizzaType){..}
  18. 18. How do we do that? MumbaiStore PuneStore PizzaStore GurgaonStore ChennaiStore
  19. 19. abstract class PizzaStore{ abstract Pizza createPizza(type); void orderPizza(pizzaType){ pizza = createPizza(pizzaType); pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); } } Pizza pizza;
  20. 20. class MumbaiStore extends PizzaStore{ public Pizza createPizza(String pizzaType){ if(pizzaType.equals(“Cheese”)) return new MumbaiStyleCheesePizza(); if(pizzaType.equals(“Veggie”)) return new MumbaiStyleVeggiePizza(); …. } }
  21. 21. MumbaiStore PuneStore PizzaStore Code
  22. 22. 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
  23. 23. 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
  24. 24. 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.
  25. 25. Customers!!!
  26. 26. Joel’s order I need Pune style Pizza. PizzaStore puneStore = new PuneStore(); I want Cheese Pizza puneStore.orderPizza(“Cheese”); Internally within the orderPizza method Pizza pizza = createPizza(“Cheese”); pizza.bake(); pizza.cut(); pizza.box();
  27. 27. MainClass.java //public static void main(String[] a) PizzaStore puneStore = new PuneStore(); puneStore.orderPizza(“Cheese”);
  28. 28. MumbaiStyleCheesePizza PuneStyleCheesePizza Pizza
  29. 29. 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
  30. 30. Factory Method Pattern Factory Pattern encapsulates the object creation by letting subclasses decide what objects to create.
  31. 31. Creator Classes MumbaiStore PuneStore PizzaStore GurgaonStore ChennaiStore
  32. 32. Product Classes Cheese Pizza Veggie Pizza Spicy Chicken Pizza Barbeque Chicken Pizza Pizza
  33. 33. Product Concrete Product Creator Concrete Creator factoryMethod() otherMethods() factoryMethod()
  34. 34. Thank you! 

×