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
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