I have come across with this design and I didn't fully grasp mixing object oriented programming with functional programming in Java 8.
I was interested in mixing both language paradigms and most of the tutorials in the internet are about simple, so I wasn't be able to find a sample about large-scale software design by mixing them. So with this sample case I think I had an opportunity to dig it for some cases of it.
Let me show the related parts of the code in this case. This code contains a generic FetchUtils class which implements a custom iterator, but for the sake of brevity I have removed some parts.
public class FetchUtils<R, MSGIN, MSGOUT> {
public SomeClass<R> getSomething(MSGIN query,
Function<MSGIN, MSGOUT> queryFunc,
Function<MSGOUT, List<R>> getResultList) {
//...
MSGOUT callResult = queryFunc.apply(query);
buffer = getResultList.apply(callResult);
//...
//return someThing;
}
//...
}
In the client there are a Function defined and a lambda expression pointing a reference to getCustomer
method of a class. Actual call from the client to above method which is using generic types is sending these functionals.
public class CustomerResponse {
//...
public List<Customer> getCustomer() {
if (thing == null) {
thing = new ArrayList<Customer>();
}
return this.customers;
}
//...
}
public class MyClient {
//...
@Autowired
private FetchUtils<Customer, CustomerRequest, CustomerResponse> fetchUtils;
//...
public SomeClass<Customer> fetch() {
//...
Function<CustomerRequest, CustomerResponse> requestFunc = q -> {
try {
return myService.doSomething(q);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
};
CustomerRequest query = new CustomerRequest(/*...*/);
return fetchUtils.getSomething(query,
requestFunc,
r -> r.getCustomer());
//...
}
//...
}
How do you rewrite this code only using object oriented programming i.e. without passing higher order functions and only using dynamic dispatch or even without generics?
Would this design be possible without generics?
How type inference works here with these generic types and functionals?
Is this design a possible example of mixing functional and object oriented programming or how do you evaluate this design?