将工厂注入课堂

时间:2018-07-26 20:01:48

标签: c# design-patterns dependency-injection factory

我正在开发一种用于生成某种字符串的应用程序功能。我有一个界面:

interface IStringGenerator
{
    string GenerateNext();
}

现在,我已经在具有单个构造函数和一个参数的类中实现了此接口:

class FixedLengthStringGenerator : IStringGenerator
{
    // The class needs some dependencies, but I ignored them here to keep it simple
    public FixedLengthStringGenerator(int length /*and dependencies*/)
    { . . . }

    public string GenerateNext()
    { . . . }

    .
    .
    .
}

此实现仅生成具有固定给定长度length的所需字符串。每次通话中的GenerateNext()返回所需的字符串,直到没有剩余的字符串,然后返回null。考虑到这些,我需要IStringGenerator的另一种实现,它可以生成长度在最小值和最大值之间的字符串。我认为拥有这样的东西很自然:

class MinMaxLengthStringGenerator : IStringGenerator
{
    int _MinLength;
    int _MaxLength;
    int _Length;
    IStringGenerator _StringGenerator;

    public MinMaxLengthStringGenerator(int minLength, int maxLength)
    {
        _MinLength = minLength;
        _MaxLength = maxLength;

        _Length = minLength;
        _StringGenerator = new FixedLengthStringGenerator(_Length);
    }

    public string GenerateNext()
    {
        if(_Length > _MaxLength)
            return null;

        string generatedString = _StringGenerator.GenerateNext();

        if(generatedString == null)
        {
            _Length++;
            if(_Length <= _MaxLength)
            {
                _StringGenerator = new FixedLengthStringGenerator(_Length);
                return _StringGenerator.GenerateNext();
            }
            else
                return null;
        }
        else
            return generatedString;
    }
}

但是直接创建实例不是一个好主意。相反,我可以使用工厂来获取FixedLengthStringGenerator的实例。但是我仍然认为这不是一个好习惯,因为它取决于FixedLengthStringGenerator。而且,如果将来我想使用其他替代类,则无法从外部接收它。

我的问题是(从模式的角度来看)将工厂注入我的MinMaxLengthStringGenerator是否正确?

更精确地考虑

interface IFixedLengthStringGeneratorFactory
{
    FixedLengthStringGenerator Create(int length);
}

我应该像下面这样声明MinMaxLengthStringGenerator的构造函数吗?

public MinMaxLengthStringGenerator(int minLength, int maxLength, IFixedLengthStringGeneratorFactory factory)
{ . . . }

2 个答案:

答案 0 :(得分:0)

我认为你很亲近。考虑实现此接口的工厂:

interface IStringGeneratorFactory
{
    IStringGenerator CreateFixedLengthStringGenerator(int length);

    IStringGenerator CreateMinMaxLengthStringGenerator(int minLength, int maxLength, IStringGeneratorFactory factory);

    /* other types? */
}

您在_StringGenerator中对MinMaxLengthStringGenerator的分配变为:

_StringGenerator = _stringGeneratorFactory.CreateFixedLengthStringGenerator(_Length);

这将从FixedLengthStringGenerator中删除对您的MinMaxLengthStringGenerator具体实现的引用。

希望这会有所帮助!

答案 1 :(得分:0)

已更新 所以设计就这样了。

尽管应该避免在构造函数中注入参数。 现在,我们可以通过任何标准的DI容器注入依赖项,即Unity,AutoFac,Ninject

interface IStringGenerator
{
    string GenerateNext();
}
class FixStringGenerator:IStringGenerator
{
  public FixStringGenerator(int length)
  {
   ...
  }
  public string GenerateNext()
  {
      ///logic
  }
...
}

对于下面的MinMax字符串生成器类,注入FixStringGenerator

class MinMaxStringGenerator:IStringGenerator
{
    ....
     IStringGenerator _fixStringGenerator;
     public MinMaxStringGenerator(int minLength,int maxLength, IStringGenerator fixStringGenerator)
     {
        _fixStringGenerator=fixStringGenerator;
     }
    string GenerateNext()
    {
     /// do the things with fix generator "_fixStringGenerator"
    }
    ....
 }

@Mostafa,依赖项注入需要按实例注册两个实现,以便可以创建各自的实例。 HOW TO DI named instance with constructor injection

有关DI another link

的更多信息