jeudi 16 avril 2020

Is there a pattern for having a differing number of Input/Output, but the same core responsibility of work?

There is a pattern in my codebase that looks a lot like this.

public interface Processor<T> {
  T process(T thing);
}

We had a recent use case come up where we now need different inputs and outputs. So something like:

public interface NewProcessor<I, O> {
  O process(I thing;)
}

and in the future will likely need something like

public interface FutureProcessor<I, O1, O2> { //potentially N number of O
  Pair<O1, O2> process(I thing);
}

My question is: Is there a way to express this cleaner than having three separate classes? Is there a nice known hierarchy I could use here?

We have an abstract user of the first type of processor that I would prefer to not have to re-write every time we add a new processor. It does something like this today:

public abstract AbstractModule<T> {
  private Processor<T> processor;
  public AbstractModule(Processor<T> processor) {
   this.processor = processor;
  }

  T runModule(T input) {
    // abstract validateInput(input);
    T result = processor.process();
    // record results
    return result;
  }
}

Any known patterns or suggestions on how to do this would be appreciated!

Aucun commentaire:

Enregistrer un commentaire