Avoid code duplication over classes

  • A+

I am writing some classes and all of them implement a certain method they inherit from an interface. This method is close to the same for all the classes beside one call to a certain other function.

For example:

public void doSomething(){     int a = 6;     int b = 7;     int c = anOtherMethod(a,b);     while(c < 50){         c++;     } } 

What if multiple classes have the function doSomething() but the implementation of the method anOtherMethod() is different?

How do I avoid code duplication in this situation? (This is not my actual code but a simplified version that helps me describe what I mean a bit better.)


Assuming every version of anOtherFunction takes two integers and returns an integer, I would just have the method accept a function as an argument, making it Higher Order.

A function that takes two arguments of the same type and returns an object of the same type is known as a BinaryOperator. You can add a argument of that type to the method to pass a function in:

// Give the method an operator argument  public void doSomething(BinaryOperator<Integer> otherMethod) {     int a = 6;     int b = 7;      // Then use it here basically like before     // "apply" is needed to call the passed function     int c = otherMethod.apply(a,b);     while(c < 50)          c++;     } } 

How you use it though will depend on your use case. As a simple example using a lambda, you can now call it like:

doSomething((a, b) -> a + b); 

Which simply returns the sum of a and b.

For your particular case though, you may find that having doSomething as part of a Interface isn't necessary or optimal. What if instead, anOtherMethod is what's required to be supplied? Instead of expecting your classes to supply a doSomething, have them supply a BinaryOperator<Integer>. Then, when you need to get results from doSomething, get the operator from the class, then pass it to doSomething. Something like:

public callDoSomething(HasOperator obj) {     // There may be a better way than having a "HasOperator" interface     // This is just an example though     BinaryOperator<Integer> f = obj.getOperator();      doSomething(f); } 


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: