Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Service provider framework in java

Tags:

java

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

like image 691
Akhil Gupta Avatar asked Dec 25 '22 00:12

Akhil Gupta


2 Answers

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.

like image 128
scottb Avatar answered Dec 26 '22 13:12

scottb


A factory method is generally going to be returning a class that implements an interface (or extends a class, same result either way). But the actual class to be returned may be defined dynamically. In that case, the class to be returned doesn't need to exist when the factory method is written.

May be easiest by example. The JDBC example is something like DriverManager - you provide a database connection URL. From that URL, the DriverManager needs to figure out which class to return. If you're using an Oracle database, it creates an Oracle driver class (which implements the JDBC APIs), even though DriverManager doesn't know anything about Oracle specifically.

As a general case, let's say our factory method returns interface AA. The implementation of the factory method might read a configuration file that defines the class to create, then use reflection to create that class at runtime. The defined class just needs to exist in the CLASSPATH. The factory method knows how to create it, but the implementation hadn't existed at the time the factory method was written.

There are lots of everyday examples where this sort of thing occurs: JDBC providers, custom logging appenders, XML providers.

like image 28
user1676075 Avatar answered Dec 26 '22 14:12

user1676075