java design question: dispatch on subformat?

T

tobin fricke

I am implementing a class whose job it is to read data from a variety
of external formats into a common internal format (represented by this
class). For instance, we can pretend that I'm talking about images --
this class would read JPEG, TIFF, and PNG images into an internal
structure with the uncompressed bitmap, etc. I'm looking for a clever
way to structure this. A "design pattern" perhaps.

I'm a C programmer at heart, and my first instinct is to dispatch on
the data format. In C I would do something like this:

void read_image(char *blob) {
Image *image = calloc(sizeof(Image)); // internal representation

format = get_format(blob);

dispatch_table[format](blob,internal_format);

}

void JpegUnstuffFcn(char *src_blob, Image *dst_image) {
// unstuff the JPEG blob into the internal format
}

void main(void) {
register_format("JPG",JpegUnstuffFcn);
register_format("PNG",PngUnstuffFcn);
...
}

Now in Java I can imagine several approaches. I would have some kind
of interface or super-class that included an Unstuff method. Then
there would be in the Image class a table of known Unstuffers, and the
readImageFromBlob constructor would dispatch on data format to one of
these Unstuffers. Somethign like this:

interface ImageUnstuffer {
virtual void Unstuff(char blob[]);
}

class Image {
Image(char blob[]) {
knownUnstuffers.get(getFormatStr(blob)).
}
static void registerUnstuffer(String fmt, ImageUnstuffer handler) {
knownStuffers.put(fmt,handler);
}
static Dictionary knownUnstuffers;
}

My main question is, where is the best place to call
Image.registerUnstuffer? Any tips on how best to implement something
like this?

thanks,
Tobin Fricke
fricke at gmail dot com
 
T

Tobin Fricke

My main question is, where is the best place to call
Image.registerUnstuffer?

I could state this question succinctly as, ``How do the dispatcher and the
handlers find each other?''

--tobin
 
P

Phillip Mills

Now in Java I can imagine several approaches. I would have some kind
of interface or super-class that included an Unstuff method. Then
there would be in the Image class a table of known Unstuffers, and the
readImageFromBlob constructor would dispatch on data format to one of
these Unstuffers.

It's fairly common to break this up into a set of classes with separate
responsibilities.

One of those would be a UnstufferFactory that's responsible for
returning an appropriate subclass of ImageUnstuffer, given whatever
criteria lets it decide. The UnstufferFactory would probably be created
as a singleton (pattern) and would either have internal knowledge of
ImageUnstuffer types or would load ones specified in a property file.

So, I guess I'm saying that I wouldn't have a registerUnstuffer; the
UnstufferFactory would know/learn what existed when it was created.

Another thing is, it's likely an Image class would have enough
responsibilities without being concerned about how its data got loaded.
I'd likely have ImageUnstuffer.unstuff return an Image rather than
having the Image know anything about the existence of "blob".
 

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

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,709
Latest member
AustinMudi

Latest Threads

Top