What specific problem does the Factory pattern solve that plain object construction does not?
It centralizes the decision of which concrete class to instantiate into exactly one place, instead of letting that decision be duplicated across every piece of client code that needs an object. Without a factory, if ten different features in an application each need to construct a Car or a Bike depending on some condition, that same decision logic is copy-pasted into ten places, and adding a new type means finding and updating every one of them. With a factory method, client code asks for what it wants, such as a car or a bike, and never writes new on a concrete class itself, so a new type can be added by touching only the factory and the new class, not any of the client code that requests objects.
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a response