Software engineeringLesson 4 of 5 in the course Design patterns

Factory Method: Participants, Product Variation, and Trade-offs

5 flashcards · 5 quiz questions · 5 match pairs · 4 fill-in sentences · English

Lesson progress
0 / 5

Your progress is kept in this browser only.Sign up to save it

Summary

The factory method pattern is a creational design pattern for creating objects without making a class depend on the exact concrete class being instantiated. This lesson introduces the problem the pattern addresses and the roles that work together to solve it.

A class that directly calls a constructor, such as new Product1(), becomes coupled to that concrete product. If the required product changes, the class may need to be edited. Object creation can also involve complex steps, duplicated code, or information that does not belong in the class that uses the object.

All flashcards

What is the factory method pattern?
A creational pattern that uses a method to create a product while allowing the concrete product class to be selected by an implementation, commonly a creator subclass.
What problem does direct construction create?
When a class directly constructs a concrete class, it becomes coupled to that class. Changing the product may then require changing the class that uses it.
What is the role of the Product?
The Product is the shared interface or base class through which the creator works with the objects it needs.
What does a Concrete Creator do?
It implements or overrides the factory method to select and create a particular concrete product.
Why does the creator use a factory method?
The creator can use a product without being responsible for choosing its exact concrete class, allowing product creation to vary independently of product-using logic.

Quiz questions

  1. What kind of design pattern is the factory method pattern?Answer: A creational design pattern.
  2. What does a factory method allow a creator to avoid specifying directly?Answer: The exact concrete class of the product it needs.
  3. Which participant declares or provides the method used to create a product?Answer: The creator.
  4. How can two concrete creators provide different product types?Answer: Each implements the factory method to return its chosen product type.
  5. A class directly constructs `Product1`, and changing the desired product requires editing that class. Which design change reduces this dependency?Answer: Call a factory method and use the product abstraction.

Based on the Wikipedia article Factory method pattern, licensed CC BY-SA 4.0. Adapted into a summary, flashcards and quizzes.

All Software engineering lessons