Abstract Factory and Factory Method CS 124 Reference
Abstract Factory and Factory Method CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns
Intent of both patterns l l 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 creatorproduct relationship Abstract Factory uses the Factory Method pattern
Abstract Factory l l Intent: provide an interface for creating objects without specifying their concrete classes Example: Stacks, Queues, and other data structures l l 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 l Abstract class for widget, supporting class for specific platform widget
Solutions in C++ l Use of header file (class declarations) and implementation file (method definitions) ok but limited l l l 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
Design Solution for Abstract Factory Abstract. Product create. Product() virtual methods Client Note: this is an abbreviated design Concrete. Prod. A Concrete. Prod. B methods
Participants l Factory l l (Abstract) Product: declares an interface for a type of product object Concrete Product l l l implements the operations to create concrete product objects actual pattern includes abstract and concrete factory classes that generalizes the relationship between factory and 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
Stack Example return new Array. Stack(); Stack. Factory Stack create. Stack() push(), pop() Client … Stack s; s = Stack. Factory. create. Stack(); … s. pop(); … Array. Stack Linked. Stack push(), pop() Push(), pop()
Stack Example (C++) l Stack class defines virtual methods l l l Array. Stack and Linked. Stack are derived classes of Stack and contain concrete implementations Stack. Factory class defines a create. Stack() method that returns a ptr to a concrete stack l l push(), pop(), etc. Stack *create. Stack() { return new Array. Stack(); } Client programs need to be aware of Stack and Stack. Factory classes only l No need to know about Array. Stack()
Factories in Java l l l Stack is an Interface Array. Stack and Linked. Stack implement Stack. Factory returns objects of type Stack through its factory methods l Select class of the concrete prodcut it supplies to client objects l If using info from requesting client, can hardcode selection logic and choice of factory objects l Can separate selection logic for concrete factories from the data it uses to make the selection
Abstract Factory Consequences l Factory class or method can be altered without affecting the application l l Factory class can be responsible for creating different types of objects l l l Concrete classes are isolated e. g. , Data. Structure factory that returns stacks, queues, lists, etc. “product families” Promotes product consistency
Factory Method l l 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 l l Generalization: Application creates Documents Concretized by: MS Paint creates Gifs, MS Word creates Word Documents, MS Excel creates spreadsheets
Design Solution for Factory Method Factory Product factory. Method() virtual methods Concrete. Factory Concrete. Product factory. Method() methods
Application-Document Example Application Document create. Doc() virtual methods My. Application My. Document create. Doc() methods return new My. Document();
Factory Method Consequences l l Separation of interface from implementation, providing implementation flexibility Connects parallel hierarchies for consistency
- Slides: 14