1 / 26

Factory Method

Factory Method. Using new method. Kallu Pizza. More than one type of pizza. but the pressure is on to add more pizza types. encapsulating object creation. e ncapsulating object creation factory method. Reworking the PizzaStore class. Opening more branches of Kallu Pizza.

mills
Télécharger la présentation

Factory Method

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Factory Method

  2. Using newmethod

  3. Kallu Pizza

  4. More than one type of pizza

  5. but the pressure is on to add more pizza types encapsulating object creation

  6. encapsulating object creationfactory method

  7. Reworking the PizzaStore class

  8. Opening more branches of Kallu Pizza

  9. Quality & Variations

  10. Order pizza

  11. Making the New York store

  12. Declaring Factory Method

  13. How our new program should work?

  14. So how do they order? • First, Joel and Ethan need an instance of a PizzaStore. Joel needs to instantiate a ChicagoPizzaStore and Ethan needs a NYPizzaStore. • With a PizzaStore in hand, both Ethan and Joel call the orderPizza() method and pass in the type of pizza they want(cheese, veggie,etc). • To create the pizzas, the createPizza() method is called, which is defined in the two subclasses NYPizzaStore and the ChicagoPizzaStore. As we defined them, the NYPizzaStoreinstantiates a NY style pizza, and the ChicagoPizzaStore instantiates Chicago style pizza. In either case, the Pizza is returned to the orderPizza() method. • The orderPizza() method has no idea what kind of pizza was created, but it knows it is a pizza and it prepares, bakes, cuts, and boxes it for Joel and Ethan.

  15. Pizza class

  16. Adding NY and Chicago concrete subclasses

  17. Parallel Hierarchies

  18. Factory Method defines an interface for creating an object, but lets subclasses decide which class to instantiate. Factory method lets a class defer instantiates to subclasses.

More Related