If I've got code similar to this (although perhaps not as constrained as warning levels):
switch(item.StatusCode)
{
case StatusCode.SUCCESS:
CallSuccess(item);
break;
case StatusCode.WARNING:
CallWarning(item);
break;
case StatusCode.ERROR:
CallError(item);
break;
}
I often find myself converting it to something more like:
var behaviors = new Dictionary<StatusCode, Action<Item>>
{
{StatusCode.SUCCESS, (item) => CallSuccess(item)},
{StatusCode.WARNING, (item) => CallWarning(item)},
{StatusCode.ERROR, (item) => CallError(item)},
}
behaviors[item.StatusCode](item);
I find that in a lot of cases this makes for more maintainable code. Is this a pattern, does it actually help like I think it does, and what's its name?