I have a class that requires a way to retrieve a random integer value with a maximum. I don't want this class to depend on a specific way to retrieve that random value (such as system.random). Would it be best to:
(A) Use a public delegate (or func)
public delegate int NextRandomInt(int maxValue);
public class MyClass
{
public NextRandomInt NextRandomInt { get; set; }
public MyClass(NextRandomInt nextRandomInt)
{
NextRandomInt = nextRandomInt;
}
}
(B) Use a public interface
public interface IRandomIntProvider
{
int NextInt(int maxValue);
}
public class MyClass
{
public IRandomIntProvider RandomIntProvider { get; set; }
public MyClass(IRandomIntProvider randomIntProvider)
{
RandomIntProvider = randomIntProvider;
}
}
(C) Something else all together.
Both ways work. I feel like using a delegate would be simpler and quicker to implement, but the interface is more readable and may be easier when dependency injection comes around.