Re: Which GoF Design Pattern to choose?
From: Wavemaker (jabberdabber_at_BiteMeHotmail.com)
Date: 02/06/04
- Next message: Thomas Matthews: "Re: I only program the C64"
- Previous message: Timo Nentwig: "Re: Which GoF Design Pattern to choose?"
- In reply to: Timo Nentwig: "Re: Which GoF Design Pattern to choose?"
- Next in thread: Timo Nentwig: "Re: Which GoF Design Pattern to choose?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Next message: Thomas Matthews: "Re: I only program the C64"
- Previous message: Timo Nentwig: "Re: Which GoF Design Pattern to choose?"
- In reply to: Timo Nentwig: "Re: Which GoF Design Pattern to choose?"
- Next in thread: Timo Nentwig: "Re: Which GoF Design Pattern to choose?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|