-1

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.

Joshua
  • 2,275
  • 7
  • 41
  • 57
  • `remove the switch statement` what exactly do you want, then? You clearly want a factory pattern, but you have to do some sort of test on your input to know which instance you want. You could use reflection, but i wouldn't argue that it is better in your case until you have a reason to use it. – Jonesopolis Mar 30 '16 at 19:51
  • 1
    You could use a dictionary instead of a switch statement, that might make the code a bit cleaner. See https://gist.github.com/jcebuck/6d57e70761ebabf1a3aa83fb27af077a – James Buck Mar 30 '16 at 20:03
  • @james Buck thanks this is much cleaner. – Joshua Mar 30 '16 at 20:33

3 Answers3

0

Use a Factory that would return an instance of IEvent from a string, then call HandlEvent on it. However, that basically means you are moving the switch in the factory.

Siderite Zackwehdex
  • 6,293
  • 3
  • 30
  • 46
  • Yeah but I wanna do it without the switch. – Joshua Mar 30 '16 at 19:48
  • Well, you have to make a decision sooner or later, however your code would abstract away your decision. If the strings were actual type names you could use some code to get the type based on name and then get an instance of it using Activator. – Siderite Zackwehdex Mar 30 '16 at 19:51
0

You could use reflection to instantiate the type specified by the name/identifier you have (possibly with an additional abstraction through an attribute on the types to map names).

Lucero
  • 59,176
  • 9
  • 122
  • 152
0

You can do something like this:

    public void Run()
    {
        List<string> classList = new List<string>
        {
            "EventOne",
            "EventTwo"
        };

        string assemblyName = Assembly.GetExecutingAssembly().GetName().Name;

        foreach (string item in classList)
        {
            IEvent ent = Activator.CreateInstance(Type.GetType(assemblyName + "." + item)) as IEvent;
            if (ent != null)
              ent.HandlEvent();
        }
    }
IProgrammer
  • 321
  • 1
  • 2
  • 8