Abstract factory Flashcards

(39 cards)

1
Q

What is the main purpose of the Abstract Factory Pattern?

A

To create families of related objects without specifying concrete classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How does the Abstract Factory Pattern differ from the Factory Method Pattern?

A

Factory Method creates one product, while Abstract Factory creates multiple related products.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why is the Abstract Factory Pattern classified as a creational design pattern?

A

Because it focuses on object creation in a structured and flexible way.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What problem does the Abstract Factory Pattern solve in software design?

A

It ensures consistency among related objects and hides object creation from the client.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How does the Abstract Factory Pattern promote loose coupling?

A

: It abstracts object creation, allowing the client to work with interfaces instead of concrete classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the key advantage of using an Abstract Factory over directly instantiating objects?

A

It provides consistent object creation and makes it easier to switch product families.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is meant by the term “family of related objects” in the Abstract Factory Pattern?

A

A group of products that are designed to work together, like UI elements in a theme.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What would happen if an Abstract Factory did not enforce consistency across related products?

A

Incompatible objects might be used together, causing runtime errors or UI inconsistencies.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How does the Abstract Factory Pattern help maintain the Open-Closed Principle?

A

New product families can be added without modifying existing client code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

When should you use the Abstract Factory Pattern instead of the Factory Method Pattern?

A

When you need to create multiple related objects instead of just one.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the main components of the Abstract Factory Pattern?

A

Abstract Factory, Concrete Factory, Abstract Product, Concrete Product, Client.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What role does the Abstract Factory play in the pattern?

A

It defines interfaces for creating families of related products.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What role do the Concrete Factories play in the pattern?

A

They implement the Abstract Factory and create specific product families.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is an Abstract Product in the Abstract Factory Pattern?

A

It defines a common interface for a family of products.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do Concrete Products differ from Abstract Products?

A

They provide specific implementations of abstract products.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How does the Abstract Factory ensure that all created objects belong to the same family?

A

Each Concrete Factory creates only products from the same family.

17
Q

How does polymorphism play a role in the Abstract Factory Pattern?

A

The client interacts with abstract products, while factories return concrete implementations.

18
Q

Why does the Abstract Factory Pattern typically involve interfaces or abstract classes?

A

To ensure flexibility and enforce consistency across product families.

19
Q

What happens if a client code directly instantiates product classes instead of using an Abstract Factory?

A

It violates encapsulation, making it harder to switch product families.

20
Q

What is the difference between an Abstract Factory and a Simple Factory?

A

Abstract Factory creates multiple related products, while Simple Factory creates only one type of product.

21
Q

How would you apply the Abstract Factory Pattern in a UI framework that supports different themes?

A

Create factories for each theme (e.g., DarkThemeFactory, LightThemeFactory) to produce UI elements.

22
Q

How would the Abstract Factory Pattern help in a game with different types of worlds (e.g., Medieval, Sci-Fi, Fantasy)?

A

Each world would have a factory to create world-specific objects (e.g., characters, environments).

23
Q

How does the Abstract Factory Pattern improve database connection handling when working with multiple databases?

A

It provides a factory for each database type (e.g., MySQLFactory, PostgreSQLFactory).

24
Q

How would the Abstract Factory Pattern be useful in designing a cross-platform application?

A

Each platform (Windows, macOS, Linux) would have its own factory to create platform-specific components.

25
What are some disadvantages of using the Abstract Factory Pattern?
Increased complexity due to multiple classes. Hard to add new product types without modifying all factories.
26
Give an example where the Abstract Factory Pattern would not be an ideal choice.
When only one type of object is needed, multiple factories are unnecessary.
27
How does the Abstract Factory Pattern compare to using if-else or switch statements for object creation?
It eliminates conditional logic, making the code more flexible and scalable.
28
How would the Abstract Factory Pattern be useful in an e-commerce system that supports multiple payment gateways?
Each payment gateway (PayPal, Stripe, Google Pay) would have its own factory to create related objects.
29
Can the Abstract Factory Pattern be used in multi-threaded applications? If so, what should be considered?
Yes, but ensure thread safety when accessing shared factory instances.
30
How would you modify an Abstract Factory implementation to allow runtime selection of the factory type?
Use a Factory Selector that dynamically picks a factory based on configuration.
31
What is the Abstract Factory in the provided C++ code snippet? class Button { public: virtual void render() = 0; }; class WindowsButton : public Button { public: void render() override { std::cout << "Rendering Windows Button" << std::endl; } }; class MacOSButton : public Button { public: void render() override { std::cout << "Rendering MacOS Button" << std::endl; } }; class GUIFactory { public: virtual Button* createButton() = 0; }; class WindowsFactory : public GUIFactory { public: Button* createButton() override { return new WindowsButton(); } }; class MacOSFactory : public GUIFactory { public: Button* createButton() override { return new MacOSButton(); } };
GUIFactory class.
32
What is the Concrete Factory in the provided C++ code snippet?
WindowsFactory and MacOSFactory classes.
33
What is the Abstract Product in the provided C++ code snippet?
Button class.
34
What is the Concrete Product in the provided C++ code snippet?
WindowsButton and MacOSButton classes.
35
How does the provided C++ code follow the Abstract Factory Pattern?
It defines a GUIFactory to create platform-specific UI elements.
36
How would you modify the Abstract Factory implementation to support an additional product, such as a Checkbox?
Add an Abstract Checkbox interface and Concrete Checkbox implementations.
37
What would happen if a Concrete Factory did not implement all the product creation methods defined in the Abstract Factory?
It would break consistency, potentially causing runtime errors.
38
What is the benefit of using dynamic binding in the Abstract Factory Pattern?
It allows runtime flexibility, enabling the factory to create objects dynamically.
39
How can you implement the Abstract Factory Pattern in Python, Java, or JavaScript?
Python: Use abstract base classes (ABC). Java: Use interfaces and factory classes. JavaScript: Use ES6 classes with factory functions.