Factory Method Design Pattern


GoF Statement: Defines an interface for creating an object, but lets subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
Category: Creational
UML Diagram:  
Description of the Demonstration:

The parent class, PizzaShop, defines an interface consisting of two functions. The first, PizzaShop::orderPizza, is used by "customers" to place orders for a particular type of pizza (cheese, clam, veggie, or Pepperoni). Each subclass of PizzaShop (NYPizzaShop, ChicagoPizzaShop, and CaliforniaPizzaShop) must then override the virtual Factory Method function PizzaShop::createPizza() in order to create its own particular style of cheese, clam, veggie, or Pepperoni pizza. Following the Factory Method Design Pattern the class PizzaShop defers instantiation of pizzas to its subclasses.


Code:

Click here to download a zip file containing all the source files and an executable of the Demonstration. See the _ReadMe.txt file in the zip.