Static Factory Method

preview_player
Показать описание
From Joshua Bloch's "Effective Java", this is Item 01: Consider static factory methods instead of constructors

A static factory method is an alternative to creating objects without invoking constructors directly. This technique should not be confused with the Factory Pattern from the GoF.

The purpose of the static factory method is the same as a class constructor: to provide instances of a class. It differs from constructors in that a class can have several static factory methods with different names that indicate purpose.

Advantages:
1) Methods have descriptive names.
2) They are not require to provide new instances every time they are invoked.
3) Can return an object of any sub-type of their return type.
4) The class of the returned object can vary from call to call as a function of the input.
5) The class of the returned object need not exist when the class containing the method is written.

Limitations:
1) Classes without public or protected constructors cannot be sub-classed.
2) Factory methods are hard for programmers to find (vs Constructors which are easy to find).
Рекомендации по теме
Комментарии
Автор

thanks you sir, u nailed it...because of final keyword in instances variables value is not changed each time instances creating

cool_samiranbiswas