Creating an object from from an ID (or name)

前端 未结 5 1248
借酒劲吻你
借酒劲吻你 2020-12-21 10:09

I have an abstract class that a lot of child classes inherit:

  public abstract class CrawlerBase
    {
        public abstract void Process(string url);
            


        
5条回答
  •  再見小時候
    2020-12-21 10:43

    Make dictionary with factory methods:

    var factories = new Dictionary>
    {
      {"Trials", () => new Trials()},
      {"Coverage", () => new Coverage()},
    // and so on
    }
    

    ...and throw away switch:

    foreach (var item in result)
    {
      factories[item.Type]().Process(item.URL);
    }
    

提交回复
热议问题