mardi 3 juillet 2018

C# How to have one strategy in strategy pattern return more than one value

I have a strategy pattern implemented. One of the strategies is supposed to return 2 values while rest of them returns 1 value. I am currently using out param in the interface method for the sake of it being available for one strategy that sets this value and making it null for all other strategies as in the below example

I would like to know if there is a better way of having that one strategy return multiple values without making all other strategies deal with this out param(even though they are all going to set it null) Eg:

public interface IContributionStrategy
{
    decimal (decimal configuredAmt, out decimal? recurrent);
}
public class Strategy1 : IContributionStrategy
{
  public Strategy1 (int param1)
  {
    this.Param = param1;
  }
  public decimal Calculate(decimal amt, out decimal? recurrent)
  {
     recurrent = somevalue;
     //some logic that uses amt and this.Param and performs calc
     return calculatedLogic;
  }
public class Strategy2 : IContributionStrategy
{
  public decimal Calculate(decimal amt, out decimal? recurrent)
  {
     recurrent = null; //having to set this to null only because its in the Calculate() in the interface and this strategy is not using the recurrent value.
     //some logic that uses amt and performs calc
     return calculatedLogic;
  }
}

Aucun commentaire:

Enregistrer un commentaire