Download presentation
Presentation is loading. Please wait.
Published byOphelia Reed Modified over 9 years ago
1
Abstract Factory and Factory Method CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns
2
Intent 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
3
Abstract 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
4
Solutions 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
5
Design Solution for Abstract Factory Factory createProduct() AbstractProduct virtual methods ConcreteProdA methods ConcreteProdB methods Client Note: this is an abbreviated design
6
Participants 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
7
Stack Example StackFactory createStack() Stack push(), pop() ArrayStack push(), pop() LinkedStack Push(), pop() Client … Stack s; s = StackFactory.createStack(); … s.pop(); … return new ArrayStack();
8
Stack 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()
9
Factories 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
10
Abstract Factory Consequences 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
11
Factory 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
12
Design Solution for Factory Method Factory factoryMethod() Product virtual methods ConcreteProduct methods ConcreteFactory factoryMethod()
13
Application-Document Example Application createDoc() Document virtual methods MyDocument methods MyApplication createDoc() return new MyDocument();
14
Factory Method Consequences Separation of interface from implementation, providing implementation flexibility Connects parallel hierarchies for consistency
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.