如何在C#中使用泛型实现策略模式

时间:2018-01-22 02:47:36

标签: c# generics design-patterns strategy-pattern gang-of-four

我有一个字符串

Warning: Extension name 'word2vec_inner' does not match fully qualified name
'TWE-1.gensim.models.word2vec_inner' of 'word2vec_inner.pyx'
Compiling word2vec_inner.pyx because it changed.
[1/1] Cythonizing word2vec_inner.pyx

Error compiling Cython file:
------------------------------------------------------------
...
#!/usr/bin/env cython
^
------------------------------------------------------------

word2vec_inner.pyx:1:0: 'TWE-1.gensim.models.word2vec_inner' is not a valid 
module name
Traceback (most recent call last):
  File "D:\PyCharm 2017.3\helpers\pydev\pydevd.py", line 1668, in <module>
    main()
  File "D:\PyCharm 2017.3\helpers\pydev\pydevd.py", line 1662, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "D:\PyCharm 2017.3\helpers\pydev\pydevd.py", line 1072, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "D:/pycharm/TWE/TWE-1/gensim/models/setup.py", line 11, in <module>
    ext_modules=cythonize(extensions),
  File "E:\Python27\lib\site-packages\cython-0.27.3-py2.7-
win32.egg\Cython\Build\Dependencies.py", line 1039, in cythonize
    cythonize_one(*args)
  File "E:\Python27\lib\site-packages\cython-0.27.3-py2.7-
win32.egg\Cython\Build\Dependencies.py", line 1161, in cythonize_one
    raise CompileError(None, pyx_file)
Cython.Compiler.Errors.CompileError: word2vec_inner.pyx

和通用列表。

string partialCityName

我想做两件事:

1。根据名称过滤列表,我这样做:

var city = List<City>

2。根据上面的var“availableCities”获取字符列表,我这样做。

var availableCities = cityList.Where(f => f.CityName.StartsWith(partialCityName));

目前,这种情况正在一种方法中发生,并且正在发挥作用。但是我试图了解设计模式,并且正在尝试实施策略模式。

所以第一个问题是,这是正确使用的模式吗?如果是这样的话?

这是我到目前为止所得到的。

var nextAvailableCharacters = avalibleCities .
          Where(s => s.CityName.Length > partialCityName.Length).
          Select(s => s.CityName.Substring(partialCityName.Length)[0]).ToList();

然后:

public interface IFilterStrategy
{
   List<T> Filter<T>(List<T> filterThisData, string filter);
}

2 个答案:

答案 0 :(得分:2)

首先,您声明您的界面如下:

public interface IFilterStrategy<T>
{
    List<T> Filter(List<T> filterThisData, string filter);
}

然后你需要从这个接口的特定于类型的实例化继承:

public class FilterCityStrategy : IFilterStrategy<City>
{
    public List<City> Filter(List<City> filterThisData, string partialCityName)
    {
      return filterThisData.Where(f => f.CityName.StartsWith(partialCityName)).ToList<City>();
    }
}

请参阅https://repl.it/repls/UnluckyEqualRaptors

您获得的错误是因为Filter方法被声明为模板参数City的模板方法。这意味着City在此函数定义中充当任意类型,因此在代码的其他位置隐藏/屏蔽City类的声明:

    public List<City> Filter***<City>***(List<City> filterThisData, string partialCityName)
    {
        //Fails to compile cityName not member of f 
        return filterThisData.Where(f => f.CityName.StartsWith(partialCityName));
    } 

答案 1 :(得分:0)

您可以稍微扩展它以适应更复杂的过滤。例如,如果您有国家/地区过滤器和城市过滤器,则城市过滤器也可能具有国家/地区代码。

interface IFilter
{
}

class CountryFilter 
{
    string PartialCountryName { get; set;}
}

class CityFilter 
{
    string CountryCode { get; set;}
    string PartialCityName { get; set;}
}



interface IFilterStrategy<T,U> where U:IFilter
{
    List<T> Filter(List<T> source,U filter);
}

class CountryFilterStategy : IFilterStrategy<Country,CountryFilter>
{
    List<Country> Filter(List<Country> source,CountryFilter filter)
    {
        //logic
    }
}


class CityFilterStategy : IFilterStrategy<City,CityFilter>
{
    List<City> Filter(List<City> source,CityFilter filter)
    {
        //logic
    }
}