instanciate an interface - alternatives?

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
 
B

BarryNL

Adi said:
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

What actually know the specific class to instantiate? Something needs to
know the real class name! You might try a Google on 'Factory Pattern'.
 
A

Andrew Hobbs

Adi Schwarz said:
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);
}

It is not a matter of workarounds. You do not seem to understand how and
why interfaces are used. I would suggest going back to your text book (and
if you do not have one then access 'Thinking in Java' by Bruce Eckel which
is available online).

Andrew


--
********************************************************
Andrew Hobbs PhD

MetaSense Pty Ltd - www.metasense.com.au
12 Ashover Grove
Carine W.A.
Australia 6020

61 8 9246 2026
metasens AntiSpam @iinet dot net dot au


*********************************************************
 
A

Adi Schwarz

What actually know the specific class to instantiate? Something needs to
know the real class name! You might try a Google on 'Factory Pattern'.

Thanks a lot for this hint. I don't think I need a Factory Pattern for
that, but now I am on the right way. I did not realize that the get
method has to know which type of SelfLoadable it has to create...

thx,

-as
 
A

Ashton

Adi Schwarz said:
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).

Here's a theory that I've used in the past and like.

Since a key identifies an object, a key should be able to recreate the
object if it's not in cache. Derived objects of the key type are able
to create corresponding derivations of the SelfLoadable type. Using that
approach, I'd restructure your code somewhat like:

private Hashtable cache = new Hashtable();

public SelfLoadable get(SelfLoadableKey key) {
SelfLoadable o;
o = (SelfLoadable)cache.get(key);

if (o == null) { //Object not stored?
o = key.produceObject();
cache.put(key, o);
}
return o;
}
 
T

Tor Iver Wilhelmsen

Adi Schwarz said:
I know that it is not possible to instanciate an interface. But are
there any workarounds?

Yes, use java.lang.reflect.Proxy.
o = new SelfLoadable();

Why not just

o = new SomeClassImplementingSelfLoadable();

or

o = (SelfLoadable) Class.forName(someClassNameString).newInstance();


for the dynamic case?
 
A

Adi Schwarz

Ashton said:
Here's a theory that I've used in the past and like.

Since a key identifies an object, a key should be able to recreate the
object if it's not in cache.

Since I want to stay as general as possible (with the cached objects as
well as with the used keys) I do not really like that suggestion - but
it is a very interesting approach.

My solution if you are interested: I have to tell the ObjectCache
instance anyway wich type of Objects it will cache - so I call the
Constructor with

new ObjectCache(new someSpecialKindOfObjectImpelmentingSelfLoadable);

The ObjectCache stores this (blank = unloaded) object as some kind of
pattern, and if it has to create a new obj it just clones this pattern
and calls the load(key) function.

Sometimes it is really good just to take a break and have a meal ...

greets,
-as
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top