vendredi 19 avril 2019

Handle numerous micro branching in multiple classes

I have a service that handles different client types; there are a lot of service classes that differ in minute ways in the way they process different clients. For the most part these classes contain logic that is common to all clients, however there are places with logic to specific clients.

I understand I could use a Factory to return a set of handlers for client specific logic, or use the Template method to have concrete implementations of an abstract class for different clients -- the problem with these approaches is that there are a lot of minute branching decisions that have to be made based on the client throughout the code, and these minute branching decisions are trivial, unconnected to each other, and they don't warrant separate classes. Is there a design pattern to elegantly implement this?

class UserService {
   void process() {
     //....
     if (client1) doStuff1();
     if (client2) doStuff2();
     //....
     if (client1) name = "xyz";
     else if (client2) name = "abc";
     //....
     if (client1) sortasc();
     else  sortdesc();
     //....
   }
}

Aucun commentaire:

Enregistrer un commentaire