factory Method Flashcards

(53 cards)

1
Q

What is the Factory Method Pattern?

A

The Factory Method Pattern is a creational design pattern that provides an interface for creating objects while allowing subclasses to decide which specific class to instantiate.

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

What is the main intent of the Factory Method Pattern?

A

Define an interface for creating an object.

Let subclasses decide which class to instantiate.

Defer instantiation to subclasses.

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

How does the Factory Method Pattern improve flexibility?

A

It allows new object types to be introduced without modifying the base class.
It decouples client code from the specific concrete classes being instantiated.

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

How does the Factory Method Pattern work?

A

A base class defines a factory method (createDocument()).

Subclasses override the factory method to specify which type of object to create.

The base class defers instantiation to its subclasses.

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

What real-world analogy can explain the Factory Method Pattern?

A

A restaurant menu acts as an interface for ordering food.

Each restaurant (subclass) prepares dishes differently, even if they have the same name.

Customers order from the menu but don’t need to know how the dishes are made.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the components of the Factory Method Pattern?

A

Abstract Product (e.g., Document) - Defines a common interface.

Concrete Products (e.g., WordDocument, PDFDocument, HTMLDocument) - Implement specific behaviors.

Creator (Base Class) (e.g., Application) - Defines the factory method.

Concrete Creators (Subclasses) (e.g., WordApplication, PDFApplication) - Override the factory method to create specific products.

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

What is an abstract product in the Factory Method Pattern?

A

It is the base class or interface that defines the common behavior for all objects created by the Factory Method.
Example: Document class in a word processing application.

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

What are concrete products in the Factory Method Pattern?

A

They are specific implementations of the abstract product.
Example: WordDocument, PDFDocument, HTMLDocument.

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

How does the Factory Method Pattern achieve decoupling?

A

The base class defines the factory method but doesn’t know which specific subclass will be instantiated.

The subclasses decide which object to create, reducing dependency on specific concrete classes.

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

What are the key benefits of the Factory Method Pattern?

A

Decouples object creation from client code.
Easier to add new object types without modifying existing code.
Supports flexibility and extensibility.
Encapsulates the object creation process.

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

What are the key takeaways of the Factory Method Pattern?

A

defines an interface for object creation.

Delegates instantiation to subclasses.

Promotes flexibility and extensibility.

Used when object creation needs to be decoupled from the main program logic.

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

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

A

It delegates object creation to subclasses, avoiding hardcoded dependencies.

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

How does the Factory Method Pattern promote flexibility in object creation?

A

It allows new object types to be introduced without modifying existing code.

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

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

A

Because it focuses on creating objects in a structured and flexible way.

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

How does the Factory Method Pattern help achieve loose coupling between classes?

A

It separates object creation logic from the client code, reducing dependencies.

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

What is the difference between the Factory Method Pattern and a simple factory?

A

The Factory Method allows subclasses to decide what to create, while a Simple Factory centralizes object creation.

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

Why does the Factory Method Pattern use inheritance to create objects instead of directly instantiating them in the base class?

A

To allow subclasses to define specific object creation without modifying the base class.

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

What would happen if an application did not use the Factory Method Pattern but instead directly instantiated objects in the base class?

A

The base class would be tightly coupled to specific object types, making it harder to extend.

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

How does the Factory Method Pattern help in maintaining the Open-Closed Principle?

A

New classes can be added without modifying existing code, keeping the system extendable.

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

What is the downside of not using the Factory Method Pattern when working with multiple object types?

A

The code would require modifications whenever a new type is introduced, violating extensibility.

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

Can the Factory Method Pattern be used with dependency injection? If so, how?

A

Yes, by injecting the factory into a class instead of hardcoding object creation.

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

What are the key components of the Factory Method Pattern?

A

Creator (Base Class), Concrete Creator (Subclass), Abstract Product, Concrete Product, and Factory Method.

23
Q

What role does the creator class play in the Factory Method Pattern?

A

It defines the factory method but does not specify which product to create.

24
Q

What role does the concrete creator play in the Factory Method Pattern?

A

It overrides the factory method to specify the exact product type to create.

