Factory Method
From CSSEMediaWiki
(Difference between revisions)
Line 4: | Line 4: | ||
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. | 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 == | + | == When to use it (Applicability) == |
Useful when: | Useful when: | ||
Line 11: | Line 11: | ||
* 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. | ||
+ | |||
+ | |||
+ | == How it works (Structure) == | ||
+ | |||
+ | [[Image:Example.jpg]] |
Revision as of 05:34, 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.