This question already has an answer here:
I have a list of string which would have a different event type names. The code is given below,
public class Main
{
public void Run()
{
List<string> classList = new List<string>
{
"Event_one",
"Event_two"
};
foreach (string item in classList)
{
IEvent ent;
switch (item)
{
case "Event_one":
ent = new EventOne();
ent.HandlEvent();
break;
case "Event_two":
ent = new EventTwo();
ent.HandlEvent();
break;
}
}
}
}
public class EventOne : IEvent
{
public void HandlEvent()
{
throw new NotImplementedException();
}
}
public class EventTwo : IEvent
{
public void HandlEvent()
{
throw new NotImplementedException();
}
}
public interface IEvent
{
void HandlEvent();
}
How can I remove the switch statement and make the code more loosely coupled?. My code sits inside the website/app_code.
Aucun commentaire:
Enregistrer un commentaire