the observer design patternblk/cs3667/constructionpatterns/ch16-factory-method.pdflibrary code needs...

35
The Factory Method Design Pattern Some common applications A factory method is used to create objects, whose subclasses can then override to specify the derived type of product that will be created More generally, the term factory method is often used to refer to any method whose main purpose is creation of objects. Factory methods are common in toolkits and frameworks where library code needs to create objects of types which may be subclassed by applications using the framework. Parallel class hierarchies often require objects from one hierarchy to be able to create appropriate objects from another. In the GoF classification this is a creational design pattern

Upload: others

Post on 12-Mar-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The Observer Design Patternblk/cs3667/ConstructionPatterns/ch16-factory-method.pdflibrary code needs to create objects of types which may be subclassed by applications using the framework

The Factory Method Design Pattern

• Some common applications– A factory method is used to create objects, whose subclasses can then override to specify the derived type of product that will be created

– More generally, the term factory method is often used to refer to any method whose main purpose is creation of objects.

– Factory methods are common in toolkits and frameworks where library code needs to create objects of types which may be subclassed by applications using the framework.

– Parallel class hierarchies often require objects from one hierarchy to be able to create appropriate objects from another.

• In the GoF classification this is a creational design pattern

Page 2: The Observer Design Patternblk/cs3667/ConstructionPatterns/ch16-factory-method.pdflibrary code needs to create objects of types which may be subclassed by applications using the framework

Acknowledgements• Materials were borrowed from

– Design Patterns in Java by Steven Metsker and William Wake (textbook for course)

– Head First Design Patterns by Elisabeth Freeman, Eric Freeman, Bert Bates, and Kathy Sierra

Page 3: The Observer Design Patternblk/cs3667/ConstructionPatterns/ch16-factory-method.pdflibrary code needs to create objects of types which may be subclassed by applications using the framework

A Classic Example: Iterators• Iterators are an example of the iterator design pattern we will study later in the course

• Iterators with Java collections are created using a factory method associated with the Collections interface

• This isolates each caller to iterator() from knowing which class is actually created

Challenge 16.1What is the actual class for the iterator object for this specific code?

Page 4: The Observer Design Patternblk/cs3667/ConstructionPatterns/ch16-factory-method.pdflibrary code needs to create objects of types which may be subclassed by applications using the framework

Solution 16.1

Page 5: The Observer Design Patternblk/cs3667/ConstructionPatterns/ch16-factory-method.pdflibrary code needs to create objects of types which may be subclassed by applications using the framework

Creating New Objects• There are a lot of ways to create new objects in Java and most are not examples of factory methods

• If answering this question, think of methods that are present for every object in Java

Page 6: The Observer Design Patternblk/cs3667/ConstructionPatterns/ch16-factory-method.pdflibrary code needs to create objects of types which may be subclassed by applications using the framework

Solution 16.2

Page 7: The Observer Design Patternblk/cs3667/ConstructionPatterns/ch16-factory-method.pdflibrary code needs to create objects of types which may be subclassed by applications using the framework

Don’t Be Mislead• The APIs in Java may be labeled “factory” without actually being a factory design pattern

• Consider the following example

Page 8: The Observer Design Patternblk/cs3667/ConstructionPatterns/ch16-factory-method.pdflibrary code needs to create objects of types which may be subclassed by applications using the framework

Solution 16.3

Page 9: The Observer Design Patternblk/cs3667/ConstructionPatterns/ch16-factory-method.pdflibrary code needs to create objects of types which may be subclassed by applications using the framework

Pizzas, yummy, yummy• The following 16 slides are based on an example from Head First Design Patterns

• This easy to understand example involves making pizzas in pizza stores

• We will develop this example first as factory method (this chapter) and then add an abstract factory (next chapter)

• We first evolve the need for a design pattern by developing “specific” code and eventually abstracting it to a design pattern

Page 10: The Observer Design Patternblk/cs3667/ConstructionPatterns/ch16-factory-method.pdflibrary code needs to create objects of types which may be subclassed by applications using the framework

Ordering Pizzas

Page 11: The Observer Design Patternblk/cs3667/ConstructionPatterns/ch16-factory-method.pdflibrary code needs to create objects of types which may be subclassed by applications using the framework

The Need for Frequent Changes• Fast food joints and pizza joints frequently have to modify their food products to keep attracting customers

• We need software that can accommodate frequent changes

Page 12: The Observer Design Patternblk/cs3667/ConstructionPatterns/ch16-factory-method.pdflibrary code needs to create objects of types which may be subclassed by applications using the framework

A Simple Pizza Factory

Page 13: The Observer Design Patternblk/cs3667/ConstructionPatterns/ch16-factory-method.pdflibrary code needs to create objects of types which may be subclassed by applications using the framework

Implementation

Page 14: The Observer Design Patternblk/cs3667/ConstructionPatterns/ch16-factory-method.pdflibrary code needs to create objects of types which may be subclassed by applications using the framework

Reworking the Pizza Store

Page 15: The Observer Design Patternblk/cs3667/ConstructionPatterns/ch16-factory-method.pdflibrary code needs to create objects of types which may be subclassed by applications using the framework

Class Diagram

• This is a programming idiom that is commonly mistaken for a true design pattern; the problem is we want to share common methods when possible

