




























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





























Overview
Significance:
Use the factory pattern...
When you don’t know what class object you need ahead of time
When all of the potential classes are in the same subclass hierarchy
To centralize class selection code
When you don’t want the user to know every subclass
To encapsulate object creation
Significance: Solution
Solution:
Building the Diagram Definition of the
Factory Method
Factory Method: Interface
Product: This is the interface of the objects the factory method creates
Ex: Dog Factory
Product: Dog
Method that must be implemented via the interface: speak();
Factory Method: Concrete Creator
Provides an interface for the creation of Products
Ex: Dog Factory Product: Dog Concrete Product: Poodle, Dalmation, Beagle Method that must be implemented: speak(); Concrete Creator: DogFactory that returns the Dog when the function is called i.e. Return new Poodle();
Factory Method: Creator
Declares the factory method which returns an object of type Product.
Product: Dog Concrete Product: Poodle, Dalmation, Beagle Method that must be implemented: speak(); Concrete Creator: DogFactory Creator: Gets different dogs from the factory
Factory Method: Behavior
Advantages of using the Factory Method
Factory Method Example
Example: UML
Interface: Color with method draw
Concrete Classes: Red, Blue, Green Implementing the same interface
Concrete Creator: ColorFactory
Creator: FactoryPatternDemo
Red Blue Green
Color
ColorFactory
+getColor():Color
Product: Interface
Example: UML
Interface: Color
Red Blue Green
Color
ColorFactory
+getColor():Color