Factory Pattern For Object Creation:
Factory Pattern is a type of Creational Design Pattern. It is a very famous design pattern for creating instance of class. In this pattern a common Factory method is used to Create the instance of the class hence hides all the creational logic from the client and hence creates a new object using a common interface.
Example of Java Library using Factory pattern:
List Collection.UnmodifiableList(List)
uses Factory Method
Benifits of Factory Pattern:
1. Complete Control over instance Creation.
2. Naming for what you are creating objects.
3. Solves the problem that a class can have only one constructor with a given signature.
4. Unlike constructors, factory methods are not required to create a new instance every time they are invoked.
Disadvantage of Factory Pattern:
1. Cannot Directly access Constructor while inheriting.
Parametarized Factory Method:
Factory Pattern can Use Parameters to produce different implementation of an interface. With what parameters have been passed factory can implement logic for what method to call. As in case or Rule Factory we can define rules for different instance to be followed and while creating object different ruleSet can be checked for different object.
Factory Pattern is a type of Creational Design Pattern. It is a very famous design pattern for creating instance of class. In this pattern a common Factory method is used to Create the instance of the class hence hides all the creational logic from the client and hence creates a new object using a common interface.
Example of Java Library using Factory pattern:
List Collection.UnmodifiableList(List)
uses Factory Method
Benifits of Factory Pattern:
1. Complete Control over instance Creation.
2. Naming for what you are creating objects.
3. Solves the problem that a class can have only one constructor with a given signature.
4. Unlike constructors, factory methods are not required to create a new instance every time they are invoked.
Disadvantage of Factory Pattern:
1. Cannot Directly access Constructor while inheriting.
Parametarized Factory Method:
Factory Pattern can Use Parameters to produce different implementation of an interface. With what parameters have been passed factory can implement logic for what method to call. As in case or Rule Factory we can define rules for different instance to be followed and while creating object different ruleSet can be checked for different object.