繁体   English   中英

将工厂注入课堂

[英]Injecting A Factory Into A Class

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

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)
{ . . . }

我觉得你很亲密。 考虑实现此接口的工厂:

interface IStringGeneratorFactory
{
    IStringGenerator CreateFixedLengthStringGenerator(int length);

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

    /* other types? */
}

您在MinMaxLengthStringGenerator_StringGenerator分配将变为:

_StringGenerator = _stringGeneratorFactory.CreateFixedLengthStringGenerator(_Length);

这将从MinMaxLengthStringGenerator删除对FixedLengthStringGenerator具体实现的MinMaxLengthStringGenerator

希望这可以帮助!

更新 ,然后设计就这样进行。

尽管应该避免在构造函数中注入参数。 现在,我们可以通过任何标准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,依赖注入需要按实例注册两个实现,以便它可以创建各自的实例。 如何使用构造函数注入来命名实例

关于DI的更多链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM