Factory Method Pattern Creational Intent» Define an interface for creating an object but lets subclasses decide the specific class to instantiate > Delegate creation to the appropriate subclass Also known as» Virtual constructor Factory-1
Motivation Frameworks use abstract classes» To define and maintain relationships between objects» May be responsible for creating them Factory-2
Motivation 2 Consider an application that manages multiple document types» Application class manages documents create as required from New in menu > Knows when to create but not what to create» Document class is application specific > Drawing document > Text document > Spreadsheet document Factory-3
Motivation 3 Application subclasses redefine an abstract create operation to return the appropriate type of object We call these methods factory methods because they are responsible for manufacturing an object Factory-4
Example Architecture DOCUMENT * make * open * close * save * docs:[ ] doc = create_document( ) docs.add(doc) doc.open_document APPLICATION + create_document( ) open_document new_document MY_DOC + make open close save Result = mydoc.make MY_APP + create_document mydoc Factory-5
Abstract Architecture Product * product Creator * factorymethod( ) * anoperation() * product = factorymethod( ) ConcreteProduct + concreteproduct ConcreteCreator + factorymethod() Result = concreteproduct.make( ) Factory-6
Scenario A trivial scenario» The appropriate concrete method is invoked through polymorphism. Factory-7
Participants Product» Defines the interface of objects the factory method creates Concrete product» Implements the Product interface Factory-8
Participants 2 Creator» Declares the factory method, which returns an object of type Product.» May define a default implementation of the factory method» May call the factory method to create a Product object Concrete creator» Overrides the default factory method to return an instance of the appropriate subclass Factory-9
Applicability A class cannot anticipate the class of the objects 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 Factory-10
Consequences Provides hooks for subclasses» Avoids creating an object directly Connects parallel class hierarchies» A class may delegate some of its responsibilities to a separate class» For a given class hierarchy (e.g. Figures) delegated responsibilities may be different for each type of Figure (stretch a line is different from stretch a box) > As a consequence a parallel hierarchy of manipulator classes may be created Factory-11
Parallel Hierarchy Example Figure * createmanipulator * Manipulator * stretch * Line + Box + createmanipulator + createmanipulator + Line + stretch + Box + stretch + Factory-12
Related Patterns Abstract Factory is often implemented with Factory Methods Factory Methods are usually called within the Template pattern Prototypes do not require sub-classing Creator but they often require an initialize operation» Factory Method does not require an initialze operation Factory-13
Factory Method in Java API The routine getgraphics() on Java Component API obtains a Graphics object that draws on the specific Component object to which getgraphics() belongs. Box is a Container class that can be created as a horizontal or vertical container.» public static Box createhorizontalbox()» public static Box createverticalbox() Factory-14