A
Adi Schwarz
Hi,
I know that it is not possible to instanciate an interface. But are
there any workarounds?
I want to write a class that stores and manages certain objects. This
objects have to implement an interface called "SelfLoadable", that means
they can manage to load their information (from a file, database,
internet,...) themselves by providing a key that identifies the object
(e.g. primary key).
public interface SelfLoadable {
public abstract void load(Object key);
}
Now my main class named ObjectCache shall contain a method named "get".
It shall look up if this object is stored in the cache, if not it shall
create it and automatically load its data:
private Hashtable cache = new Hashtable();
public SelfLoadable get(Object key) {
SelfLoadable o;
o = (SelfLoadable)cache.get(key);
if (o == null) { //Object not stored?
o = new SelfLoadable();
o.load(key);
}
return o;
}
new SelfLoadable(); does not work since it is an interface. But how do I
create the object if it is not stored in cache?
The idea is that I do not have to care about the cache - the get method
shall always return the object I request, no matter if the object is
cached or not.
I hope I could reduce the problem so far that it is understandable.
greets,
-adi
I know that it is not possible to instanciate an interface. But are
there any workarounds?
I want to write a class that stores and manages certain objects. This
objects have to implement an interface called "SelfLoadable", that means
they can manage to load their information (from a file, database,
internet,...) themselves by providing a key that identifies the object
(e.g. primary key).
public interface SelfLoadable {
public abstract void load(Object key);
}
Now my main class named ObjectCache shall contain a method named "get".
It shall look up if this object is stored in the cache, if not it shall
create it and automatically load its data:
private Hashtable cache = new Hashtable();
public SelfLoadable get(Object key) {
SelfLoadable o;
o = (SelfLoadable)cache.get(key);
if (o == null) { //Object not stored?
o = new SelfLoadable();
o.load(key);
}
return o;
}
new SelfLoadable(); does not work since it is an interface. But how do I
create the object if it is not stored in cache?
The idea is that I do not have to care about the cache - the get method
shall always return the object I request, no matter if the object is
cached or not.
I hope I could reduce the problem so far that it is understandable.
greets,
-adi