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

Post on 12-Mar-2020

5 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

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

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?

Solution 16.1

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

Solution 16.2

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

• Consider the following example

Solution 16.3

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

Ordering Pizzas

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

A Simple Pizza Factory

Implementation

Reworking the Pizza Store

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

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

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

Allowing the Subclasses to Decide

A Remake of the Pizza Store

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

The Pizza Class Itself

The Concrete Subclasses

The Driver Program

Parallel Class 

Hierarchies

The Factory Method Design Pattern

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

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

Solution 16.4

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

Solution 16.5

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

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

Solution 16.6

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

Solution 16.7

top related