samedi 29 mai 2021

What is the best approach to reuse similar method in multiple Services that use similar objects?

I have several services that have the same method that is called by a Scheduler. This is one example of one service.

@Service
public class MyService1 {

     @Autowired
     private MyLocalMapper1 localMapper1;
     
     @Autowired
     private MyLocalRepository1 localRepository1;

     @Autowired
     private MyExternalMapper1 externalMapper1;
     
     @Autowired
     private MyExternalRepository1 externalRepository1;
  

     public void startProcess() {
          //I use the mappers and the repositories in here
     }
}

I have 15 services exactly similars to this one but every service has a specific mappers (e.g. MyLocalMapper2, MyLocalMapper3, etc...) and also repositories.

e.g

 @Service
 public class MyService2 {

     @Autowired
     private MyLocalMapper2 localMapper2;
     
     @Autowired
     private MyLocalRepository2 localRepository2;

     @Autowired
     private MyExternalMapper2 externalMapper2;
     
     @Autowired
     private MyExternalRepository2 externalRepository2;
  

     public void startProcess() {
          //I use the mappers and the repositories in here
     }
}

Is there any design pattern which allows to reuse the code inside startProcess method taking into account that the objects it uses are different in every service?

I thought in creating interfaces for every object such as: LocalMapperInterface, LocalRepositoryInterface, etc.. and pass it to a single method with all those interfaces as parameters but not sure if that is the best approach.

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire