I need to do lazy initialization so I went for singleton/multiton pattern, but I needed a callback TOO, so I went for an abstract class, but that's a contradiction, it can't be at the same time a singleton and an abstract class.
Then, as solution for it could be implemented avoiding abstract class and using singleton and interfaces(to do the callbacks), passing them(interfaces) as parameters.. But that's kind of ugly, because forbids the beautiful IDE helps for implementing abstract methods.
I wonder if there is other choice for this impossible mix.
Option abstract class (prohibits single/multitons)
class abstract Machine
{
abstract hardwareCallBack();
//..
//Here I can't put a constructor for singleton because it's abstract
}
Option singleton(it's really a multiton) with interfaces
public class Machine {
private CallBackIface callBack; //interface including hardwareCallBack();
private static final Map<String, Machine> instances
= new HashMap<String, Machine>();
private Machine(CallBackIface callback0)
{
callBack=callback0;
}
public static Machine getMachine(String name, CallBackIface callback0)
{
Machine instance = instances.get(name);
if (instance == null) //plus sync stuff...
{
instance = new Machine(callback0);
instances.put(name, instance);
}
return instance;
}
//...
}
Here a related question How can I implement an abstract singleton class in Java?