This will not be an answer you will like to hear, but: don't.
By making that constructor private/internal/protected
, you are making it hard to test the algorithms, and you are also preventing any consumers of the API from manually choosing their own implementation instead of using the factory.
I would say leave the constructors public, just make sure every dependency required is declared in the constructor. Here's my take on the various modifiers on constructors:
public
- everyone can access your constructor, and you can test the class - this is good.
protected
- subclasses can access the constructor. This is OK, but is only really used for abstract classes and/or constructor chaining.
internal
- You're restricting the usage of the constructor to InternalsVisibleTo
(friend) assemblies. This means that anyone within your assembly can construct it normally, but other assemblies have to explicitly be either InternalsVisibleTo
or be forced to use your factory, thereby coupling the algorithm to the factory.
private
- Useful for hiding a default constructor (although you don't need to do this if you create a constructor with at least one argument), or creating chainable constructors only.
TL;DR - I would recommend against making the constructor internal
. By doing so you might as well make the class internal
(you are referencing the algorithms by their interface and not their class, right?)
Another solution would be to make the implementations of the classes private
and nest them inside of the Factory. Have them all implement a common interface (which they should be doing already) and expose the interface to the rest of the application. This still does not avoid the problem of making it hard to test.