I am reading Joshua Blochs "Effective Java", and it says
A third advantage of static factory methods is that, unlike constructors, they can return an object of any subtype of their return type. This gives you great flexibility in choosing the class of the returned object. One application of this flexibility is that an API can return objects without making their classes public. Hiding implementation classes in this fashion leads to a very compact API. This technique lends itself to interface-based frameworks, where interfaces provide natural return types for static factory methods.
Can anyone explain what "natural return types are"? Thanks!
A return statement causes the program control to transfer back to the caller of a method. Every method in Java is declared with a return type and it is mandatory for all java methods. A return type may be a primitive type like int, float, double, a reference type or void type(returns nothing).
More specifically, covariant (wide to narrower) or contravariant (narrow to wider) return type refers to a situation where the return type of the overriding method is changed to a type related to (but different from) the return type of the original overridden method.
A method returns to the code that invoked it when it completes all the statements in the method, reaches a return statement, or throws an exception, whichever occurs first. You declare a method's return type in its method declaration. Within the body of the method, you use the return statement to return the value.
The getReturnType() method of Method classEvery Method has a return type whether it is void, int, double, string or any other datatype. The getReturnType() method of Method class returns a Class object that represent the return type, declared in method at time of creating the method.
In this context, "natural" simply means natural for the context of the factory method; i.e. what is appropriate or what you would expect. Intuitive would be another synonym.
This is just normal English usage ... not IT or Java-specific terminology.
A concrete example of this would be something like Guava's ImmutableList
factory methods.
This has 13 overloads of the of
method, with increasing numbers of parameters. If you look at the source of the first 3:
@SuppressWarnings("unchecked")
public static <E> ImmutableList<E> of() {
return (ImmutableList<E>) EMPTY;
}
public static <E> ImmutableList<E> of(E element) {
return new SingletonImmutableList<E>(element);
}
public static <E> ImmutableList<E> of(E e1, E e2) {
return construct(e1, e2);
}
The zero-arg and two-arg methods actually return an instance of RegularImmutableList
, which is a subclass of ImmutableList
; the one-arg method returns an instance of SingletonImmutableList
.
But is this detail about the subclass relevant to you, the caller? No. It is "natural" that if you call a method that constructs an ImmutableList
, you get back a reference to an ImmutableList
.
"Natural" in this sense perhaps means "at an appropriate level of abstraction".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With