Recently I have asked this question: How do you rewrite the code which using generics and functionals in Java 8 and mixing oop and functional programming by using only object-oriented? on stackoverflow and I couldn't get an answer but there are some comments and further one saying that maybe this is an Inner Platform Effect.
After that I thought that maybe I haven't asked the question as I have thought it should be so I have continued to research on it, but I could not find any meaningful resource.
Here is again the code that I'm asking about(in the question on stackoverflow there also client code):
public class MyUtils<R, MSGIN, MSGOUT> {
public SomeClass<R> getSomething(MSGIN query,
Function<MSGIN, MSGOUT> queryFunc,
Function<MSGOUT, List<R>> getResultList) {
//some logic here
MSGOUT callResult = queryFunc.apply(query);
//another logic here
buffer = getResultList.apply(callResult);
//yet another logic
//return someThing;
}
//...
}
What I was trying to ask is that is it some kind of object functional programming design pattern or if is not then is there a term or name for this technique.
Here what is curious case for me that I'm concentrating on it is that normally when using only object oriented programming you have some behaviour parameterization alternatives (strategy pattern, template pattern, visitor, to name a few) by utilizing dynamic dispatch and polymorphism, but when it comes to functional programming there are pattern matching, higher order functions and composition. In this case it looks like by utilizing generics on the class and type inference in the arguments that are higher order functions, it maybe implementing some kind of template or strategy pattern.
More specifically, a client calls this method giving lambda expressions whose inputs and outputs be something like that as they will be inferenced as designed like one parameter which is a higher order function taking other's output as input(A, B, C
type inference) to these parameters.
A query, //param 1 => A
Function<A, B> queryFunc, //param 2 => A -> B
Function<B, C> getResultList //param 3 => B -> C
Is this a technique? If it is then is there a name for it and does it utilize functional programming in Java 8? How can we get most of it from this technique? Can this technique be applied to more parts when designing programs in Java by utilizing also functional programming?
As suggested in the comments here is the possible reasons behind this abstraction(if it's the right terms) according to my understanding:
==========================
consume other service <- Consumer Component -> consume a REST service to get all results page by page
==========================
|
v
consume SOAP service to get all results page by page
============================
e.g: SOAPConsumer
----------------------------
private: offset = 0;
private: pageSize = 100;
private: filterCriteria = new FilterCriteria();
private: buffer = List.emptyList();
public: SOAPConsumer();
-----------------------------
public: consumeAll() {
conn = connect();
// I think it abstracts this while part. Maybe otherwise they thought that they need to implement that part in other consumers also.
continue = true;
while(continue) {
resultSet = sendRequest(conn, filterCriteria, offset, pageSize);
resultSetWithMyInterestedPartOfTheResult = extract(resultSet);
buffer.addAll(resultSetWithMyInterestedPartOfTheResult);
lastResult = getLastResult(resultSet); //setNext
offset = lastResult.getId();//setNext
if(offset < pageSize) then continue = false;
}
return buffer;
}
private: connect();
private: sendRequest();
private: extract();
private: getLastResult()
private: setNext();
---------------------------------
Please note that as I write in the question, I'm mainly exploring if it is common technique when mixing fp with oop.
Function.andThen()
is for function composition but what is interesting to me here is that when doing composition you don't have a template if I mean correctly, but in this code it looks like it is available to be used as a template, since you are not applying functions sequentially. There are logic in between the applications of functions in this method. So then I tend to think like it is not just a function composition and somehow it's mixing object oriented with functional programming. Please correct me If I'm wrong on some parts.