- Factory of factories
- creates a factory of related objects using abstraction
- provides one level of interface higher than the factory pattern
- used to return one of several factories
Where to use ?
- A system needs independent of how its products are created, composed, and represented
- An alternative to Facade to hide platform-specific classes
- Interface for creating a family of related objects, without explicitly specifying their classes.
- Only the product interfaces are revealed, the implementations remains hidden to the clients
Implementation example
- Interface Color implemented by 3 concrete classes Red, Green & Blue
- Interface Shape implemented by 3 concrete classes Circle, Rectangle & Square
- Abstract class AbstractFactory having 2 methods to get both types of objects (for returning Color and Shape reference types)
- 2 Factory classes (ColorFactory and ShapeFactory) implementing the common abstract class AbstractFactory. Both are returning specific types of objects based on parameter passed
- Class FactoryProvider having method to return specific factory object (return type AbstractFactory) based on passed parameter
- Client will get a factory object (AbstractFactory type) by passing parameter (factory-name) by calling FactoryProvider
- Then Client will get the object by passing parameter to AbstractFactory
Example of Abstract factories
- java.sql.Connection
Abstract factory which create Statements, PreparedStatements, CallableStatements,... for each database type. - java.awt.Toolkit
Abstract superclass of all actual implementations of the AWT.
Subclasses of Toolkit are used to bind the various components to particular native toolkit implementations (Java AWT) - javax.swing.LookAndFeel
Abstract Swing factory to switch between several look and feel for the components displayed (Java Swing)
No comments:
Post a Comment
Note: only a member of this blog may post a comment.