我想注入构造函数参数 IActionLogger actionLogger,但希望其他参数 largeBucket、smallBucket 和 amountToRetrieve 是上下文相关的(不确定这是否是正确的术语)。
问题:
我是否应该将这些构造函数参数设为自动属性并将 IActionLogger actionLogger 参数保留在构造函数中?
基本上,计算会根据 largeBucket、smallBucket 和 amountToRetrieve 变量而有所不同?我将这些变量放在构造函数中,因为我需要事先进行一些设置。
public class BucketActionsHandler : IBucketActionsHandler
{
private List<IAction> _actions = new List<IAction>();
private Bucket _largeBucket;
private Bucket _smallBucket;
private IActionLogger _actionLogger;
private int _amountToRetrieve;
public BucketActionsHandler(Bucket largeBucket, Bucket smallBucket, int amountToRetrieve, IActionLogger actionLogger)
{
_largeBucket = largeBucket;
_smallBucket = smallBucket;
_amountToRetrieve = amountToRetrieve;
_actionLogger = actionLogger;
_actions.Add(new LastAction(largeBucket, smallBucket, amountToRetrieve));
_actions.Add(new EmptySmallerBucketAction(largeBucket, smallBucket, amountToRetrieve));
_actions.Add(new EmptyLargeBucketAction(largeBucket, smallBucket, amountToRetrieve));
_actions.Add(new FillLargeBucketAction(largeBucket, smallBucket, amountToRetrieve));
_actions.Add(new FillSmallBucketAction(largeBucket, smallBucket, amountToRetrieve));
_actions.Add(new TransferToLargeBucketAction(largeBucket, smallBucket, amountToRetrieve));
_actions.Add(new TransferToSmallBucketAction(largeBucket, smallBucket, amountToRetrieve));
}
private IAction GetNextAction()
{
foreach (var action in _actions)
{
if (action.SatisfiedCondition())
{
return action;
}
}
return null;
}
public void CalculateSteps()
{
IAction nextAction;
do
{
nextAction = GetNextAction();
nextAction.Execute();
if (nextAction == null)
{
throw new InvalidOperationException("No valid action available");
}
} while(!(nextAction is LastAction));
}
}