The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Custom IFormatter

I am designing part of a system that involves um.... "widgets".

Widgets can come in various file formats. Each file format has slight variations in data and vast differences in format (3 XML and 2 flat file formats).  I'm trying to develop a way to do conversions from one format to another.

One idea is to use the IFormatter interface. We could convert from ABC format to XYZ format like this:

AbcFormatter abc = new AbcFormatter();
Widget w = abc.Deserialize( inStream );

XyzFormatter xyz = new XyzFormatter();
xyz.Serialize( outStream, w );

The Widget class is a common runtime format that all these formatters would deserialize to. In other words, Deserialize always returns a Widget regardless of the  format in the inStream.

My hesitation is that this is an extreme specialization of the IFormatter interface. All of the framework implementations work with _any_ serializable object, but these would only work with Widgets.

What do you think? Would you do it differently?

Jeff Mastry Send private email
Friday, July 29, 2005
You need an internal format to convert to from. This is less work than writing conversion from any format to any format.

With 5 different formats you have, in the first case 10 transformers (5 to and 5 from) and in the second case you have 5 x 4 = 20 transformers.

Each new format would add 2 transformers in the first case and (N + 1) N - N (N - 1) = 2N transformers (10 for N = 5).

The design could be something like:

interface Document {
    String toInternalFormat();
    void parseInternalFormat(String value);

class DocumentTranslator {
    void convert(fromDocument, toDocument) {
Dino Send private email
Friday, July 29, 2005
Sorry I wasn't clear on that. The Widget class is the common format that all other formats Desrialize to.

I guess I should be asking myself if I really need the other pieces of the IFormatter interface. I could just have IWidgetFormatter if there's no need for the Binder, Context and SurrogateSelector properties.

Context would be useful since we save these to disk and transport them across the wire between web services.

I don't see much need for the other properties.

I guess IFormatter is more than I need. Funny how a few days off can change your perspective.
Jeff Mastry Send private email
Monday, August 01, 2005

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
Powered by FogBugz