mardi 3 mai 2022

Generic interface has too many types, pattern to reduce?

I can't find the right pattern for this design.

I have an interface that has some methods. All of these methods need to be generic because these types can differ in every implementation of the interface. The purposes of these methods are the same but the return types and parameters are different. Imagine it like every service uses an adapter to handle the requests and responses. The methods in these adapters are the same but the types of requests and responses can vary.

The naive implementation can be found below. The main problem with it is that this interface uses way too many types. You could tell that I might break it down into smaller interfaces and smaller adapters but those would increase the number of dependencies where I would use these features.

What would be the ideal pattern to lower the number of generic types?

interface Adapter<A, B, C, D, E, F> {
  
  B methodA(A param);

  void methodB(B param);

  ...

  void methodF(F param);
}
class MyAdapter implements Adapter<TypeA, TypeB, TypeC, TypeD, TypeE, TypeF> {
  
  @Override
  public TypeB methodA(TypeA param) {
    return null;
  }

  ...

  @Override
  public void methodB(TypeB param) {
    ...
  }
}
class MyService {

  private MyAdapter myAdapter;
  
  ...
}

Aucun commentaire:

Enregistrer un commentaire