factory Method Flashcards
(53 cards)
What is the Factory Method Pattern?
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.
What is the main intent of the Factory Method Pattern?
Define an interface for creating an object.
Let subclasses decide which class to instantiate.
Defer instantiation to subclasses.
How does the Factory Method Pattern improve flexibility?
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 does the Factory Method Pattern work?
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.
What real-world analogy can explain the Factory Method Pattern?
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.
What are the components of the Factory Method Pattern?
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.
What is an abstract product in the Factory Method Pattern?
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.
What are concrete products in the Factory Method Pattern?
They are specific implementations of the abstract product.
Example: WordDocument, PDFDocument, HTMLDocument.
How does the Factory Method Pattern achieve decoupling?
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.
What are the key benefits of the Factory Method Pattern?
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.
What are the key takeaways of the Factory Method Pattern?
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.
What problem does the Factory Method Pattern solve in software design?
It delegates object creation to subclasses, avoiding hardcoded dependencies.
How does the Factory Method Pattern promote flexibility in object creation?
It allows new object types to be introduced without modifying existing code.
Why is the Factory Method Pattern classified as a creational design pattern?
Because it focuses on creating objects in a structured and flexible way.
How does the Factory Method Pattern help achieve loose coupling between classes?
It separates object creation logic from the client code, reducing dependencies.
What is the difference between the Factory Method Pattern and a simple factory?
The Factory Method allows subclasses to decide what to create, while a Simple Factory centralizes object creation.
Why does the Factory Method Pattern use inheritance to create objects instead of directly instantiating them in the base class?
To allow subclasses to define specific object creation without modifying the base class.
What would happen if an application did not use the Factory Method Pattern but instead directly instantiated objects in the base class?
The base class would be tightly coupled to specific object types, making it harder to extend.
How does the Factory Method Pattern help in maintaining the Open-Closed Principle?
New classes can be added without modifying existing code, keeping the system extendable.
What is the downside of not using the Factory Method Pattern when working with multiple object types?
The code would require modifications whenever a new type is introduced, violating extensibility.
Can the Factory Method Pattern be used with dependency injection? If so, how?
Yes, by injecting the factory into a class instead of hardcoding object creation.
What are the key components of the Factory Method Pattern?
Creator (Base Class), Concrete Creator (Subclass), Abstract Product, Concrete Product, and Factory Method.
What role does the creator class play in the Factory Method Pattern?
It defines the factory method but does not specify which product to create.
What role does the concrete creator play in the Factory Method Pattern?
It overrides the factory method to specify the exact product type to create.