Re: Switch statement alternative?

Tech-Archive recommends: Fix windows errors by optimizing your registry



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?

.



Relevant Pages

  • PRE-PEP: new Path class
    ... Should path be a subclass of str? ... be times a developer wants to use string methods, ... the most common operations should be supported directly. ... to divide (separate) things. ...
    (comp.lang.python)
  • Re: variable length fields for flexibility in subroutines
    ... private string _IntBlock; ... public string IBreturn ... public string IBstreet ...
    (comp.lang.cobol)
  • Re: Generics SortedList and DataSource
    ... /// Summary description for Query. ... string m_group; ... public Query(string Name, string Group, bool IsDirect) ... public string Email ...
    (microsoft.public.dotnet.languages.csharp)
  • Problems designing a web site template chooser
    ... I think this is because I need to separate the servlet into different ... String name; ... public String getServletInfo() ... Maybe you should split into a servlet and a JSP. ...
    (comp.lang.java.programmer)
  • Re: Generics SortedList and DataSource
    ... DataSource) and not generics SortedList? ... /// Summary description for Query. ... string m_group; ... public string Email ...
    (microsoft.public.dotnet.languages.csharp)