C
chris.senior
Most Java programmers are familiar with what I have called the "service
provider" in Java (if there is a better, more widespread name - please
tell me). In the pattern we want to discover an implementation of an
interface at runtime without caring where the implementation comes
from. For example:
public interface MyService {
public void myMethod();
}
public class MyServiceImpl {
public void myMethod() {
System.out.println("Do stuff...");
}
}
public class MyServiceFactory {
public static MyService getImpl() {
// look up class by some mechanism (e.g. system property)
Class c = Class.forName(System.getProperty("MyService.impl"));
return (MyService)c.newInstance();
}
}
public void someClientMethod() {
MyService service = MyServiceFactory.getImpl();
service.myMethod();
}
This is a pretty typical example (Log4J does something like this) but
there are lots of ever so tiny variations on the pattern littered
through the VM code and common libraries and frameworks. At the end of
this post is a shortlist of place's I have seen this pattern - I'd be
interested if anyone has seen this (or something like it) anywhere
else? Inside and outside the VM?
- Chris
* Log4J's LogFactory - Uses system property (last time I looked)
* java.beans.PropertyEditorManager - Also uses a name based search path
to look for implementations
* java.beans.BeanInfo - Uses a purely name based pattern to locate the
implementation
* JAR file service provider -
http://java.sun.com/j2se/1.4.2/docs/guide/jar/jar.html#Service Provider
(not sure who uses this?)
* URL handlers/connections - looks in certain packages (system
property) for implementations, using the package name (e.g. https) as
part of the search.
http://java.sun.com/j2se/1.4.2/docs...ing, java.lang.String, int, java.lang.String)
* Eclipse executable extension points -
http://wiki.eclipse.org/index.php/FAQ_What_are_extensions_and_extension_points?
That's all I could think of for now...
provider" in Java (if there is a better, more widespread name - please
tell me). In the pattern we want to discover an implementation of an
interface at runtime without caring where the implementation comes
from. For example:
public interface MyService {
public void myMethod();
}
public class MyServiceImpl {
public void myMethod() {
System.out.println("Do stuff...");
}
}
public class MyServiceFactory {
public static MyService getImpl() {
// look up class by some mechanism (e.g. system property)
Class c = Class.forName(System.getProperty("MyService.impl"));
return (MyService)c.newInstance();
}
}
public void someClientMethod() {
MyService service = MyServiceFactory.getImpl();
service.myMethod();
}
This is a pretty typical example (Log4J does something like this) but
there are lots of ever so tiny variations on the pattern littered
through the VM code and common libraries and frameworks. At the end of
this post is a shortlist of place's I have seen this pattern - I'd be
interested if anyone has seen this (or something like it) anywhere
else? Inside and outside the VM?
- Chris
* Log4J's LogFactory - Uses system property (last time I looked)
* java.beans.PropertyEditorManager - Also uses a name based search path
to look for implementations
* java.beans.BeanInfo - Uses a purely name based pattern to locate the
implementation
* JAR file service provider -
http://java.sun.com/j2se/1.4.2/docs/guide/jar/jar.html#Service Provider
(not sure who uses this?)
* URL handlers/connections - looks in certain packages (system
property) for implementations, using the package name (e.g. https) as
part of the search.
http://java.sun.com/j2se/1.4.2/docs...ing, java.lang.String, int, java.lang.String)
* Eclipse executable extension points -
http://wiki.eclipse.org/index.php/FAQ_What_are_extensions_and_extension_points?
That's all I could think of for now...