Page 16: The Observer Design Patternblk/cs3667/ConstructionPatterns/ch16-factory-method.pdflibrary code needs to create objects of types which may be subclassed by applications using the framework

Franchising Pizza Stores• We want different factories for different regions but the factory should only control how the pizza is created and not the other operations

Page 17: The Observer Design Patternblk/cs3667/ConstructionPatterns/ch16-factory-method.pdflibrary code needs to create objects of types which may be subclassed by applications using the framework

A Framework for a Pizza Store• We make the pizzaStore abstract; the methods for prepare, bake, cut and box can be shared

• The method for creating the pizza must be implemented by the subclasses

Page 18: The Observer Design Patternblk/cs3667/ConstructionPatterns/ch16-factory-method.pdflibrary code needs to create objects of types which may be subclassed by applications using the framework

Allowing the Subclasses to Decide

Page 19: The Observer Design Patternblk/cs3667/ConstructionPatterns/ch16-factory-method.pdflibrary code needs to create objects of types which may be subclassed by applications using the framework

A Remake of the Pizza Store

Page 20: The Observer Design Patternblk/cs3667/ConstructionPatterns/ch16-factory-method.pdflibrary code needs to create objects of types which may be subclassed by applications using the framework

The Factory Method

• The factory method handles the creation of an object

• The parameters allow variation in the product created

• The method is abstract so the subclasses themselves are required to handle object creation

Page 21: The Observer Design Patternblk/cs3667/ConstructionPatterns/ch16-factory-method.pdflibrary code needs to create objects of types which may be subclassed by applications using the framework

The Pizza Class Itself

Page 22: The Observer Design Patternblk/cs3667/ConstructionPatterns/ch16-factory-method.pdflibrary code needs to create objects of types which may be subclassed by applications using the framework

The Concrete Subclasses

Page 23: The Observer Design Patternblk/cs3667/ConstructionPatterns/ch16-factory-method.pdflibrary code needs to create objects of types which may be subclassed by applications using the framework

The Driver Program

Page 24: The Observer Design Patternblk/cs3667/ConstructionPatterns/ch16-factory-method.pdflibrary code needs to create objects of types which may be subclassed by applications using the framework

Parallel Class 

Hierarchies

Page 25: The Observer Design Patternblk/cs3667/ConstructionPatterns/ch16-factory-method.pdflibrary code needs to create objects of types which may be subclassed by applications using the framework

The Factory Method Design Pattern

Page 26: The Observer Design Patternblk/cs3667/ConstructionPatterns/ch16-factory-method.pdflibrary code needs to create objects of types which may be subclassed by applications using the framework

Checking a Client’s Credit at Oozinoz• With this design you can provide credit information whether or not the credit agency is online

• The problem is that the user needs to know which method to instantiate but only you know if the agency is up and running

Page 27: The Observer Design Patternblk/cs3667/ConstructionPatterns/ch16-factory-method.pdflibrary code needs to create objects of types which may be subclassed by applications using the framework

Revising the Design• You want to commit to an interface for creating an object but keep control which object is actually instantiated

• Here are the requirements

Page 28: The Observer Design Patternblk/cs3667/ConstructionPatterns/ch16-factory-method.pdflibrary code needs to create objects of types which may be subclassed by applications using the framework

Solution 16.4

Page 29: The Observer Design Patternblk/cs3667/ConstructionPatterns/ch16-factory-method.pdflibrary code needs to create objects of types which may be subclassed by applications using the framework

The createCreditCheck method

• The user should be able to call the createCreditCheck method to get the credit checking object

• But you need to retain control over which type of credit check will be provided (online or offline) depending on whether the credit agency is online or not

Page 30: The Observer Design Patternblk/cs3667/ConstructionPatterns/ch16-factory-method.pdflibrary code needs to create objects of types which may be subclassed by applications using the framework

Solution 16.5

Page 31: The Observer Design Patternblk/cs3667/ConstructionPatterns/ch16-factory-method.pdflibrary code needs to create objects of types which may be subclassed by applications using the framework

Parallel Hierachies• A parallel hierarchy is a pair of class hierarchies in which each class in one hierarchy has a corresponding class in the other hierarchy

• Machines can start a new job once the current job is completed

• You want a separate class called MachinePlanner that will make the most efficient use of the machines

• These will form parallel hierarchies

Page 32: The Observer Design Patternblk/cs3667/ConstructionPatterns/ch16-factory-method.pdflibrary code needs to create objects of types which may be subclassed by applications using the framework

The Planner

• Some assumptions– Mixers and fusers are always available for additional work; they use a basic planner

– The other machines (the StarPress and ShellAssembly have individual planners) have their own individual planners

Page 33: The Observer Design Patternblk/cs3667/ConstructionPatterns/ch16-factory-method.pdflibrary code needs to create objects of types which may be subclassed by applications using the framework

Solution 16.6

Page 34: The Observer Design Patternblk/cs3667/ConstructionPatterns/ch16-factory-method.pdflibrary code needs to create objects of types which may be subclassed by applications using the framework

The createPlanner method

• Assume calling the createPlanner returns the basic planner as a default

• However, for the ShellAssembler and StarPress classes this default will have to be overriden to return their individual planners

Page 35: The Observer Design Patternblk/cs3667/ConstructionPatterns/ch16-factory-method.pdflibrary code needs to create objects of types which may be subclassed by applications using the framework

Solution 16.7