samedi 23 septembre 2017

Whats the right design pattern for the following code?

I have following classes:

class ServiceA{
    User getUser(){} // API call to userServiceA.com
    Profile getProfile(){} // API call to profileServiceA.com
}

class ServiceB{
    User getUser(){} // API call to userServiceB.com
    Profile getProfile(){} // API call to profileServiceB.com
}

class GroupService(){
    ServiceA serviceA;
    ServiceB serviceB;
    constructor(){
        this.serviceA = new ServiceA();
        this.serviceB = new ServiceB();
    }

    getUser(String type){
        if(type.equals("A")){
            serviceA.getUser();
        }else if(type.equals("B")){
            serviceB.getUser();
        }
    }
}

class Controller(){
    get(RC routingContext){
        String type = routingContext.getParam("type");
        GroupService groupService = new GroupService();
        groupService.getUser(type);
    }
}

In this project ServiceC,D,E... will be kept adding, and that will turn the GroupService class into a chaos.

For this particular scenario, which is the correct design pattern I can apply? Factory pattern is one solution, but then if else or Map will still exist.

Aucun commentaire:

Enregistrer un commentaire