Class 2 |
Intermediate Java 30-IT-397 |
|
Factory Pattern
-
Resource: http://www.patterndepot.com/put/8/factory.pdf
-
The factory pattern returns an instance of an object based on the parameters
passed to it.
-
It can return any one of a number of objects, as long as all objects it
can return either implement or extend the return type of the factory method
itself.
-
If this were not the case, the method simply would not work, because it
can only return objects of the same type of the declared return type.
-
We know that, with polymorphism, you can actually return a subclass when
the superclass is the declared return type.
-
What you need...
-
Classes that the factory method can instantiate.
-
A superclass that the classes (above) extend. Or, an interface that
they implement.
-
A factory class with a method that takes a parameter and has the logic
necessary to determine which class to return.
-
A slight variation of the Factory Pattern - class NumberFormat.
-
How could this be redesigned to be a factory pattern,
according to the patterndepot definition?
-
What are advantages/disadvantages of this method?
Factory Example... it's a toughy!
Singleton Pattern

Created by: Brandan Jones
December 17, 2001