Re: Which GoF Design Pattern to choose?

From: Wavemaker (jabberdabber_at_BiteMeHotmail.com)
Date: 02/06/04


Date: Fri, 06 Feb 2004 19:14:59 GMT


"Timo Nentwig" wrote:
> Rod Davison wrote:
>
> > document [] d = new document[3];
> > d[0] = bbcFeed.getDocument();
> > d[1] = cnnFeed.getDocument();
> > d[2] = reuterFeed.getDocument();
> > for (int = 0; i < 3; i++) d[i].parse();
> >
> > But you get the idea. BY instantiating the right
> > kind of document object when the text appears, you
> > eliminate coding switch logic later.
>
> I can't do the above as I don't know the type of the
> document without prior determination. I only do have
> a list of url and the fetched html code, so I need to
> find out what kind of document I do have by looking at
> the url. And hence I do have some kind of switch()ing.
>
> if (url.startsWith("http://www.cnn.com") d[i] = cnnFeed.get...
> else
> if ...

Using a switch here may be unavoidable. You are at the boundary of
your application in which you are dealing with raw data. When you
receive the data, it has to be interpretted and transformed into
objects; you have to have some way of determining what the data
represents, hence the switch statement. Once you instantiate the
right kind of objects, you can let polymorphism take over for you.

It might work something like this:

Document doc;

switch(identifySourceOf(text))
{
case CNN:
    doc = new CNNDocument(text);
    break;

case BBC:
    doc = new BBCDocument(text);
    break;

// etc...
}

// Somewhere else...
doc.Parse();

Where CNNDocument and BBCDocument are derived from Document and
implement their own parsing strategy. You may want to use other
approaches in how you design your classes, but don't worry about
having to use a switch at some point. Since you are dealing with raw
data, you really don't have a choice.



Relevant Pages

  • Re: use of unassigned local variable
    ... The issue is the compiler doesn't know whether any of the cases will have resulted in the creation of a Foo to assign to teh reference so it is uninitialized as far as the compiler is concerned. ... I am instantiating a class in a switch statement as there are a number ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: use of unassigned local variable
    ... "Mike P" wrote in message ... > I am instantiating a class in a switch statement as there are a number ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Problematic code in K&R
    ... in K&R the authors published the following code for dealing with ... command line arguments: ... switch { ... argc = 0; ...
    (comp.lang.c)
  • Re: Oh $#!@. I think I got screwed on eBay again.
    ... >> I believe Germany uses 50 Hz. ... >> Might be a switch to change it. ... > Dealing with motors, that doesn't matter much. ... Prev by Date: ...
    (rec.crafts.metalworking)
  • Re: Jargons of Info Tech industry
    ... >dealing with spam, and we've discussed these issues at great ... is figuring out how to get people to switch over. ... Canadian Mind Products, Roedy Green. ...
    (comp.unix.programmer)