Real World Example
> Blacksmith makes weapons. Elves need elf weapons and orcs need Orc weapons. Summon the right type of blacksmith according to the customer at hand.
> Blacksmith manufactures weapons. Elves require Elvish weapons, orcs require Orcish weapons. Depending on the customer at hand the right type of blacksmith is summoned.
In plain words
> It provides a way to delegate instantiation logic to subclasses.
> It provides a-delegate the instantiation logic to child classes.
Wikipedia says
> in class-based programming, the factory method pattern is a creation pattern that uses factory methods to handle the problem of creating objects without specifying the exact category of objects that will be created.
This is done by invoking a factory method to create an object that is either specified in the interface or implemented by a subclass, implemented in a base class, and can be overridden by a derived class rather than by calling the constructor.
> in class-based programming, the factory method pattern was a creational pattern that uses factory methods to deal with The problem of creating objects without have to specify the exact class of the object, that would be created. This was done by creating objects by calling a factory method-either specified in an interface and implemented by child CLA SSEs, or implemented in a base class and optionally overridden by derived classes-rather than by calling a constructor.
Use the Factory Method pattern when
Use factory method mode:
* A class can ' t anticipate the class of objects it must create
* A class cannot predict the class of objects it must create
* A class wants its subclasses to specify the objects it creates
* A class that wants its subclasses to specify the objects it creates
* Classes delegate responsibility to one of several helper subclasses, and your want to localize the knowledge of which Hel Per subclass is the delegate
* Class will delegate responsibility to one of several helper subclasses, and you want to localize which helper subclasses are the knowledge of the delegate
Java design mode-Factory mode