Re: How to approach constructing an instance of an unknown class?
- From: "K Viltersten" <tmp1@xxxxxxxxxxxxxx>
- Date: Thu, 4 Sep 2008 13:26:47 +0200
I have a class that reads an XML and
according to its contents, it creates a
List<Letter>, where Letter is an abstract
class that is implemented in Alpha and
Beta classes.
I use a foreach statement consisting of a
switch upon which i create an instance of
e.g. Alpha an add it to the list.
The only limitation is that if i ever
invent a new implementation, say Gamma,
i'll be forced to rewrite, recompile and
reinstall the executable.
I'm looking for a suggestion on how to
approach the problem so that a new type
will be handled appropriately (provided
that i distribute SOME definition of the
appropriate actions to be taken).
Sounds like a classic case for the abstract factory pattern. For instance,
let's say that you decide on the class to use depending on the name of the
XML element that you encounter. Then, you abstract away the logic of
creating a letter of specific type from an XML node as a delegate (or an
interface), and introduce a registry (could be one, could be several) for
known implementations of that interface/delegate.
delegate Letter LetterFactory(XElement element);
Dictionary<XName, LetterFactory> Factories;
List<Letter> ParseLetters(XDocument document)
{
XElement current;
...
var factory = Factories[current.Name];
var letter = factory(current);
...
}
...
Alpha AlphaLetterFactory(XElement element)
{
Alpha result = ...; // create and fill from XML
return result;
}
Factories.Add("alpha", AlphaLetterFactory);
...
Beta BetaLetterFactory(XElement element)
{
Beta result = ...; // create and fill from XML
return result;
}
Factories.Add("beta", BetaLetterFactory);
...
XDocument doc = ...;
ParseLetters(doc);
Oh, i think i see the approach. Only one
question, regarding the var keyword. Is
it allowed even in DotNet 2.0? I fear
that it's a newer thing, like 3.0 or 3.5
and such great utilities i can't use. :(
--
Regards
Konrad Viltersten
----------------------------------------
May all spammers die an agonizing death;
have no burial places; their souls be
chased by demons in Gehenna from one room
to another for all eternity and beyond.
.
- Follow-Ups:
- Re: How to approach constructing an instance of an unknown class?
- From: Peter Morris
- Re: How to approach constructing an instance of an unknown class?
- From: Jon Skeet [C# MVP]
- Re: How to approach constructing an instance of an unknown class?
- From: Marc Gravell
- Re: How to approach constructing an instance of an unknown class?
- References:
- How to approach constructing an instance of an unknown class?
- From: K Viltersten
- Re: How to approach constructing an instance of an unknown class?
- From: Pavel Minaev
- How to approach constructing an instance of an unknown class?
- Prev by Date: Re: How to approach constructing an instance of an unknown class?
- Next by Date: Re: Generics, Interfaces, and Inheritance question
- Previous by thread: Re: How to approach constructing an instance of an unknown class?
- Next by thread: Re: How to approach constructing an instance of an unknown class?
- Index(es):
Relevant Pages
|