25
What is an abstract product in the Factory Method Pattern?
A common interface for all product types.
26
How do concrete products differ from the abstract product in the Factory Method Pattern?
Concrete products implement the abstract product’s interface with specific behavior.
27
How does polymorphism play a role in the Factory Method Pattern?
The client interacts with abstract products, while the actual object type is decided at runtime.
28
Why does the Factory Method Pattern typically involve an interface or an abstract class?
To allow multiple concrete implementations while ensuring a consistent interface.
29
How does the Factory Method defer object instantiation to its subclasses?
By defining a factory method that subclasses override to create specific objects.
30
What is the difference between a Factory Method and an Abstract Factory?
Factory Method creates a single product type, Abstract Factory creates families of related products.
31
What happens if a subclass forgets to override the Factory Method?
It may result in runtime errors or return a default or null object.
32
How would you apply the Factory Method Pattern to a notification system that sends emails, SMS, and push notifications?
Create a NotificationFactory with subclasses for EmailNotification, SMSNotification, and PushNotification.
33
How would the Factory Method Pattern help in designing a game with different enemy types (e.g., zombies, aliens, robots)?
Each EnemyFactory subclass would create a specific enemy type.
34
How would the Factory Method Pattern be beneficial in a document editor that supports Word, PDF, and HTML file types?
A DocumentFactory can create different document types without modifying the main editor.
35
How would you modify an existing class hierarchy to incorporate the Factory Method Pattern without breaking existing functionality?
Introduce an abstract factory method and move object creation logic to subclasses.
36
What are some disadvantages of using the Factory Method Pattern?
Increased complexity due to extra classes. Overhead from inheritance and subclassing.
37
Give an example where the Factory Method Pattern would not be an ideal choice.
When only one type of object is needed, making a factory unnecessary.
38
How does the Factory Method Pattern compare to using an if-else or switch statement for object creation?
The Factory Method avoids conditional logic, making the code easier to extend.
39
If a software application needs to create different types of database connections (e.g., MySQL, PostgreSQL, SQLite), how can the Factory Method Pattern be used?
A DatabaseConnectionFactory can create different database connection objects based on configuration.
40
Can the Factory Method Pattern be used in multi-threaded applications? If so, what should be considered?
Yes, but thread safety must be ensured when creating shared objects.
41
How would you modify a Factory Method implementation to allow caching of created objects?
Store created objects in a cache (map or dictionary) and reuse them when needed.
42
In the given C++ code snippet, what is the Factory Method? class Document { public: virtual void open() = 0; }; class WordDocument : public Document { public: void open() override { std::cout << "Opening Word document..." << std::endl; } }; class Application { public: virtual Document* createDocument() = 0; }; class WordApplication : public Application { public: Document* createDocument() override { return new WordDocument(); } };
createDocument() in the Application class.
43
What is the Abstract Product in the C++ code snippet? class Document { public: virtual void open() = 0; }; class WordDocument : public Document { public: void open() override { std::cout << "Opening Word document..." << std::endl; } }; class Application { public: virtual Document* createDocument() = 0; }; class WordApplication : public Application { public: Document* createDocument() override { return new WordDocument(); } };
Document class.
44
What is the Concrete Product in the C++ code snippet?
WordDocument class.
45
What is the Creator in the C++ code snippet?
Application class.
46
What is the Concrete Creator in the C++ code snippet?
WordApplication class.
47
How would you modify the Factory Method implementation to support an ExcelDocument?
Create a ExcelDocument class and a ExcelApplication subclass that overrides createDocument().
48
What would happen if a factory method returned a different type of object than expected?
It could break polymorphism and cause runtime errors.
49
What is the benefit of using dynamic binding in the Factory Method Pattern?
It allows runtime selection of the appropriate object type.
50
How can you implement the Factory Method Pattern in Python, Java, or JavaScript?
Python: Use @abstractmethod in a base class. Java: Use an abstract class with abstract createProduct(). JavaScript: Use a factory function or ES6 class with an overridable method.
51
How does the Factory Method Pattern relate to the Abstract Factory Pattern?
Abstract Factory is often implemented using Factory Methods. Each factory method in the Abstract Factory creates a specific product in the product family.
52
How does the Factory Method Pattern relate to the Template Method Pattern?
Factory Methods are often called within Template Methods. Example: The NewDocument() method in an application calls the Factory Method (CreateDocument()) as part of its workflow.
53
How does the Factory Method Pattern differ from the Prototype Pattern?
Factory Method requires subclassing to define which object to create. Prototype does not require subclassing and creates new objects by cloning an existing instance.