overviewWhat is Abstract Factory pattern and what is it for? According to Design Patterns by GoF, the intent of Abstract Factory is to, Provide an interface for creating families of related or dependent objects without specifying their concrete classes. source: GoF Abstract Factory conforms to the principle of "program to an interface, not an implementation." In Abstract Factory pattern, the class that is responsible for creating concrete objects (Products) is said to be a Factory class. In order to create different types of concrete Products, different type of concrete Factory classes will be used. Each Factory is just a collection of factory methods (e.g. CreateProductA(), CreateProductB()). Each factory method creates a concrete Product of the same family. The client code's factory method creates the final product based on a given factory. This extra layer of abstraction is provided to the client code by introducing the concept of factory. This is the most common way to implement the Abstract Factory pattern. exampleAbstractFactory / AbstractProduct / Client
[1] A factory method that creates an empty maze to begin with, not a final end product. [2] Because each Factory knows how to make the product of their own family. The client code (MazeGame) simply has a factory method which depends on an abstract factory to create the final end Product - a final version of Maze. As demonstrated above, the Factory class adds an layer of abstraction to the client without exposing the implementation details of the products. Another good example of an Abstract Factory in use could be UI toolkits across Windows, Mac and Linux, UI composites such as windows, buttons as described on DZone. scenariosUse Abstract Factory patter when you want to:
comparisonAbstract Factory vs Builder:
They are quite similar with the following differences:
Abstract Factory vs. Factory Method
5 Comments
DP
10/16/2016 05:02:52 pm
Can we have an example of implementing abstract factory + factory method + builder pattern all together.
Reply
Hsufeng Ko
10/16/2016 11:27:37 pm
An 'Abstract Factory' and a 'Factory Method' patterns differ in their intention. Therefore, it won't make much sense to have an example with mixed intention, will it?
Reply
Erich Gamma
4/3/2017 11:25:58 pm
From GoF:
Hsufeng Ko
5/25/2020 03:05:46 pm
I'd agree some said the factory method is just a method, it can be overridden in a subclass, whereas the abstract factory is an object that has multiple factory methods on it. Therefore, I had modified my note to reflect that an Abstract Factory may build upon factory methods. Leave a Reply. |
Categories
All
Archives
May 2020
|