Refactoring options – multiple methods in same class or into separate classes

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật