What is the main purpose of the Abstract Factory pattern?
The Abstract Factory pattern provides an interface to create families of related or dependent objects without specifying their concrete classes. It helps to create objects that belong together and ensures they are compatible.
Click to reveal answer
intermediate
How does the Abstract Factory pattern differ from the Factory Method pattern?
The Abstract Factory creates families of related objects, while the Factory Method creates one object. Abstract Factory groups multiple factories under one interface, Factory Method defines a method to create a single object.
Click to reveal answer
beginner
Name the main components of the Abstract Factory pattern.
1. Abstract Factory: declares creation methods for abstract products. 2. Concrete Factory: implements creation methods for concrete products. 3. Abstract Product: declares interfaces for product types. 4. Concrete Product: implements product interfaces. 5. Client: uses the factory and products through abstract interfaces.
Click to reveal answer
intermediate
Why is the Abstract Factory pattern useful in system design?
It helps to isolate the client code from concrete classes, making the system more flexible and easier to extend. It supports scalability by allowing new product families without changing existing code.
Click to reveal answer
beginner
Give a real-life analogy for the Abstract Factory pattern.
Think of a furniture store that sells sets of furniture like Victorian or Modern styles. The store (abstract factory) can create matching chairs, sofas, and tables (products) for each style without the customer needing to know the details.
Click to reveal answer
What does the Abstract Factory pattern primarily help with?
ADefining the structure of a database schema
BCreating families of related objects without specifying their concrete classes
CManaging object lifecycles with garbage collection
DCreating a single object with a factory method
✗ Incorrect
The Abstract Factory pattern focuses on creating families of related objects without exposing their concrete classes.
Which component in Abstract Factory declares the interface for creating products?
AAbstract Factory
BConcrete Factory
CConcrete Product
DClient
✗ Incorrect
The Abstract Factory declares the interface for creating abstract products.
In the Abstract Factory pattern, who uses the factories to get products?
AConcrete Product
BAbstract Product
CConcrete Factory
DClient
✗ Incorrect
The Client uses the abstract factory interface to get products without knowing their concrete classes.
Which of the following is NOT a benefit of using Abstract Factory?
AIsolates client from concrete classes
BSupports adding new product families easily
CAutomatically manages memory allocation
DEnsures products created are compatible
✗ Incorrect
Memory management is not a responsibility of the Abstract Factory pattern.
Which real-life example best illustrates Abstract Factory?
AA furniture store selling matching sets of chairs and tables
BA vending machine that sells snacks
CA single coffee machine brewing one type of coffee
DA library cataloging books
✗ Incorrect
A furniture store selling matching sets represents creating families of related objects.
Explain the Abstract Factory pattern and its main components in your own words.
Think about how a client can get different related objects without knowing their exact classes.
You got /5 concepts.
Describe a scenario where using the Abstract Factory pattern improves system design and why.
Consider a system that needs to support multiple themes or product lines.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the Abstract Factory pattern in system design?
easy
A. To create families of related objects without specifying their concrete classes
B. To allow direct instantiation of concrete classes
C. To enforce a single instance of a class
D. To simplify database queries
Solution
Step 1: Understand the role of Abstract Factory
The Abstract Factory pattern is designed to create related objects without exposing their concrete classes.
Step 2: Compare options with the pattern's purpose
To create families of related objects without specifying their concrete classes correctly describes this purpose, while others describe different patterns or unrelated concepts.
Final Answer:
To create families of related objects without specifying their concrete classes -> Option A
Quick Check:
Abstract Factory = Create related objects [OK]
Hint: Abstract Factory creates related objects without concrete classes [OK]
Common Mistakes:
Confusing Abstract Factory with Singleton pattern
Thinking it creates only one object
Mixing it up with Builder or Factory Method
2. Which of the following is the correct way to declare an Abstract Factory interface in a typical object-oriented design?
easy
A. var AbstractFactory = { create: function() { return ProductA; } }
B. class AbstractFactory { new ProductA(); new ProductB(); }
C. function AbstractFactory() { return new ProductA(); }
D. interface AbstractFactory { createProductA(): ProductA; createProductB(): ProductB; }
Solution
Step 1: Identify correct interface syntax
Abstract Factory is usually declared as an interface with methods to create related products.
Step 2: Analyze each option
interface AbstractFactory { createProductA(): ProductA; createProductB(): ProductB; } correctly declares an interface with creation methods. Options A, B, and C do not follow interface or abstract factory conventions.
Hint: Check which factory instance is passed to client [OK]
Common Mistakes:
Assuming ModernFactory is used instead
Confusing method names or return values
Expecting errors due to missing interface
4. In the following Abstract Factory implementation, what is the main issue causing a runtime error?
class Factory {
createProduct() {
return new Product();
}
}
class Product {}
const factory = new Factory();
const product = factory.createProduct();
console.log(product.name.toUpperCase());
medium
A. Missing interface declaration for Factory
B. Product class does not have a 'name' property, causing undefined access
C. Factory class cannot instantiate Product directly
D. createProduct method is not returning an object
Solution
Step 1: Analyze the Product class and usage
The Product class is empty and does not define a 'name' property.
Step 2: Understand the console.log statement
Accessing product.name returns undefined, and calling toUpperCase() on it causes a TypeError runtime error.
Final Answer:
Product class does not have a 'name' property, causing undefined access -> Option B
Quick Check:
Accessing missing property = Product class does not have a 'name' property, causing undefined access [OK]
Hint: Check if accessed properties exist on created objects [OK]
Common Mistakes:
Thinking Factory cannot instantiate Product
Expecting interface is mandatory at runtime
Assuming createProduct returns null or undefined
5. You are designing a UI toolkit that supports multiple themes (Light and Dark). Using the Abstract Factory pattern, which design ensures that all UI components (Button, Checkbox) are consistent with the selected theme?
hard
A. Create Buttons and Checkboxes directly in client code based on theme
B. Use a single factory that creates default Buttons and Checkboxes ignoring theme
C. Create separate factories for Light and Dark themes, each producing themed Buttons and Checkboxes
D. Use a factory that creates only Buttons and let client create Checkboxes
Solution
Step 1: Understand the need for consistency
Abstract Factory helps create families of related objects that match a theme or style.
Step 2: Evaluate design options
Create separate factories for Light and Dark themes, each producing themed Buttons and Checkboxes creates separate factories for each theme, ensuring all components are consistent. Other options break consistency or delegate creation improperly.
Final Answer:
Create separate factories for Light and Dark themes, each producing themed Buttons and Checkboxes -> Option C
Quick Check:
Theme consistency = Separate factories [OK]
Hint: Use one factory per theme to keep components consistent [OK]
Common Mistakes:
Mixing themes in one factory
Creating components directly in client code
Partial factory implementations missing products
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.