1

ServiceOne.java

private void doStuff(){}

ServiceTwo.java

private void doStuff()

Where to put common code ?

Shall I create a new service, but then what interface will it implement ?

Or shall I just create a utility class ? Please guide on what is the best approrach.

2
  • If it is a non-static void method, it probably relies on some functionality within the class. Beware if you can successfully abstract this information before you try to refactor your method. Just a similar name may not infer an identical implementation. I mean, there are a lot of implementations of toString(). Commented Nov 14, 2014 at 14:16
  • @owlstead the doStuff methods are exactly identical. Commented Nov 14, 2014 at 21:27

1 Answer 1

3

Well it actually depends on the stuff. If the stuff is related to the business logic of the application and inside has to do with domain objects then it has to go in another class and be aggregated in the services or depending on domain in a superclass of both services. For example both services need to check if a user already exists.

On the other hand if that method has nothing to do with business logic, it could go in a utility class. For example suppose it has to poll a given function, calling it multiple time until response is not null.

2
  • 1
    superclass of both services, seems like a nice idea. Thanks. Commented Nov 14, 2014 at 21:28
  • @coding_idiot Only go that way if the classes are naturally of the same type. You could also go for composition and let both classes use another class that contains the method. Commented Nov 14, 2014 at 21:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.