I have a situation in a class tries to create a group of same type of components and if some component fails then it needs to decide whether it should continue or throw the exception. Now the behavior whether to continue or re-throw depends in the context in which that class is used.So a obvious solution was to pass the behavior from outside in a constructor, which is effectively a strategy pattern. For now I had two implementations:ContinuePolicyAbortPolicyThese both policies were stateless so I created a single instance for them. Now I needed a strategy called SingleSuccessPolicy ( which ignores exception if atleast one of the components is successfully created). Now the problem is that to know whether all are failing or there is at least one success, the strategy needs to store some state (possibly count of components to be created). The side effect of it is that it makes creating a single instance infeasible.Java Code:class GroupComponent {   private final ComponentExceptionPolicy exceptionPolicy;   public GroupComponent(ComponentExceptionPolicy policy, GroupData data) {      this.exceptionPolicy = policy;   }   public void createComponents() throws Exception {      for(ComponentData compData : data.getComponentsData()) {         try {            createComponent(compData)         } catch (Exception e) {           policy.apply(compData, e);         }      }   }}Code for Strategies:public interface ComponentExceptionPolicy {    public void apply(ComponentData data, Exception e);}class ContinuePolicy extends ComponentExceptionPolicy {    @Override    public void apply(ComponentData data, Exception e) throws Exception {      //just log it and continue    }}class AbortPolicy extends ComponentExceptionPolicy {    @Override    public void apply(ComponentData data, Exception e) throws Exception {       throw e;    }}class MaximumFailuresPolicy extends ComponentExceptionPolicy {   private int n; //the expected size of group component   MaximumFailuresPolicy(int n) {       this.n = n;   }   @Override   public void apply(ComponentData data, Exception e) {      //on each exception I decrement count      if(--n == 0) { //check if exception threshold is reached         //as soon as found that all have failed then exception is thrown,         //if any of component succeeds, then n will not reach to 0 and it         //will just continue         throw e;      } else {         //just log      }   }}EDIT:To clarify, MaximumFailuresPolicy is meant to check that atleast one of the component should be initialized successfully. Maybe I have named it incorrectlyIs it a proper way to achieve this? Any help is appreciated.
Statefull Strategy pattern
java;object oriented;design patterns