01-03-2012, 02:46 PM
Factory Patterns:Factory Method and Abstract Factory
Factory Patterns
l Factory patterns are examples of creational patterns
l Creational patterns abstract the object instantiation process.
They hide how objects are created and help make the overall
system independent of how its objects are created and composed.
l Class creational patterns focus on the use of inheritance to decide
the object to be instantiated
é Factory Method
l Object creational patterns focus on the delegation of the
instantiation to another object
é Abstract Factory
Factory Patterns
l All OO languages have an idiom for object creation. In Java this
idiom is the new operator. Creational patterns allow us to write
methods that create new objects without explicitly using the new
operator. This allows us to write methods that can instantiate
different objects and that can be extended to instantiate other
newly-developed objects, all without modifying the method's
code! (Quick! Name the principle involved here!)
The Factory Method Pattern
l 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.
l Motivation
é Consider the following framework:
é The createDocument() method is a factory method.
The Factory Method Pattern
l Applicability
Use the Factory Method pattern in any of the following situations:
é A class can't anticipate the class of objects it must create
é A class wants its subclasses to specify the objects it creates
l Structure
The Factory Method Pattern
l Participants
é Product
Ý Defines the interface for the type of objects the factory method creates
é ConcreteProduct
Ý Implements the Product interface
é Creator
Ý Declares the factory method, which returns an object of type Product
é ConcreteCreator
Ý Overrides the factory method to return an instance of a ConcreteProduct
l Collaborations
é Creator relies on its subclasses to implement the factory method so that it
returns an instance of the appropriate ConcreteProduct
The Factory Method Pattern
l So what exactly does it mean when we say that "the Factory
Method Pattern lets subclasses decide which class to instantiate?"
é It means that Creator class is written without knowing what actual
ConcreteProduct class will be instantiated. The ConcreteProduct class
which is instantiated is determined solely by which ConcreteCreator
subclass is instantiated and used by the application.
é It does not mean that somehow the subclass decides at runtime which
ConreteProduct class to create
Factory Method Example 1 (Continued)
l Note that although the Client delegates the creation of a
ConcreteManipulator to a ConcreteFigure object, the focus of the
pattern is still on how a specific subclass of Figure creates one
particular type of Manipulator. So this is still the Factory Method
Pattern (a class creational pattern).
Factory Method Example 2 (Continued)
l Here's a MazeGame class with a createMaze() method:
/**
* MazeGame.
*/
public class MazeGame {
// Create the maze.
public Maze createMaze() {
Maze maze = new Maze();
Room r1 = new Room(1);
Room r2 = new Room(2);
Door door = new Door(r1, r2);
maze.addRoom(r1);
maze.addRoom(r2);
Design Patterns In Java Bob Tarr Factory Patterns
12
Factory Method Example 2 (Continued)
r1.setSide(MazeGame.North, new Wall());
r1.setSide(MazeGame.East, door);
r1.setSide(MazeGame.South, new Wall());
r1.setSide(MazeGame.West, new Wall());
r2.setSide(MazeGame.North, new Wall());
r2.setSide(MazeGame.East, new Wall());
r2.setSide(MazeGame.South, new Wall());
r2.setSide(MazeGame.West, door);
return maze;