Factory method is just a fancy name for a method that instantiates objects. Like a factory, the job of the factory method is to create -- or manufacture -- objects.
The factory pattern (also known as the factory method pattern) is a creational design pattern.
A factory is a Java class that is used to encapsulate object creation code.
A factory class instantiates and returns a particular type of object based on data passed to the factory.
The different types of objects that are returned from a factory typically are subclasses of a common parent class.
The data passed from the calling code to the factory can be passed either when the factory is created or when the method on the factory is called to create an object. This creational method is often called something such as getInstance or getClass .
centralize creation of objects
In simple words, if we have a super class and n sub-classes, and based on data provided, we have to return the object of one of the sub-classes, we use a factory pattern.
No comments:
Post a Comment