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 full-size slide

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

    View full-size slide

  3. Why You Should Care
     Create more resilient code

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  6. Other Influential Books Followed

    View full-size slide

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

    View full-size slide

  8. Factory Concept (not a pattern)

    View full-size slide

  9. Factory Concept (not a pattern)

    View full-size 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 full-size 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 full-size slide

  12. Factory Method Pattern

    View full-size 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 full-size slide

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

    View full-size slide

  15. Abstract Factory Pattern

    View full-size 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 full-size slide

  17. Thank you!
    - Doug Corbett

    View full-size slide