Pergunta de entrevista da empresa Sonar

What is the factory pattern and builder pattern? When would you use 1 over the other?

Resposta da entrevista

Sigiloso

8 de set. de 2025

The factory pattern hides object creation behind a method that decides which concrete class to instantiate, making it useful when you want to abstract away complex logic or vary the type returned at runtime. The builder pattern separates object construction from its representation, allowing you to create complex objects step by step with clear control over optional and required parts. Use a factory when the concern is managing and reusing instances, and use a builder when the challenge is how to assemble a complex object with many dynamic configurations.