Factory Method

From CSSEMediaWiki
(Difference between revisions)
Jump to: navigation, search
Line 15: Line 15:
 
== How it works (Structure) ==
 
== How it works (Structure) ==
  
[[Image:Example.jpg]]
+
[[Image:Factory Method.jpg]]

Revision as of 05:43, 26 September 2008

(This is summarised from GoF design patterns book)

Intent

The factory method is useful when you wish to define an interface for creating an object, but you want to let subclasses decide which class to create. Factory Method lets an abstract/normal class defer instantiation of an object of a different class to subclasses of the abstract/normal class.

When to use it (Applicability)

Useful when:

  • A class cannot anticipate the class of an object it must create.
  • A class wants its subclasses to specify the objects it creates.
  • Classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate.


How it works (Structure)

Factory Method.jpg

Personal tools