we have some API which will be called either by client A, B, C or D
Current code
doSomething(String client){
if (client.equals("A")){
...
}
else if (client.equals("B")){
...
}
Proposed refactoring 1
separate into multiple methods and have each client call the dedicated method such as
doSomethingForClientA()
doSomethingForClientB()
we need this because internally each method will call other private methods defined in the same class
Proposed refactoring 2
Use strategy (or template) pattern and separate into multiple classes for the clients to call
the method call from client remains
doSomething()
Which approach is better in the long run? Is there any design pattern to help with option 1 ?
or a 3rd option?
Proposed refactoring 2 is better in the long run.
This will allow you to extend additional functionality for additional (unforeseen ) client types later, without modifying tested parts of code, for already existing clients.
1
This is really good question with presented analysis!
If you look for a maintainable and extensible code
, of course use the option 2!.
It is one of the examples where Dependency Inversion Principle is applied.
As a reference i would suggest to look at S.O.L.I.D.principles. In addition, there is a good reply on somewhat similar question – Programming SOLID Principles
[extending my previous comment]
You might move these special codes to wrapper classes assigned to the client types, create them when the client enters, and use their functions.
interface ClientWrapper {
void doSomething();
}
class ClientWrapperForA implements ClientWrapper {
void doSomething() {
// do that something for client A
}
}
You can create configurable, singleton ClientWrapperFactory, the config is a map with [“A” – “wrapper.class.name.for.clientA”] pairs, and creates the instance by calling Class.forName(cfgMap.get(“A”)).newInstance(), store the current wrapper instance in the session context (or “get” it all the time, and use lazy creation) – I suppose you talk about a web application. Adding a new client means only extending this map with a new item, and create the appropriate wrapper class.
2
The second option is better. Use the Strategy Pattern. Of course you can use still use doSomethingForClientX()
in the private functions if you can’t think of a better name. Use the template pattern only if your sequence of operations (private function calls) remains the same.
There is something missing from your question: Is it just the one single method doSomething that needs to handle client A and client B differently, or are there more such methods? If there is just one difference, then you can get away with an if statement or maybe a switch statement, so the original isn’t too bad.
I wouldn’t expose doSomethingForClientA() and doSomethingForClientB(). You can have these two as private methods, and call the right one from your doSomething method. You might not do it that way if the code for each case is 95% the same.
Once you decide to use multiple classes, you show that you haven’t heard of the delegate pattern. That’s where you have one class, and you plug in a delegate object that is responsible for handling the differences between different clients. Obviously different delegate objects for different clients. Whether you use the delegate pattern or multiple classes, you get the huge advantage that you don’t have to pass information about the client around. Which is most useful if the caller is not the client and doesn’t actually know what kind of client is used.