Java Lazy Load Singleton in Abstract Class - java

I am trying to achieve Singleton Instance with Abstract class in Java. I have read like implementing it as a LazyLoad is the best practice. I could not make it as I am not much used to this pattern and even Java.
getInstance() is always null, I am not sure why?
I don't think this follows lazy loading as the instance is constructed.
Any additional comments are welcome apart from this as I am completely new to Java and worked on c# before.
Here is what I have,
Interface:
public interface IConditionAppender{
public String Append();
}
Abstract
public abstract AppenderBase {
private static IConditionAppender instance;
protected AppenderBase(IConditionAppender instance)
{
this.instance = instance;
}
public static IConditionAppender getInstance(){ return instance; }
}
Implementation
public final class AndAppender extends AppenderBase implements IConditionAppender {
private AndAppender()
{
super(new AndAppender())
}
#Override
public String Append()
{
return " AND ";
}
}
Test
System.out.println(AndAppender.getInstance().Append());

Here is how AndAppender can be implemented
public final class AndAppender implements ConditionAppender {
private static final AndAppender instance;
public static AndAppender getInstance() {
if (instance == null)
instance = new AndAppender();
return instance;
}
private AndAppender() { }
#Override
public String append() {
return " AND ";
}
}
The same approach for OrAppender etc.
Note: this implementation is not thread-safe.
A bit simpler way is to use Enum which is singleton by default and can implement interfaces.
public enum Appender implements ConditionAppender {
AND(" AND "), OR(" OR ");
final String operation;
Appender(String operation) {
this.operation = operation;
}
#Override
public String append() {
return operation;
}
public static void main(String[] args) {
System.out.println(AND.append());
System.out.println(OR.append());
}
}

The code below may help you~
public abstract class AbstractSingleton {
private static Map<String, AbstractSingleton> registryMap = new HashMap<String, AbstractSingleton>();
AbstractSingleton() throws SingletonException {
String clazzName = this.getClass().getName();
if (registryMap.containsKey(clazzName)) {
throw new SingletonException("Cannot construct instance for class " + clazzName + ", since an instance already exists!");
} else {
synchronized (registryMap) {
if (registryMap.containsKey(clazzName)) {
throw new SingletonException("Cannot construct instance for class " + clazzName + ", since an instance already exists!");
} else {
registryMap.put(clazzName, this);
}
}
}
}
#SuppressWarnings("unchecked")
public static <T extends AbstractSingleton> T getInstance(final Class<T> clazz) throws InstantiationException, IllegalAccessException {
String clazzName = clazz.getName();
if (!registryMap.containsKey(clazzName)) {
synchronized (registryMap) {
if (!registryMap.containsKey(clazzName)) {
T instance = clazz.newInstance();
return instance;
}
}
}
return (T) registryMap.get(clazzName);
}
public static AbstractSingleton getInstance(final String clazzName)
throws ClassNotFoundException, InstantiationException, IllegalAccessException {
if (!registryMap.containsKey(clazzName)) {
Class<? extends AbstractSingleton> clazz = Class.forName(clazzName).asSubclass(AbstractSingleton.class);
synchronized (registryMap) {
if (!registryMap.containsKey(clazzName)) {
AbstractSingleton instance = clazz.newInstance();
return instance;
}
}
}
return registryMap.get(clazzName);
}
#SuppressWarnings("unchecked")
public static <T extends AbstractSingleton> T getInstance(final Class<T> clazz, Class<?>[] parameterTypes, Object[] initargs)
throws SecurityException, NoSuchMethodException, IllegalArgumentException,
InvocationTargetException, InstantiationException, IllegalAccessException {
String clazzName = clazz.getName();
if (!registryMap.containsKey(clazzName)) {
synchronized (registryMap) {
if (!registryMap.containsKey(clazzName)) {
Constructor<T> constructor = clazz.getConstructor(parameterTypes);
T instance = constructor.newInstance(initargs);
return instance;
}
}
}
return (T) registryMap.get(clazzName);
}
static class SingletonException extends Exception {
private static final long serialVersionUID = -8633183690442262445L;
private SingletonException(String message) {
super(message);
}
}
}
From: https://www.cnblogs.com/wang9192/p/3975748.html

