Quick revision
The short answer
Abstract factory is justified when we must create families of related objects that have to be used together, behind one factory interface, so the whole family can be switched at once and never mixed. A cloud provider factory producing storage, queue and secrets clients for AWS or GCP is a good example, and JAXP's DocumentBuilderFactory and TransformerFactory pick a parser implementation family at run time. In Spring applications, profiles or conditional configuration often replace a hand-written abstract factory. The cost is an extra interface per product and per family, and adding a new product type changes every factory.
What to remember
In this answer, we saw that abstract factory is justified for families of related objects that must be used together, such as a cloud provider's storage, queue and secrets clients, or JAXP's parser and transformer families chosen at run time. In Spring, profiles and conditional configuration often play the factory's role per deployment, while a hand-written factory still helps when the family is chosen per tenant or request. The price is many interfaces and the fact that adding a product type changes every factory.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Abstract factory is justified when we must create families of related objects that have to be used together, behind one factory interface, so the whole family can be switched at once and never mixed. A cloud provider factory producing storage, queue and secrets clients for AWS or GCP is a good example, and JAXP's DocumentBuilderFactory and TransformerFactory pick a parser implementation family at run time. In Spring applications, profiles or conditional configuration often replace a hand-written abstract factory. The cost is an extra interface per product and per family, and adding a new product type changes every factory.
Why this question is asked
Abstract factory is the most heavyweight of the creational patterns, and it is often introduced too early, wrapping a single implementation in three layers of interfaces. It is also exactly the right tool in a few situations, and knowing those situations is what interviewers want to hear. In this answer, we will recap what a family of objects is, build a cloud provider family, look at the same idea in JAXP, see how Spring configuration often replaces the pattern, and weigh its costs.
1A Quick Recap: Families of Related Objects
The abstract factory pattern provides one interface for creating several related objects, called products, without naming their concrete classes. Each concrete factory creates a complete, consistent family: one implementation of every product, all designed to work together.
The key word is family. If we only need to create one kind of object with varying implementations, a simple factory or a Supplier is enough. Abstract factory earns its cost when there are several product types, and mixing implementations from different families would be a bug, for example an AWS queue client with GCP credentials, or a parser from one XML library with a transformer from another that does not understand its node classes.
2A Cloud Provider Family
Suppose a product runs on AWS for most customers and on GCP for some who require it, and each deployment needs storage, a message queue and secrets. Each set of clients shares credentials, region and retry settings, and must come from the same provider.
Notice what the pattern guarantees: code that receives a CloudClients can only get products from one provider, configured once with one set of credentials. That consistency is the real benefit, more than the ability to switch.
3The Same Idea in JAXP
The JDK's XML APIs are a classic abstract factory. DocumentBuilderFactory creates DOM parsers and TransformerFactory creates transformers for XSLT, and each is found at run time through system properties, configuration files, service providers or the built-in default. A parser implementation family, such as the JDK's built-in Xerces and Xalan copies, provides factories whose products work together.
The reason the JDK chose this design was that applications needed to replace the XML implementation without changing code, and that each implementation needed its own concrete classes for parsers, documents and transformers. Application code only ever sees the abstract types in javax.xml.parsers and javax.xml.transform.
4How Spring Often Replaces It
In a Spring application, the container already knows how to create objects and choose implementations by configuration. Instead of a hand-written factory, we can declare the family as beans and let a profile or a property select which set exists.
The configuration class plays the role of the concrete factory: one class per family, each defining all the products together, so they cannot be mixed by accident. Spring Boot's auto-configuration uses the same idea with @ConditionalOnClass and @ConditionalOnProperty. A hand-written abstract factory is still useful when the choice happens at run time per request or per tenant, rather than once per deployment, because profiles cannot switch families inside a running application.
5What the Pattern Costs
Every product needs an interface, and every family needs a concrete factory plus one implementation per product. With three products and two families, that is one factory interface, three product interfaces, two factories and six implementations, eleven types where a single-provider application needs three.
The pattern also has an asymmetry that the original book points out. Adding a new family is easy: write a new factory and its products, and nothing else changes. Adding a new product type is hard: the factory interface gets a new method, and every existing factory must implement it. So the pattern suits domains where the set of product types is stable and the set of families grows, not the other way round.
| Change | What we must edit | Cost |
|---|---|---|
| Add a family (Azure) | One new factory and its products | Low, nothing existing changes |
| Add a product (cache client) | Factory interface and every factory | High, touches all families |
| Change one implementation | That product class only | Low |
| Only one family exists | Interfaces nobody else implements | Pure overhead |
6Follow-up questions
How is abstract factory different from factory method?
Factory method creates one kind of product, and the choice is made by a subclass overriding one method. Abstract factory creates a family of several related products through one object, and the choice of family is made by choosing which factory object to use.
When would you choose a hand-written factory over Spring profiles?
When the family must be chosen at run time inside one application, for example per tenant, with some tenants on AWS and others on GCP. Profiles pick one family per deployment; a factory object can be selected per request.
What is a sign that an abstract factory is unjustified?
Only one concrete factory exists and none is planned, or products from different families could be mixed safely. In both cases, simple constructors or beans are clearer and cheaper.
7Conclusion
In this answer, we saw that abstract factory is justified for families of related objects that must be used together, such as a cloud provider's storage, queue and secrets clients, or JAXP's parser and transformer families chosen at run time. In Spring, profiles and conditional configuration often play the factory's role per deployment, while a hand-written factory still helps when the family is chosen per tenant or request. The price is many interfaces and the fact that adding a product type changes every factory.
Open with
Abstract factory makes sense when I need to create a family of related objects that must be used together, behind one factory interface, so the whole family is switched at once and never mixed.
Then, if they want more
A good example is a CloudClients interface with methods for storage, a queue and secrets, with an AwsClients and a GcpClients implementation, each built with its own credentials and region. Code that receives a CloudClients can only get S3, SQS and AWS secrets together, or GCS, Pub/Sub and GCP secrets together.
In the JDK, JAXP works this way: DocumentBuilderFactory and TransformerFactory find a parser implementation family at run time, so the application only sees the abstract types.
In Spring applications I often do not need to write the pattern by hand, because a profile or a conditional configuration class can define the whole family of beans for AWS or GCP, and services just inject the interfaces.
I would still write a real factory if the family has to be chosen per tenant or per request at run time. The costs are an interface for every product and an implementation per product per family, and the asymmetry: a new family is easy to add, but a new product type means changing the factory interface and every factory.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "When is the abstract factory pattern justified? Give an example from Java or from cloud...": https://scrivio-interview-library.netlify.app/java/creational-patterns/d25733e3a9/