Avoiding duplicate service calls in java

Viewed 111

I have got the below service classes:

AccountService class:

public class AccountService {
    public createAccount(argumentsList1);
    public closeAccount(argumentsList2);
    //many other methods
}

ServiceHandler class:

public class ServiceHandler {//ServiceHandler is singleton

    SavingsAccountService savingsAccountService;//SavingsAccountService implements AccountService

    LoanAccountService loanAccountService;//LoanAccountService implements AccountService

    public AccountService getService(boolean isSavings) {
        if(isSavings) {
            return savingsAccountService;
        } else {
            return loanAccountService;
        }
    }
}

Also, I have got many different types of services like ABCService, XYZService, etc.. which invoke ServiceHandler first to get the runtime object of AccountService and then invoke the respective method as shown in the below example:

ABCService class:

public class ABCService {
    public void process1(boolean isSavingsAccount, a, b, c, arguments..) {
        AccountService accountService = ServiceHandler.getService(isSavingsAccount);
        accountService.createAccount(argumentsList1);
    }
}

XYZService class:

public class XYZService {
    public void process2(boolean isSavingsAccount, d, e, f, argumemts...) {
        AccountService accountService = ServiceHandler.getService(isSavingsAccount);
        accountService.closeAccount(argumentsList2);
    }
}

There are many other services like PQRService (similar to ABCService, XYZService), etc.. Here, I did not like duplicating the call to the ServiceHandler.getService(isSavingsAccount) in all other services (like ABCService, etc..) first to get the handle and then further invoking the required methods createAccount(), closeAccount(), etc.. How can I expose the methods of AccountService through ServiceHandler itself (so that I can call createAccount() with a single line of code)? Note that ServiceHandler is a singleton class and should be threadsafe.

Is there any specific design pattern that I can use to eliminate the above duplication?

1 Answers

Just apply the Dependency Injection principle

  • Make all your services depend on supertype AccountService
  • Compute getService(isSavings) only once to determine the required concrete AccountService eithier SavingsAccountService or LoanAccountService at runtime
  • Inject this into your services

Sample code for more clarification

ABCService class:

public class ABCService {
    // inject the service using constructor or setter method
    private AccountService accountService;

    public void process1(a, b, c, arguments..) {
        accountService.createAccount(argumentsList1);
    }
}

XYZService class:

public class XYZService {
    // inject the service using constructor or setter method
    private AccountService accountService;

    public void process1(d, e, f, argumemts...) {
        accountService.closeAccount(argumentsList1);
    }
}

Hint:
If you need the client to pass isSavings boolean parameter each time at runtime, so your solution fit the requirrment
Mainly you applied Factory method Design pattern, just rename ServiceHandler class to AccountServiceFactory for more readability

Related