Related

Java interfaces - prevent code repetition

I'm trying to create an interface for a set of classes,
but I am failing in finding a solution, which
prevents copying almost similar code for each new method in the
interface.
The interface
public interface P6BO<T extends BusinessObject> {
String[] getFields();
void create(T businessObject) throws P6BOException;
void delete(T businessObject) throws P6BOException;
}
30+ implementations of the interface.
public class ActivityBO implements P6BO<Activity> {
...
#Override
public void create(Activity activity) throws P6BOException {
...
}
#Override
public void delete(Activity activity) throws P6BOException {
...
}
}
Container class to initialize all the implementations and
provide a single point of access to each of them.
public class P6Bom {
public final ActivityBO activity = new ActivityBO();
public final EpsBO eps = new EpsBO();
public final ResourceBO resource = new ResourceBO();
public P6Bom(P6Info p6Info) throws P6BOException {
activity.activate(p6Info, p6Cache, p6Buffer);
eps.activate(p6Info, p6Cache, p6Buffer);
resource.activate(p6Info, p6Cache, p6Buffer);
}
...
public void create(BusinessObject businessObject) throws P6BOException {
if (businessObject instanceof Activity) {
activity.create((Activity) businessObject);
} else if (businessObject instanceof EPS) {
eps.create((EPS) businessObject);
} else if (businessObject instanceof Resource) {
resource.create((Resource) businessObject);
}
}
public void delete(BusinessObject businessObject) throws P6BOException {
if (businessObject instanceof Activity) {
activity.delete((Activity) businessObject);
} else if (businessObject instanceof EPS) {
eps.delete((EPS) businessObject);
} else if (businessObject instanceof Resource) {
resource.delete((Resource) businessObject);
}
}
public P6BO<? extends BusinessObject> getBO(BusinessObject businessObject) throws P6BOException {
if (businessObject instanceof Activity) {
return activity;
} else if (businessObject instanceof EPS) {
return eps;
} else if (businessObject instanceof Resource) {
return resource;
} else {
throw new P6BOException("not implemented.");
}
}
}
The test class
public class Test() {
/* Works: but is not generic (I cannot call the delete method for any BusinessObject). */
Activity activity = new Activity("MyNewActivity");
P6Bom.activity.create(activity);
P6Bom.activity.delete(activity);
/* Works: but results in a double administration in the P6Bom */
Activity activity = new Activity("MyNewActivity");
P6Bom.create(activity);
P6Bom.delete(activity);
/* Compiler error
The method delete(capture#1-of ? extends BusinessObject)
in the type P6BO<capture#1-of ? extends BusinessObject>
is not applicable for the arguments (Activity)
*/
p6Bom.getBO(activity).delete(activity);
}
What would be an elegant solution to prevent the repetitive administration of each method in the P6Bom interface?
You can use a map:
public class P6Bom {
public final Map<Class<? extends P6BO<? extends BusinessObject>>, P6BO<? extends BusinessObject>>> classMap;
public P6Bom(P6Info p6Info) throws P6BOException {
classMap = new HashMap<>();
classMap.put(ActivityBo.class, new ActivityBO());
...
for(var bo: classMap.values()) {
bo.activate(p6Info, p6Cache, p6Buffer);
}
}
...
public void create(BusinessObject businessObject) throws P6BOException {
getBO(businessObject).create(businessObject);
}
public P6BO<? extends BusinessObject> getBO(BusinessObject businessObject) throws P6BOException {
if (classMap.containsKey(businessObject.getClass())) {
return classMap.get(businessObject.getClass());
} else {
throw new P6BOException("not implemented.");
}
}
}
I forget if the <? extends BusinessObject> allows you to do that. If the compiler complains, omit that part in the classMap declaration and the getBO signature and ignore the warnings ;-)
The thing to remember here is that in the bytecode, the generics are removed, so the actual method signature allows you always to pass in BusinessObjects. Once inside the method, it will be cast and throw a RuntimeException if the class doesn't match, but from the compiler/interface side, it is perfectly possible to write code like that.
This seems like a classical factory and strategy pattern issue, where the Open/Closed principle is violated. You want to extend your application, but without modifying it.
#kutschkem's solution is so far the most elegant way. In addition I would suggest to instantiate the strategy/business object manager when the first business object is created.
This will free your hands from enumerating the BOM's, because the suggested way, the Open/Closed principle is still violated. Once a new business object is introduced, its respective manager needs to be added in the class.
The solution could be the following:
public class P6Bom {
private final Map<String, P6BO<? extends BusinessObject>> classMap;
private final P6Info info;
private final P6Cache cache;
private final P6Buffer buffer;
public P6Bom(P6Info info, P6Cache cache, P6Buffer buffer) {
this.classMap = new HashMap<>();
this.info = info;
this.cache = cache;
this.buffer = buffer;
}
public <T extends BusinessObject> void create(T businessObject) throws P6BOException {
this.getBO(businessObject).create(businessObject);
}
public <T extends BusinessObject> P6BO<T> getBO(T businessObject) throws P6BOException {
if (!this.classMap.containsKey(businessObject.getClass().getName())) {
try {
var bo = (P6BO<T>)Class.forName(businessObject.getClass().getPackageName() + ".bo." + businessObject.getClass().getSimpleName() + "BO").getConstructors()[0].newInstance();
bo.activate(this.info, this.cache, this.buffer)
this.classMap.put(
businessObject.getClass().getName(),
bo
);
} catch (Exception e) {
throw new P6BOException("not implemented.");
}
}
return (P6BO<T>)this.classMap.get(businessObject.getClass().getName());
}
}
Of course, for that solution there need to be a convention (or configuration) how strategies (e.g. ActivityBO) could be found for BusinessObjects. In the example, they live in a subpackage called bo.

