public class ObjectFactory extends Object
Instead of invoking new MySomething(), devs can import ObjectFactoryUtil.*
statically and then invoke createObject(MySomething.class).
Thus allowing downstream projects to extend the system by providing a replacement-class. For example, if the
replacement-class MyOther was registered for MySomething, the method
createObject(MySomething.class) would return an instance of MyOther instead of
MySomething.
However, it is urgently recommended not to use this approach, whenever it is possible to use a better solution,
preferably a well-defined service (=> ServiceLoader). There are situations, e.g. data-model-classes
(a.k.a. entities), where services are not possible and the ObjectFactory is the perfect solution.
In order to register a sub-class as replacement for a certain base-class, implementors have to provide a
ClassExtension and register it using the ServiceLoader-mechanism.
Important: You should usually not need to access this class directly! Use ObjectFactoryUtil instead
(statically import its methods).
| Modifier | Constructor and Description |
|---|---|
protected |
ObjectFactory() |
| Modifier and Type | Method and Description |
|---|---|
<T> T |
createObject(Class<T> clazz) |
<T> T |
createObject(Class<T> clazz,
Class<?>[] parameterTypes,
Object... parameters) |
<T> T |
createObject(Class<T> clazz,
Object... parameters) |
<T> ClassExtension<T> |
getClassExtension(Class<T> clazz) |
<T> Class<? extends T> |
getExtendingClass(Class<T> clazz) |
static ObjectFactory |
getInstance()
Gets the singleton instance of this
ObjectFactory. |
protected ObjectFactory()
public static ObjectFactory getInstance()
ObjectFactory.
Important: You should normally not invoke this method directly, but use ObjectFactoryUtil
instead.
ObjectFactory instance; never null.public <T> Class<? extends T> getExtendingClass(Class<T> clazz)
public <T> ClassExtension<T> getClassExtension(Class<T> clazz)
public <T> T createObject(Class<T> clazz)
public <T> T createObject(Class<T> clazz, Object... parameters)
public <T> T createObject(Class<T> clazz, Class<?>[] parameterTypes, Object... parameters)
Copyright © 2013–2017. All rights reserved.