Factory Method

From CSSEMediaWiki
(Difference between revisions)
Jump to: navigation, search
(New page: (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 d...)
 
Line 9: Line 9:
 
Useful when:
 
Useful when:
  
* A class cannot anticipate the class of an object it must create.
+
* A class cannot anticipate the class of an object it must create.
* A class wants its subclasses to specify the objects it creates.
+
* 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.
+
* Classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate.

Revision as of 05:09, 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.

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.
Personal tools