Calling common methods without a common interface

I have some generated code (i.e. it cannot be changed) that looks something like this.
class Generated1 {
public String getA() {
return "1";
}
public void setB(String b) {
}
public void setC(String c) {
}
public void setD(String d) {
}
}
class Generated2 {
public String getA() {
return "2";
}
public void setB(String b) {
}
public void setC(String c) {
}
public void setD(String d) {
}
}
I am exploring these objects by reflection. None of them implement any common interface but there's many of them and I want to treat them as if they implement:
interface CommonInterface {
String getA();
void setB(String b);
void setC(String c);
void setD(String d);
}
It certainly should be possible. This is considered perfectly good code
class CommonInterface1 extends Generated1 implements CommonInterface {
// These are perfectly good classes.
}
class CommonInterface2 extends Generated2 implements CommonInterface {
// These are perfectly good classes.
}
I suppose what I'm looking for is something like:
private void doCommon(CommonInterface c) {
String a = c.getA();
c.setB(a);
c.setC(a);
c.setD(a);
}
private void test() {
// Simulate getting by reflection.
List<Object> objects = Arrays.asList(new Generated1(), new Generated2());
for (Object object : objects) {
// What is the simplest way to call `doCommon` with object here?
doCommon(object);
}
}
My question: How do I treat an object that doesn't implement an interface but actually has all the code to do so as if it does implement the interface.
I want to replace
private void doCommon(Generated1 c) {
String a = c.getA();
c.setB(a);
c.setC(a);
c.setD(a);
}
private void doCommon(Generated2 c) {
String a = c.getA();
c.setB(a);
c.setC(a);
c.setD(a);
}
...
with
private void doCommon(CommonInterface c) {
String a = c.getA();
c.setB(a);
c.setC(a);
c.setD(a);
}
I know I can use a Proxy like this but I'd really prefer to use something better.
private void test() {
// Simulate getting by reflection.
List<Object> objects = Arrays.asList(new Generated1(), new Generated2());
for (Object object : objects) {
// What is the simplest way to call `doCommon` with object here?
doCommon(adapt(object));
}
}
private CommonInterface adapt(Object o) {
return adapt(o, CommonInterface.class);
}
public static <T> T adapt(final Object adaptee,
final Class<T>... interfaceToImplement) {
return (T) Proxy.newProxyInstance(
adaptee.getClass().getClassLoader(),
interfaceToImplement,
// Call the equivalent method from the adaptee.
(proxy, method, args) -> adaptee.getClass()
.getMethod(method.getName(), method.getParameterTypes())
.invoke(adaptee, args));
}
If you're using reflection, you don't need the two CommonInterfaceX classes, you can use a proxy implementing CommonInterface:
public class Wrapper implements InvocationHandler {
private final Object delegate;
public static <T> T wrap(Object obj, Class<T> intf) {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
Object proxy = Proxy.newProxyInstance(cl, new Class<?>[] {intf},
new Wrapper(obj));
return intf.cast(proxy);
}
private Wrapper(Object delegate) {
this.delegate = delegate;
}
#Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Method dmethod = delegate.getClass().getMethod(
method.getName(), method.getParameterTypes());
return dmethod.invoke(delegate, args);
}
}
You can use this class as follows:
List<Object> objects = Arrays.asList(new Generated1(), new Generated2());
for (Object object : objects) {
CommonInterface proxy = Wrapper.wrap(object, CommonInterface.class);
doCommon(proxy);
}
UPDATE: note that the same Wrapper class works with any interface.
There's no way to achieve a static type relationship between Generated1 and Generated2.
Even if you created CommonInterface1 and CommonInterface2, you still wouldn't be able to statically use a Generated1 object as a CommonInterface1 because new Generated1() is not a CommonInterface1 (and will never become one)
By far the simplest solution is to change your code generation to add the CommonInterface to Generated1 and Generated2.
If that's absolutely impossible, the only other way to avoid this code duplication is to go for reflection.
You can do it manuallly by reflection.
public class Generated {
public String getA() {
return "A";
}
public String sayHello(String name) {
return "hello " + name;
}
}
public class Helper {
private static final String METHOD_NAME = "getA";
private static final String METHOD_WITH_PARAM_NAME = "sayHello";
public static void main(String[] args) throws Exception {
Generated generated = new Generated();
accessMethod(generated);
accessMethodWithParameter(generated);
}
private static void accessMethod(Generated g) throws Exception {
Method[] methods = g.getClass().getDeclaredMethods();
for(Method method : methods) {
if(isCommonMethod(method)) {
String result = (String) method.invoke(g);
System.out.println(METHOD_NAME + "() = " + result);
}
}
}
private static boolean isCommonMethod(Method m) {
return m.getName().equals(METHOD_NAME) && m.getReturnType().equals(String.class);
}
private static void accessMethodWithParameter(Generated g) throws Exception {
Method[] methods = g.getClass().getDeclaredMethods();
for(Method method : methods) {
if(isCommonMethodWithParameter(method)) {
String result = (String) method.invoke(g, "Max");
System.out.println(METHOD_WITH_PARAM_NAME + "(\"Max\") = " + result);
}
}
}
private static boolean isCommonMethodWithParameter(Method m) {
return m.getName().equals(METHOD_WITH_PARAM_NAME) &&
m.getReturnType().equals(String.class) &&
m.getParameterTypes().length == 1 &&
m.getParameterTypes()[0].equals(String.class);
}
}
Output is
getA() = A
sayHello("Max") = hello Max
If you want to replace as your comment. I think you can do it easily
First, you create interface CommonInterface
interface CommonInterface {
String getA();
void setB(String b);
void setC(String c);
void setD(String d);
}
After that, you create 2 class Generated1 and Generated2 inherited CommonInterface
class Generated1 implements CommonInterface {
#overide
public String getA() {
return "1";
}
#overide
public void setB(String b) {
}
#overide
public void setC(String c) {
}
#overide
public void setD(String d) {
}
}
class Generated2 implements CommonInterface {
#overide
public String getA() {
return "2";
}
#overide
public void setB(String b) {
}
#overide
public void setC(String c) {
}
#overide
public void setD(String d) {
}
}

