FACTORY METHOD Flashcards
Factory Method is a
creational design pattern that provides
an interface for creating objects in a superclass, but allows
subclasses to alter the type of objects that will be created.
What does the Factory Method pattern suggest?
It suggests replacing direct object construction calls with calls to a special factory method.
What should you replace when using the Factory Method pattern?
Replace direct object construction calls using the new operator.
What is used instead of the new operator in the Factory Method pattern?
A special factory method is used.
the factory method in the base
class should have its return type declared as
this interface.
The Product
declares the interface, which is common to all objects that can be produced by the creator and its subclasses.
Concrete Products
are different implementations of the product interface.
The Creator class
declares the factory method that returns
new product objects. It’s important that the return type of this
method matches the product interface.
Concrete Creators
override the base factory method so it
returns a different type ofproduct.
Use the Factory Method when you don’t know beforehand
the exact types and dependencies of the objects your code should
work with.
The Factory Method separates
product construction code from
thecodethatactuallyusestheproduct.Thereforeit’seasierto
extend the product construction code independently from the
rest of the code.
Use the Factory Method when you want to provide users
of your library or framework with a way to extend its internal
components.
how would the frame-
work recognize that your subclass should be used instead of a
standard component?
The solution is to reduce the code that constructs components across the framework into a single factory method and let anyone override this method in addition to extending the component itself.