Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Factory Pattern Is Not What You Think It Is

The Factory Pattern Is Not What You Think It Is

The factory pattern was not what Doug thought it was a few years ago. Feeling foolish, Doug asked a some co-workers what they knew about the factory pattern. He discovered that he either needs to find some more sophisticated co-workers or the misunderstanding is more common than one might have expected.

In this presentation, Doug will describe and differentiate the factory concept, factory method and abstract factory pattern so that other developers will avoid making the same mistake.

Doug Corbett

June 13, 2017

More Decks by Doug Corbett

Other Decks in Programming


  1. Why You Should Care  Create more resilient code 

    Benefit from the wisdom of others
  2. Gang of Four  Authored the first book on software

    design patterns.  23 Patterns  3 Categories Creational Patterns Structural Patterns Behavioral Patterns
  3. Factory Concept (not a pattern)  Pros  Separates instantiation

    from representation  It encapsulates class family instantiation  Hides public instantiation  Cons  Caller has to know what it wants to create  It violates the Open/Close principle
  4. Factory Method Pattern  Characteristics  Factories implement an interface

     Specific factory created might not be specified by the caller, but by configuration or IoC container
  5. Factory Method Pattern  Pros  Consumer has no concrete

    references to factories or objects. Loose coupling.  Initialization can be encapsulated within the factory.  Can prevent instantiation of objects outside the factory.  Cons  Need new factory for each new object
  6. Abstract Factory Pattern  Pros  Like Factory Method Pattern,

    but supports factories that can handle families of objects.  Great for supporting extensibility for other departments or third party vendors.  Cons  More complex