Factory Method Design Pattern

2,000 views

Published on

Presented by Geoff Burns at the Feb 2009 meeting of the Melbourne Patterns Group.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,000
On SlideShare
0
From Embeds
0
Number of Embeds
54
Actions
Shares
0
Downloads
46
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Factory Method Design Pattern

  1. 1. 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.
  2. 2. Why Use Patterns <ul><li>Someone has already solved your problem </li></ul><ul><li>Common terms facilitate communication </li></ul><ul><li>The patterns emphasize the importance of context </li></ul>
  3. 3. Design Strategies recommended by GoF <ul><li>Design to Interfaces </li></ul><ul><li>Favour composition over inheritance </li></ul><ul><li>Find what varies and encapsulate it </li></ul>
  4. 4. Themes of Creational Patterns <ul><li>Encapsulate Knowledge about which concrete classes the system uses </li></ul><ul><li>Hide how these classes are created and put together </li></ul>
  5. 5. 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.
  6. 6. Applicability <ul><li>a class can't anticipate the class of objects it must create. </li></ul><ul><li>a class wants its subclasses to specify the objects it creates. </li></ul><ul><li>hiding which helper subclass has been delegated responsibility for implementing a task. </li></ul>
  7. 7. Factory Method Structure
  8. 8. Consequences <ul><li>Eliminates binding to a specific implementation at instance creation. </li></ul><ul><li>Can increase sub-classing. </li></ul><ul><li>Provides hooks for subclasses </li></ul><ul><li>Connects parallel class hierarchies </li></ul>
  9. 9. Implementation <ul><li>With or without default implementation. </li></ul><ul><li>Parameterized factory methods. </li></ul><ul><li>Using templates to avoid sub-classing. </li></ul><ul><li>Naming conventions. </li></ul>
  10. 10. Examples
  11. 11. Example 2

×