factory-pattern
Installation
SKILL.md
Factory Pattern
With the factory pattern we can use factory functions in order to create new objects. A function is a factory function when it returns a new object without the use of the new keyword!
Say that we need many users for our application. We can create new users with a firstName, lastName, and email property. The factory function adds a fullName property to the newly created object as well, which returns the firstName and the lastName.
When to Use
- Use this when you need to create multiple objects that share the same properties
- This is helpful when object creation depends on a certain environment or configuration
When NOT to Use
- For simple objects where a plain object literal suffices — a factory adds unnecessary indirection
- When class constructors are the established convention in your project and the team expects
new - When there's no conditional logic or configuration driving object creation