Factory Method Pattern Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method lets a class defer instantiation to subclasses.
Why Use Patterns Someone has already solved your problem Common terms facilitate communication The patterns emphasize the importance of context
Design Strategies recommended by GoF Design to Interfaces  Favour composition over inheritance Find what varies and encapsulate it
Themes of Creational Patterns Encapsulate Knowledge about which concrete classes the system uses Hide how these classes are created and put together
Factory Method Pattern Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method lets a class defer instantiation to subclasses.
Applicability a class can't anticipate the class of objects it must create.  a class wants its subclasses to specify the objects it creates.  hiding which helper subclass has been delegated responsibility for implementing a task.
Factory Method Structure
Consequences Eliminates binding to a specific implementation at instance creation. Can increase sub-classing. Provides hooks for subclasses   Connects parallel class hierarchies
Implementation With or without default implementation. Parameterized factory methods. Using templates to avoid sub-classing.  Naming conventions.
Examples
Example 2

Factory Method Design Pattern

  • 1.
    Factory Method PatternDefine an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
  • 2.
    Why Use PatternsSomeone has already solved your problem Common terms facilitate communication The patterns emphasize the importance of context
  • 3.
    Design Strategies recommendedby GoF Design to Interfaces Favour composition over inheritance Find what varies and encapsulate it
  • 4.
    Themes of CreationalPatterns Encapsulate Knowledge about which concrete classes the system uses Hide how these classes are created and put together
  • 5.
    Factory Method PatternDefine an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
  • 6.
    Applicability a classcan't anticipate the class of objects it must create. a class wants its subclasses to specify the objects it creates. hiding which helper subclass has been delegated responsibility for implementing a task.
  • 7.
  • 8.
    Consequences Eliminates bindingto a specific implementation at instance creation. Can increase sub-classing. Provides hooks for subclasses Connects parallel class hierarchies
  • 9.
    Implementation With orwithout default implementation. Parameterized factory methods. Using templates to avoid sub-classing. Naming conventions.
  • 10.
  • 11.