ABSTRACT FACTORY Flashcards
Abstract Factory is
a creational design pattern that lets you
produce families of related objects without specifying their
concrete classes.
Abstract Products declare
interfaces for a set of distinct but
related products which make up a product family.
Concrete Products
are various implementations of abstract
products, grouped by variants. Each abstract product (chair/
sofa) must be implemented in all given variants (Victorian/
Modern).
The Abstract Factory interface
declares a set of methods for
creating each of the abstract products.
Concrete Factories implement
creation methods of the
abstract factory. Each concrete factory corresponds to a specific variant of products and creates only those product variants.
When should you use the Abstract Factory?
When your code needs to work with various families of related products, but you don’t want it to depend on the concrete classes of those products, as they might be unknown beforehand or to allow for future extensibility.
What does the Abstract Factory provide?
An interface for creating objects from each class of the product family.
Why does using the Abstract Factory help avoid mistakes in object creation?
Because by creating objects through the Abstract Factory interface, you eliminate the risk of creating the wrong variant of a product that doesn’t match the products already created by your app.
When should you consider implementing the Abstract Factory?
When you have a class with a set of Factory Methods that blur its primary responsibility.
How does the Abstract Factory relate to well-designed programs?
In well-designed programs, each class is responsible for only one thing. If a class handles multiple product types, it might be worth extracting its factory methods into a standalone factory class or a full-blown Abstract Factory implementation.
What is the first step in implementing an Abstract Factory?
Map out a matrix of distinct product types versus variants of these products.
What should you declare for all product types?
Declare abstract product interfaces for all product types and ensure all concrete product classes implement these interfaces.
What does the abstract factory interface include?
A set of creation methods for all abstract products.
How are concrete factories implemented?
Implement a set of concrete factory classes, one for each product variant.
Where should the factory initialization code reside?
Somewhere in the app, depending on the configuration or environment. It should instantiate a specific concrete factory class and pass it to all classes that construct products.