I have a class, it needs to process data differently based on user input.
There are two processor classes which both adhere to the same interface but behave slightly differently.
My IOC container is injecting an IThingerFactory to my Program instance.
An example with my current solution can be seen below.
Is there a better way to solve this problem?
public class Program
{
public IThingerFactory ThingerFactory { get; set; }
public Program(IThingerFactory thingerFactory)
{
ThingerFactory = thingerFactory;
}
public void FunctionWhichDoesStuff(int? input)
{
ThingerFactory.GetThinger(input).DoAThing();
}
}
public interface IThinger
{
void DoAThing();
}
public class DailyThinger : IThinger
{
public void DoAThing()
{
throw new NotImplementedException();
}
}
public class MonthlyThinger : IThinger
{
public MonthlyThinger(int monthNumber)
{
MonthNumber = monthNumber;
}
public int MonthNumber { get; set; }
public void DoAThing()
{
throw new NotImplementedException();
}
}
public interface IThingerFactory
{
IThinger GetThinger(int? number);
}
public class ThingerFactory : IThingerFactory
{
public IThinger GetThinger(int? number)
{
return number.HasValue ?
new MonthlyThinger(number.Value) as IThinger :
new DailyThinger() as IThinger;
}
}