I have a piece of logic that I need to execute either once or multiple times (in a loop) based on the type. Does a strategy pattern make sense here? In essence:
if (type == 1)
{
ProcessReport("");
}
else if (type == 2)
{
for (int i = 0; i < numUsers; i++)
{
ProcessReport(userId);
}
}
public void ProcessReport(string id)
{
if (id == "")
{
//Send full report
}
else
{
GetReportFragment();
//Send report
}
}
Well, since you obviously use a "type code" to distinguish different behaviors, you could start by replacing it with subclasses (polymorphism). That's usually the first thing to do when there is a type code based branching.
For simple problems, however, this might be an overkill. What's more objectionable with your code is:
"") to indicate specific behavior: at least create a separate method for a "full report", if you don't have an ID to specifyIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With