您好,我有一个关于以正确方式使用基因的问题。
请看我的代码,我需要一个建议,是否可以通过其他方式创建基本搜索。我想从 Conc2Search 和 Conc1Search 类中删除 ISearcher impementation。
class Program
{
static void Main(string[] args)
{
var res1 = SearchCreator.GetSearch(1).Search();
Console.WriteLine("GenetalSearch count:{0}", res1.Count());
var res2 = SearchCreator.GetSearch(2).Search();
Console.WriteLine("Conc1Search count:{0}", res2.Count());
var res3 = SearchCreator.GetSearch(3).Search();
Console.WriteLine("Conc2Search count:{0}", res3.Count());
}
public class SearchCreator
{
public static ISearcher<Base> GetSearch(int type)
{
switch (type)
{
case 1: return new GenetalSearch();
case 2: return new Conc1Search();
case 3: return new Conc2Search();
}
return null;
}
}
}
public class Base
{
}
public class Conc1 : Base
{
}
public class Conc2 : Base
{
}
public interface ISearcher<out T> where T : Base
{
IEnumerable<T> Search();
}
public class GenetalSearch : ISearcher<Base>
{
public IEnumerable<Base> Search()
{
var list = new List<Base> {new Base(), new Base(), new Base()};
return list;
}
}
public class Conc1Search : ISearcher<Conc1>, ISearcher<Base>
{
public IEnumerable<Conc1> Search()
{
var list = new List<Conc1>() { new Conc1(), new Conc1(), new Conc1(), new Conc1()};
return list;
}
IEnumerable<Base> ISearcher<Base>.Search()
{
return Search();
}
}
public class Conc2Search : ISearcher<Conc2>, ISearcher<Base>
{
public IEnumerable<Conc2> Search()
{
var list = new List<Conc2>() { new Conc2(), new Conc2(), new Conc2(), new Conc2(), new Conc2() };
return list;
}
IEnumerable<Base> ISearcher<Base>.Search()
{
return Search();
}
}