Title: Abstract Factory and Factory Method
1Abstract Factoryand Factory Method
- CS 124
- Reference Gamma et al(Gang-of-4), Design
Patterns
2Intent of both patterns
- Separate the implementation of objects from their
use by defining an interface for creating the
objects without specifying their concrete classes - Abstract Factory focus is on allowing multiple
implementations of a product - Factory Method focus is on generalizing the
creator-product relationship - Abstract Factory uses the Factory Method pattern
3Abstract Factory
- Intent provide an interface for creating objects
without specifying their concrete classes - Example Stacks, Queues, and other data
structures - Want users to not know or care how these
structures are implemented (separation) - Example UI toolkit to support multiple
look-and-feel standards, e.g., Motif, PM - Abstract class for widget, supporting class for
specific platform widget
4Solutions in C
- Use of header file (class declarations) and
implementation file (method definitions) ok but
limited - Header file usually contains private declarations
which are technically part of the implementation - Change in implementation requires that the
application using the data structure be
recompiled - Alternative create an abstract superclass with
(pure) virtual data structure methods
5Design Solution for Abstract Factory
Factory createProduct()
AbstractProduct virtual methods
Client
ConcreteProdA methods
ConcreteProdB methods
Note this is an abbreviated design
6Participants
- Factory
- implements the operations to create concrete
product objects - actual pattern includes abstract and concrete
factory classes that generalizes the relationship
between factory and product - (Abstract) Product declares an interface for a
type of product object - Concrete Product
- defines a product object to be created by the
corresponding concrete factory - implements the abstract product interface
- Client uses only Factory and Abstract Product
7Stack Example
return new ArrayStack()
StackFactory createStack()
Stack push(), pop()
Client
ArrayStack push(), pop()
LinkedStack Push(), pop()
Stack s s StackFactory.createStack() s.pop
()
8Stack Example (C)
- Stack class defines virtual methods
- push(), pop(), etc.
- ArrayStack and LinkedStack are derived classes of
Stack and contain concrete implementations - StackFactory class defines a createStack() method
that returns a ptr to a concrete stack - Stack createStack() return new ArrayStack()
- Client programs need to be aware of Stack and
StackFactory classes only - No need to know about ArrayStack()
9Factories in Java
- Stack is an Interface
- ArrayStack and LinkedStack implement Stack
- StackFactory returns objects of type Stack
through its factory methods - Select class of the concrete prodcut it supplies
to client objects - If using info from requesting client, can
hardcode selection logic and choice of factory
objects - Can separate selection logic for concrete
factories from the data it uses to make the
selection
10Abstract FactoryConsequences
- Factory class or method can be altered without
affecting the application - Concrete classes are isolated
- Factory class can be responsible for creating
different types of objects - e.g., DataStructure factory that returns stacks,
queues, lists, etc. - product families
- Promotes product consistency
11Factory Method
- Define an interface for creating an object, but
let subclasses decide which class to instantiate.
Factory method lets a class defer instantiation
to subclasses - Example Generalizing the relationship between
an application and the documents it processes - Generalization Application creates Documents
- Concretized by MS Paint creates Gifs, MS Word
creates Word Documents, MS Excel creates
spreadsheets
12Design Solution forFactory Method
Factory factoryMethod()
Product virtual methods
ConcreteProduct methods
ConcreteFactory factoryMethod()
13Application-Document Example
Application createDoc()
Document virtual methods
MyDocument methods
MyApplication createDoc()
return new MyDocument()
14Factory Method Consequences
- Separation of interface from implementation,
providing implementation flexibility - Connects parallel hierarchies for consistency