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?
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?
- um, why? Generics and the use of higher-order functions are generally ways to reuse code and make it more understandable. Devolving it like you seem to be suggesting will require lots of boilerplate, potentially error-prone code (especially without the use of generics). – ScouringsFetchUtils
since I only wanted to focus on the design itself. What I was meant by saying rewrite that trying to find out is there a general term for this design or some pointers for further study and what and how it could achieved this abstraction when you compare it to other possible designs. – BeanfeasthasNext
method and in it and uses that functionals after each next iteration. There is an other omitted part that implements a spliterator using that iterator and returns a stream. So the purpose in this code is that given a web service consumer or in general any function fetch something and process results by applying again given functionals. – Beanfeast