Software Design and Architecture
Factory Method Design Patterns
Factory Method
The factory method pattern is a creational
pattern that uses factory methods to deal with
the problem of creating objects without having to
specify the exact class of the object that will be
created. This is done by creating objects by
calling a factory method.
Intent
Define an interface for creating an object, but let
subclasses decide which class to instantiate.
Factory Method lets a class defer instantiation to
subclasses.
Factory Method
Motivation
Consider the example of an ImageReader and
subclasses which implement the ImageReader
interface and instantiate objects of ImageReader for
reading images based on the type of image e.g.,
JpegReader, GifReader etc.
Factory Method
Example
Factory Method
Collaborations
The creator relies on its subclasses to define the
factory method so that it returns an instance of
the appropriate subclass.
Factory Method
Factory Method
Related Patterns
Abstract Factory pattern often implemented with
Factory Methods
Template Methods contain calls to Factory
Methods
The End
Assignment
What are the Advantages and
disadvantages of factory Method