Effective Java: Item 01 Static Factory Method
Contents
A class can provide a public static factory method, which is simply a static method that returns an instance of the class.
Providing a static factory method instead of a public constructor has both advantages and disadvantages:
- One advantage of static factory methods is that, unlike constructors, they have names, it can highlight differences between overloaded constructors.
- A second advantage of static factory methods is that, unlike constructors, they are not required to create a new obj each time they’re invoked.
- A third advantage of static factory methods is that, unlike constructors, they can return an object of any subtype of their return type. (ref: EnumSet)
- A fourth advantage of static factory methods is that they reduce the verbosity of creating parameterized type instance.
- The main disadvantage of providing only static factory method is that classes without public or protected constructors cannot be subclassed.(favor composition over inheritance)
- A second disadvantage of static factory methods is that they are not readily distinguishable from other static methods (name convention).
Code Examples:
|
|