Recall & Review
beginner
What is the Generic Factory Pattern?
It is a design pattern that creates objects using a generic method or class, allowing you to produce different types of objects without specifying their exact classes upfront.
Click to reveal answer
beginner
How does a generic factory improve code reusability?
By using generics, the factory can create many types of objects with one method or class, reducing repeated code and making it easier to add new types later.
Click to reveal answer
intermediate
TypeScript syntax: How do you declare a generic factory function?
You use angle brackets with a type parameter, like <T>, before the function parameters. Example:
function createInstance<T>(c: new () => T): T { return new c(); }Click to reveal answer
intermediate
What role does the 'new () => T' type play in a generic factory?
It tells TypeScript that the argument is a constructor function that can create an instance of type T without any parameters.
Click to reveal answer
beginner
Why use a generic factory instead of creating objects directly?
It separates object creation from usage, making code easier to maintain and extend. You can create new types without changing the factory logic.
Click to reveal answer
What does the generic type parameter <T> represent in a generic factory?
✗ Incorrect
The generic type represents the type of object the factory will create, allowing flexibility.
In TypeScript, what does 'new () => T' mean in a factory function parameter?
✗ Incorrect
'new () => T' means a constructor function that takes no arguments and returns an instance of type T.
Which benefit does a generic factory pattern provide?
✗ Incorrect
Generic factories allow creating many object types with one reusable factory method.
What is a key advantage of separating object creation using a factory?
✗ Incorrect
Separating creation makes it easier to update or extend object creation without changing the rest of the code.
How do you call a generic factory function to create an instance of class Car?
✗ Incorrect
You pass the class Car as an argument and specify the generic type: createInstance(Car).
Explain how a generic factory pattern works in TypeScript and why it is useful.
Think about how one function can create many types of objects.
You got /5 concepts.
Describe the TypeScript syntax needed to define a generic factory function that creates instances of any class.
Focus on the function signature and how it uses the constructor.
You got /4 concepts.