Service provider framework in java
java
Solution
As per the book "Effecive java" what does the statement "The class of the object returned by a static factory method need not even exist at the time the class containing the method is written." means in following paragraph:
A good way to explain what is meant by this sentence is to consider the `EnumSet` type, which is a class in the `java.util` package.
`EnumSet` is an abstract class without any accessible constructors. In order to get an `EnumSet` instance, the programmer uses one of its static factory methods, eg. `EnumSet.of( ... )`. For example:
Set<MyEnum> s = EnumSet.of(MyEnum.FIRST_CONSTANT);
The object returned by the `of()` method does not have an implementation type of `EnumSet`. Rather, the implementation type depends on the `MyEnum` class. If `MyEnum` has 64 or fewer constants, then the type of the object returned by `of()` is `RegularEnumSet`. If `MyClass` has more than 64 constants, the type of the object returned is `JumboEnumSet`. The actual type of the object returned, however, is of no interest to the programmer. All he or she cares about is getting some type of object that adheres to the `EnumSet` contract.
Now, let's say that, oh, five years later the Java language architects decide that it would be important to have another `EnumSet` implementation type, say, just for example, a cached enum implementation for classes with very large numbers of constants (more than 1024). They write this class to follow the `EnumSet` contract and call it:
final CachedEnumSet extends EnumSet {
:
:
}
Even though `CachedEnumSet` didn't exist when `EnumSet` was written, the fact that the use of static factories enabled a contract-based implementation system enabled the Java architects to add this new implementation years later.
Now, when a client invokes `EnumSet.of()`, he or she could get a `RegularEnumSet` object, a `JumboEnumSet` object, or the new `CachedEnumSet` object, but they wouldn't care because the object they get is still a subtype of `EnumSet` and is governed by its contract.
Problem
As per the book "Effecive java" what does the statement "The class of the object returned by a static factory method need not even exist at the time the class containing the method is written." means in following paragraph: The class of the object returned by a static factory method need not even exist at the time the class containing the method is written. Such flexible static factory methods form the basis of service provider frameworks, such as the Java Database Connectivity API (JDBC). A service provider framework is a system in which multiple service providers implement a service, and the system makes the implementations available to its clients, decoupling them from the implementations