Your SlideShare is downloading. ×
Design pattern intro+ factory method pattern
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Design pattern intro+ factory method pattern

282
views

Published on

An example of factory method pattern.

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
282
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Design Patterns
  • 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. Is-A Relationship (Inheritance) Animal
  • 4. Has-A Relationship (Composition)
  • 5. Animal a = new Animal() Dog d = new Dog() Animal aDog = new Dog()
  • 6. Pizza Store Prepare Bake Cut Box
  • 7. Create Bake Cut Box
  • 8. Menu Cheese Pizza Veggie Pizza Spicy Chicken Pizza Barbeque Chicken Pizza
  • 9. Cheese Pizza Veggie Pizza Spicy Chicken Pizza Barbeque Chicken Pizza Pizza Is-A Relationship
  • 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. Store Locations Mumbai Pune
  • 12. How would the customer’s order for a pizza be? Choose store location Choose pizza Mumbai Cheese Pizza
  • 13. public static void main(String[] a){ PizzaStore p = new PizzaStore(); p.orderPizza(“Pune”,”Cheese”); } class Main { }
  • 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. 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. Design Principle Classes should be open for extension but closed for modification class PizzaStore{ } ….
  • 17. Design Principle Separate out what changes from what remains the same void createPizza(store, pizzaType){..} void orderPizza(store,pizzaType){..}
  • 18. How do we do that? MumbaiStore PuneStore PizzaStore GurgaonStore ChennaiStore
  • 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. class MumbaiStore extends PizzaStore{ public Pizza createPizza(String pizzaType){ if(pizzaType.equals(“Cheese”)) return new MumbaiStyleCheesePizza(); if(pizzaType.equals(“Veggie”)) return new MumbaiStyleVeggiePizza(); …. } }
  • 21. MumbaiStore PuneStore PizzaStore Code
  • 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. 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. 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. Customers!!!
  • 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. MainClass.java //public static void main(String[] a) PizzaStore puneStore = new PuneStore(); puneStore.orderPizza(“Cheese”);
  • 28. MumbaiStyleCheesePizza PuneStyleCheesePizza Pizza
  • 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. Factory Method Pattern Factory Pattern encapsulates the object creation by letting subclasses decide what objects to create.
  • 31. Creator Classes MumbaiStore PuneStore PizzaStore GurgaonStore ChennaiStore
  • 32. Product Classes Cheese Pizza Veggie Pizza Spicy Chicken Pizza Barbeque Chicken Pizza Pizza
  • 33. Product Concrete Product Creator Concrete Creator factoryMethod() otherMethods() factoryMethod()
  • 34. Thank you! 