lundi 14 août 2017

How to better initialize objects in factory?

I have following case: there is a factory which returns instances of Function interface:

public class RatingTableRowModifierFactory
{
  @Autowired
  private PlanSpecificBenefitsRatingTableRowModifier planSpecificBenefitsRatingTableRowModifier;
  @Autowired
  private CompositeBenefitsRatingTableRowModifier compositeBenefitsRatingTableRowModifier;
  @Autowired
  private RafRatingTableRowModifier rafTableRowModifier;

  public Function<RowBean, RowBean> getRowModifierInstance(.... input)
  {
    switch (ratingType)
    {
      case AGE:
        return planSpecificBenefitsRatingTableRowModifier;
      case COMPOSITE_AGE:
        return planSpecificBenefitsRatingTableRowModifier
          .andThen(compositeBenefitsRatingTableRowModifier)
          .andThen(rafTableRowModifier);
      default:
        return new EmptyRatingTableRowModifier();
    }
  }
}

The main issue in my approach is that each specific instance of RowModifier has to accept some additional parameters in order to correctly execute apply method. For example I need to pass algorithm id parameter into the planSpecificBenefitsRatingTableRowModifier and quote id into the rafTableRowModifier. I can't add these arguments inside the RowBean object and the only place where I can add them is getRowModifierInstance method. Please let me know if it's ok or not? As for me, it looks bad because if number of modifiers will grow then number of method parameters might also grow which is bad from my opinion... Please help me to understand how to avoid that?

Thanks in advance,

Aucun commentaire:

Enregistrer un commentaire