1

The user of my library calls a generic method to retrieve resources from outside the application.

They receive these resources in form of a Resource instance for each resource. These instances can be any subtype of Resource; this allows storing images in RAM differently from sounds, etc...

The issue is this runs behind the scenes, thus the user does nothing beyond calling getResource(String) method.

Possible ways to identify the resource-type:

  • instanceof operator (user action)
  • passing on a type enum on the resource (allows using switches, etc.)
  • having the user pass in the type they want the resource loaded (not applicable here)

Question: What other ways are there for me to tell the user what type his resource is?


Addendum: The correct resource type is determined by the code that loads the data from disc. If anyone cares, it first checks the filetype and then makes another check on the raw data looking for magic numbers and such. The user will e.g. store a .png in the container and receive a ready-loaded and compressed RGBA8 texture.

dimo414
  • 47,227
  • 18
  • 148
  • 244
dot_Sp0T
  • 389
  • 4
  • 26
  • 1
    object.getClass() will work? – Jos May 17 '16 at 11:05
  • See this link if helpful http://stackoverflow.com/questions/541749/how-to-determine-an-objects-class-in-java – Sanjit Kumar Mishra May 17 '16 at 11:19
  • You could return a tuple of result and type, but as others have noted, that's kind of redundant, as the object itself already contains information about its type. – tobias_k May 17 '16 at 11:20
  • Also, not entirely clear what you want: How can the method determine what type the user expects/needs, or how can the user determine what type the method returns, so he an treat it accordingly? – tobias_k May 17 '16 at 11:23
  • 1
    How about storing some metadata in your base class Resource? The metadata can be flyweight objects shared among resource instances of the same type. – shrewquest May 17 '16 at 11:24
  • @shrewquest yeah, e.g. using an enum :) – dot_Sp0T May 17 '16 at 11:25
  • @tobias_k added an addendum explaining this bit. TL;DR the engine determines the correct type of resource based on the bytedata – dot_Sp0T May 17 '16 at 11:30
  • 3
    What's the use case? - If I, as a 'user', want a sound resource I'll have you load a sound resource for me. If I want an image, go get me an image. And the other way around, if I know the ID of a resource, but I do not know the basic kind of resource, what can I sensibly do with the generic resource? Unless I'm creating some kind of media player to operate on video, images, sound, text, I think there's little use of the generic approach. – JimmyB May 17 '16 at 11:37
  • Put in another way, what do all resources have in common structurally that's worth sharing between all of them? – JimmyB May 17 '16 at 11:40
  • @JimmyB the thing itself is a gameengine project of mine, mainly for the purpose of finding solutions to different programming problems. In this case the resources can be preloaded at startup, thus the original gamecontainer/archive can be removed from the FS after loading again and the game/program only exists in RAM. So if I don't automatically convert the resource to the correct in-engine types I end up with about 1.6times the ram consumption which is something I am trying to avoid now, resp I'm looking to find ways to avoid this. doesn't mean I'll end up with it like that though – dot_Sp0T May 17 '16 at 11:44
  • 3
    Still, what's wrong with making the user express his intent? `ImageResource loadImage( long imageId )`, `SoundResource loadSound( long soundId )`? – JimmyB May 17 '16 at 11:49
  • @JimmyB absolutely nothing, but I'm still looking for other ways – dot_Sp0T May 17 '16 at 11:52
  • 2
    Oh, ok. - It will boil down to a combination of types and (meta-)data. Where you draw the line between the two is up to you to some extent. You can declare to return `Object` and have the user use `instanceof` or `getClass()`. Or you can return `ImageResource`, and allow the user to inquire further details if needed (e.g. `getPixelFormat()` or `getCompression()`). Or you can declare returns of `PNGImageResource`, `WavSoundResource`, `Mp3SoundResource` &c. so the user does not have to deal with meta-data at all. Choose your way. – JimmyB May 17 '16 at 11:58
  • @dot_Sp0T can you elaborate on your requirements for this "other way"? Like JimmyB said the caller either needs to know what type of resource their requesting (e.g. `"image.png"` is clearly an image) or they intentionally don't care and just want a `Resource` in which case you needn't care either. Having separate methods for each type of resource (`loadImage()`, `loadSound()`, etc.) is the standard solution. Using `instanceof` or enums are (unpleasant) workarounds. – dimo414 Jun 02 '16 at 13:40

0 Answers0