How can I instantiate an object in jUnit, that needs to reflectively create another object inside its constructor?

I'm trying to write a junit test for one of my classes. The design was not done by me; this is a fairly old application, java7, struts1, and clydeDB framework.
The classes are set up like this:
ProcessObj,
IProcessObj (interface),
ProcessHome,
public class ProcessHome {
private static ProcessHome instance = new ProcessHome();
//default Constructor
private ProcessHome() {
}
public static ProcessHome getInstance() {
return instance;
}
public IProcessObj getProcessObj() throws POException {
return ProcessObj.getInstance(); //this is below
}
}
public class ProcessObj implements IProcessObj {
// instance
private static IProcessObj instance;
...
//constuctor
private ProcessObj() throws POException {
init();
}
static IProcessObj getInstance() throws POException {
if (instance == null) {
instance = new ProcessObj();
}
return instance;
}
//jUnit test setUp
#Before
public void setUp() throws Exception {
public static IProcessObj iPO;
iPAO = ProcessHome.getInstance()
.ProcessObj();
Constructor<ProcessObj> pa = ProcessObj.class
.getDeclaredConstructor();
pa.setAccessible(true);
iPO = pa.newInstance();`
...
It works fine up to here, but then in the ProcessObj, the initialization method goes through another set of classes that are set up exactly like the process objects that are above, for the data access layer.
Is there a way that I can create a usable instance of the process object? Can someone explain to me what exactly is going on here? I keep getting a InvocationTargetException.
Why you don't do something like this:
public class ProcessHome {
private static ProcessHome instance = new ProcessHome();
//default Constructor
private ProcessHome() {
}
public static ProcessHome getInstance() {
return instance;
}
public IProcessObj getProcessObj() throws POException {
return ProcessObj.getInstance(); //this is below
}
}
public class ProcessObj implements IProcessObj {
// instance
private static IProcessObj instance;
...
//constuctor
private ProcessObj() throws POException {
init();
}
static IProcessObj getInstance() throws POException {
if (instance == null) {
instance = new ProcessObj();
}
return instance;
}
//jUnit test class
public class ProcessHomeTest {
private IProcessObj iPO = ProcessHome.getInstance()
.ProcessObj();
#Test
public void testIProcessObj() throws Exception {
//use iPO heretest iPO
assertEquals("some","some");
}

Factory objects creator with generics

I want to do a factory pattern in java with generics. My code is:
The interface:
public abstract class Factory<T> {
public abstract T create();
}
FactoryA class:
public class FactoryA extends Factory<FactoryA> {
public FactoryA() {
}
public FactoryA create() {
return new FactoryA();
}
}
FactoryB class:
public class FactoryB extends Factory<FactoryB> {
public FactoryB() {
}
public FactoryB create() {
return new FactoryB();
}
}
The main class:
public class FactoryCreator {
public static <T> T createFactory() {
Factory<T> t = ?; // is that right way?
return t.create();
}
public static void main(String[] args) {
FactoryA factoryA = FactoryCreator.createFactory();
FactoryB factoryB = FactoryCreator.createFactory();
}
}
The question, what Factory t = need to be equal, or is there any other way?
Not really sure what you're trying to achieve, but this might help;
public interface Factory<T>
{
public T create(String type);
public T create(String type, Object arg);
public T create(String type, Object[] args);
}
And then have a class implement that factory interface, like this;
public class TemplateFactory<T> implements Factory {
#Override
public T create(String type) throws IllegalArgumentException
{
return create(type, null);
}
#Override
public T create(String type, Object arg) throws IllegalArgumentException
{
// Convert to array of 1 element
Object[] arguments = new Object[1];
arguments[0] = arg;
return create(type, arguments);
}
#Override
public T create(String type, Object[] args) throws IllegalArgumentException
{
// Create array for all the parameters
Class<?> params[] = (args != null) ? new Class<?>[args.length] : new Class<?>[0];
if(args != null)
{
// Adding the types of the arguments
for(int i = 0; i < args.length; ++i)
params[i] = (args[i] != null) ? args[i].getClass() : null;
}
try
{
// Create a class variable
Class classLoader = Class.forName(type);
// Find the right constructor
Constructor co;
if(params.length > 0)
co = classLoader.getConstructor(params);
else
co = classLoader.getConstructor();
// Instantiate the class with the given arguments
T newObject = (T)co.newInstance(args);
return newObject;
}
catch(Exception e)
{
throw new IllegalArgumentException(e.toString());
}
}
}
And then use it like this (using some imaginary strategy-classes as an example):
TemplateFactory<StrategyInterface> factory;
factory = new TemplateFactory<>();
factory.create("packageName.StrategyA");
factory.create("packageName.StrategyB");
factory.create("packageName.StrategyC");
The strategy classes (A, B and C) would implement the StrategyInterface-class in this example.
Something like this might work:
public static <T extends Factory> T createFactory(Class<T> clazz) {
try {
t = clazz.newInstance();
return t.create();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
...
FactoryA factoryA = FactoryCreator.createFactory(FactoryA.class);
alternatively, without parameters. But then you need two methods.
public static FactoryA createFactoryA() {
return new FactoryA().create();
}
...
FactoryA factoryA = FactoryCreator.createFactoryA();
Since the Generic types are erased at runtime you have to supply the Class parameter so that the runtime knows what class you are talking about.

java - reflection: How to Override private static abstract inner class method?

I have the following class:
class MyClass{
private static final int VERSION_VALUE = 8;
private static final String VERSION_KEY = "versionName";
public boolean myPublicMethod(String str) {
try {
return myPrivateMethod(str, VERSION_KEY, VERSION_VALUE,
new MyInnerClass() {
#Override
public InputStream loadResource(String name) {
//do something important
}
});
}
catch (Exception e) {
}
return false;
}
private boolean myPrivateMethod(String str, String key, int version,
ResourceLoader resourceLoader) throws Exception
{
//do something
}
private static abstract class MyInnerClass {
public abstract InputStream loadResource(String name);
}
}
I want to write unit test for myPrivateMethod for which I need to pass resourceLoader object and override it's loadResource method.
Here is my test method:
#Test
public void testMyPrivateMethod() throws Exception {
Class<?> cls = Class.forName("my.pack.MyClass$MyInnerClass");
Method method = cls.getDeclaredMethod("loadResource", String.class);
//create inner class instance and override method
Whitebox.invokeMethod(myClassObject, "testValue1", "testValue2", "name1", 10, innerClassObject);
}
Note, that I can't change code.
Well, you could use Javassist...
See this question. I haven't tried this, but you can call this method when you want the override:
public <T extends Object> T getOverride(Class<T> cls, MethodHandler handler) {
ProxyFactory factory = new ProxyFactory();
factory.setSuperclass(cls);
factory.setFilter(
new MethodFilter() {
#Override
public boolean isHandled(Method method) {
return Modifier.isAbstract(method.getModifiers());
}
}
);
return (T) factory.create(new Class<?>[0], new Object[0], handler);
}
Well, the problem i see with your code is that you are calling myPublicMethod and you are giving fourth parameter as new MyInnerClass(). Now in your private method fourth parameter is given as ResourceLoader and from your code i see no relation between MyInnerClass and ResourceLoader. So you can try out following code. It might help.
Despite your warning that you cannot change the code i have changed it because i was trying to run your code.
class MyClass{
private static final int VERSION_VALUE = 8;
private static final String VERSION_KEY = "versionName";
public boolean myPublicMethod(String str) {
try {
return myPrivateMethod(str, VERSION_KEY, VERSION_VALUE,
new MyInnerClass() {
#Override
public InputStream loadResource(String name) {
return null;
//do something important
}
});
}
catch (Exception e) {
}
return false;
}
private boolean myPrivateMethod(String str, String key, int version,
MyInnerClass resourceLoader) throws Exception
{
return false;
//do something
}
private static abstract class MyInnerClass {
public abstract InputStream loadResource(String name);
}
}
Hope it helps.

Categories

Resources