Sunday, June 8, 2014

Head First C++ code for PizzaStore : Abstract Factory Pattern

After Factory method, Chapter 4 in the Head First Design Patterns book explains Abstract Factory design pattern.

" The Abstract Factory Pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes. "

Both Factory Method and Abstract Factory create objects but the key differences are :

  1. Factory Method create objects through inheritance, that is you need to extend a class and override a factory method. Whereas Abstract Factory create through object composition, it provide an abstract type for creating a family of products. Subclasses of this type create products. To use the Abstract Factory , you instantiate one and pass it into some code that is written against the abstract type.
  2. Factory Method is to just create one Product and each subclass is to decides which concrete class of Product to instantiate. Whereas Abstract Factory provides an abstract interface for creating a family of Products.
  3. Methods to create products in an Abstract Factory are often implemented with a Factory Method!
In the example code PizzaIngredientFactory is implemented as an Abstract Factory because we need to create family of Products ( the ingredients). PizzaIngredientFactory provides an abstract interface for creating a family of products. The subclasses as 'NYPizzaIngredientFactory' uses factory method like 'createDough()' in an Abstract Factory pattern.

Here is the class diagram of PizzaStoreV2






























The c++ code is build using VS2010 IDE. You can download code from here.


No comments: