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
Tweet

More Decks by Doug Corbett

Other Decks in Programming

Transcript

  1. The Factory Pattern Is Not
    What You Think It Is
    By Doug Corbett

    View Slide

  2. What are Patterns?
     Proven software design solutions
     Apply across domains

    View Slide

  3. Why You Should Care
     Create more resilient code

    View Slide

  4. Why You Should Care
     Create more resilient code
     Benefit from the wisdom of others

    View Slide

  5. Gang of Four
     Authored the first book on
    software design patterns.
     23 Patterns
     3 Categories
    Creational Patterns
    Structural Patterns
    Behavioral Patterns

    View Slide

  6. Other Influential Books Followed

    View Slide

  7. Creational Patterns
    1. Abstract Factory
    2. Builder
    3. Factory Method
    4. Prototype
    5. Singleton

    View Slide

  8. Factory Concept (not a pattern)

    View Slide

  9. Factory Concept (not a pattern)

    View Slide

  10. 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

    View Slide

  11. Factory Method Pattern
     Characteristics
     Factories implement an interface
     Specific factory created might not be specified by the
    caller, but by configuration or IoC container

    View Slide

  12. Factory Method Pattern

    View Slide

  13. 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

    View Slide

  14. Abstract Factory Pattern
     Characteristics
     Similar to Factory Method pattern
     Supports families of objects

    View Slide

  15. Abstract Factory Pattern

    View Slide

  16. 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

    View Slide

  17. Thank you!
    - Doug Corbett

    View Slide