Week 5 - Factory Method Flashcards
What does the factory method design pattern do?
It is a design pattern for ‘manufacturing’ objects. It allows a program to work with instances of concrete classes of an interface when the program cannot anticipate which class it is working with.
What us the intent of the factory method?
To define an interface for creating an object, but to let the subclasses decide which class to instantiate.
What is the motivation of factory method?
When an application uses a number fo different types of an object - it knows when to create an object but not what specific subtype of that classes to create because it is independent from the object (i.e. It only knows about interfaces).
What is the Applicability of the factory method?
Used when a class can’t anticipate which specific type of object to create.
How do you implement the factory method?
It is a method/function in the concrete creator class. This method is not called factory method in the implementation, but rather something else appropriate to the program. When it is called, it returns an instance of a particular concrete product class that implements the product interface. The main creator class relies on subclasses of it to define the particular factory methods, each one returning a different product class type that is appropriate for that creator.
When is the factory method used?
It is almost always used when interfaces are involved. It is used to create and instance of the concrete abject that implements the interface.
Give some examples of factory method in JHD?
Creating the Drawing in DrawApplication
Creating Handles in Figure
Creating Figures in CreationTool
Creating different tools for dragging shapes, dragging handles, dragging an ‘area tracker’ within SelectionTool
What are the disadvantages of the factory method?
Clients might have to create an new subclass of the creator class whenever they create a new concreteProduct class.
What are the advantages of factory method?
Provides hooks for subclasses.
Eliminates the need to bind static classes into code, allowing for more flexibility.
It works with parallel class hierarchies, by defining the connection between these hierarchies and knows what classes belong together.
What are hooks in classes?
A point in a class that is flexible and allows the future extension of the class in its subclasses. The hook will have sensible default behaviour which is overwritten to extend the class via its subclass.
What is a a parallel hierarchiy?
A hierarchy structure that has two trees of class hierarchies that are connected at the top. Each class in the hierarchies matches a similar class in the other hierarchy.
What are issues with implementing factory method?
The creator might be abstract and not provide an implementation of the factory method it declares. Thus implementation needs to be defined in the subclasses.
What is a parameterised factory method?
A factory method that takes parameters and creates and return a different kind of product depending on the parameters. It is very flexible in that one factory method could create all product classes as long as the correct parameter is supplied.