instanciate an interface - alternatives?

From: Adi Schwarz (adolf.schwarz.news.01-04_at_gmx.at)
Date: 01/29/04


Date: Thu, 29 Jan 2004 14:57:59 +0100

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



Relevant Pages

  • Re: kobj multiple inheritance
    ... The kobj lookup scheme doesn't require any kind of locked memory ... >> This proposed scheme also traverses through base classes of base classes ... >> Interface lookup using current kobj can be done expliticly. ... The cache is large even ...
    (freebsd-arch)
  • Re: instanciate an interface - alternatives?
    ... > I know that it is not possible to instanciate an interface. ... > It shall look up if this object is stored in the cache, ... > create it and automatically load its data: ...
    (comp.lang.java.programmer)
  • Re: [PATCH 08/28] SECURITY: Allow kernel services to override LSM settings for task actions [try #2]
    ... interface, at the granularity you want to support distinctions to be made. ... context and begin caching". ... Do any of the interfaces allow a task to act on a cache other than one ...
    (Linux-Kernel)
  • Generics mit "Zirkulärer Typreferenz"
    ... generischen Cache speichern. ... Interface implementieren: ... internal interface ICacheablewhere T: ... Über den Key wird das Objekt identifiziert, ...
    (microsoft.public.de.german.entwickler.dotnet.csharp)
  • Re: instanciate an interface - alternatives?
    ... > I know that it is not possible to instanciate an interface. ... It is not a matter of workarounds. ... > 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; ...
    (comp.lang.java.programmer)