javaspring-bootdesign-patternssingle-responsibility-principle

How to design Class which uses multiple service classes?


I am working on a springboot project where I am working on a feature which require multiple steps (almost like a algorithm). To follow the SRP I have made a separate service class to take care of each step in the algorithm. Now I have situation where my main service class is using up almost 9 other services.

How can I design my classes more better ? I know class which take more than few other service class are violating SRP.

@Service public AlgoProcessor {
// inject service1
// inject service 2
// inject service 3

public FinalReponse startprocess(){
  service1.algoStep1();
  service2.algoStep2();
// more steps
}

}

Im new in learning design principles, any feedback would be greatly appreciated !


Solution

  • In case you are in a similar situation and looking for any ideas then here's what I did- user template pattern for the steps in your algorithm. Try to group steps that are related and put them in another service. Probably not the most elegant way but it's better than what I had before. Closing this thread here.