Re: Switch statement alternative?
- From: "Martin Z" <martin.zarate@xxxxxxxxx>
- Date: 17 Oct 2006 09:47:33 -0700
Well, the agile, OOP way to do it would be to make each part type it's
own class, and then just have each class implement a "GetDescription"
method. Classes with common features get refactored into having common
parents, and such classes would generally have common descriptions, or
at least similar-enough descriptions that you could factor-out the
commonalities.
But that wouldn't acheive the goals you outlined - config-oriented
instead of recompile-oriented, and no searching for the description
strings if you want to change them all up. So here's my
non-polymorphic, config-oriented approach
So here's how I'd do it:
1) each object exposes a
Dictionary<string, string> GetBusinessFields()
method, thus implementing the ICanGetBusinessFields() interface.
2) Then, you have an XML-serializable class called
DescriptionFactory(), which is just a serliazed wrapper around a
Dictionary-based version of String.Format.... something like this (I
got carried away and did most of the implementation):
interface IDescribable
{
string DescriberName();
Dictionary <string, string> GetBusinessFields();
}
class Describer
{
[XmlAttribute()]
public string Name;
[XmlArrayItem("key", typeof(string))]
public string[] Keys;
public string Describe(string formatString, IDescribable obj)
{
Dictionary <string, string> fields =
obj.GetBusinessFields();
string[] formatValues = new string[fields.Count];
int i=0;
foreach (string key in Keys)
{
formatValues[i] = fields[key];
i++;
}
return string.Format(formatString, formatValues);
}
}
class DescriberManager
{
public Describer[] Describers;
public string FormatString;
private Dictionary<string, Describer> dict = null;
//The meat of the function. Call this to get description from
object.
public string GetDescription(IDescribable obj)
{
//lazily construct Description dictionary
if (dict == null)
{
foreach (Describer desc in Describers)
{
dict[desc.Name] = desc;
}
}
return dict[obj.DescriberName()].Describe(FormatString,
obj);
}
}
The descriptionmanager and it's child descriptions are serialized out
of your configuration system. IDescribable is implemented by your part
type(s). Of course, this is offered with no warranty - I haven't even
included an exception handler (or TryGetValue) for the very real
possibility of a KeyNotFoundException.
Phuff wrote:
Hey all, I need some direction help. I have a switch case statement
that is seemingly my only option right now, but its too large and not
easy to maintain the code. Here goes...
I have part descriptions (ie. 3/8" X ____" NYLON ALL-THREAD RODS...or
____" x ____" X ____" ____ WRAPPED MULLION) that I need to replace the
blank lines on. I do a switch on the part id.
I have 53 in all out of the parts list. What fills the blank is
dependent on information specific to the customized product being
built. Getting the information is not a problem...the problem is that
I cannot do any generalization. Almost every part is different and
what information the blank is based on is different for each part. I
have no generalized function aside from the one that fills the blank.
I just do a switch statement and have specialized business logic code
for each part.
I'm sure you can see the problem...what happens if we change a part
description in the system, or add a new part, or remove one? I have to
change the code. This is not the best practice, but I see no
alternative at the moment. Everything is so freaking customizable
here! Please help, any ideas?
.
- References:
- Switch statement alternative?
- From: Phuff
- Switch statement alternative?
- Prev by Date: formclosing and mybase.closing
- Next by Date: Re: Switch statement alternative?
- Previous by thread: Re: Switch statement alternative?
- Next by thread: VS.net Debug abilty to break at point when value is what i need
- Index(es):
Relevant Pages
|