> For the complete documentation index, see [llms.txt](https://blog.sunilgudivada.dev/notebook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://blog.sunilgudivada.dev/notebook/design-patterns/creational-design-patterns/factory-method-design-pattern.md).

# Factory Method Design Pattern

## When to use it?

Use the Factory Method when&#x20;

:ledger:  you don’t know beforehand the exact types and dependencies of the objects your code should work with.

:ledger:  you want to save system resources by reusing existing objects instead of rebuilding them each time.

:ledger:  you want to provide users of your library or framework with a way to extend its internal components.

## Pros & Cons

:green\_circle: You avoid tight coupling between the creator and the concrete products.

:green\_circle: *Single Responsibility Principle*. You can move the product creation code into one place in the program, making the code easier to support.

:green\_circle: *Open/Closed Principle*. You can introduce new types of products into the program without breaking the existing client code.

:red\_circle: The code may become more complicated since you need to introduce a lot of new subclasses to implement the pattern. The best-case scenario is when you’re introducing the pattern into an existing hierarchy of creator classes.<br>

## Code

To be updated.

## References

* <https://refactoring.guru/design-patterns/factory-method>
* <https://www.youtube.com/watch?v=ub0DXaeV6hA&list=PLF206E906175C7E07&index=5>
*

<br>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://blog.sunilgudivada.dev/notebook/design-patterns/creational-design-patterns/factory-method-design-pattern.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
