1

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.

  • 1
    For a relevant answer, your question contains too much '...' and 'something'. If you leave out the concrete code, this is just the same as Function.andThen(). – mtj Jan 16 at 9:35
  • @mtj thank you for suggestion, I think 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. – Taha Yavuz Bodur Jan 16 at 9:47
  • 2
    What would be the purpose of the technique illustrated in your question? – Robert Harvey Jan 16 at 15:25
  • @RobertHarvey This query function in the second parameter is a request to a web service and it uses first parameter to send request. Third parameter is the response of that request and it is for extracting given part. In the complete code the presented method here is a private method in that class and it returns a custom iterator that in its hasNext method it uses these higher order functions to apply them at each request. Then the only public method is getting that custom iterator and use it with a spliterator to return a stream. – Taha Yavuz Bodur Jan 16 at 15:48
  • I understand the mechanics. But why? – Robert Harvey Jan 16 at 15:48
2

There's an equivalence between your getSomething method and an abstract class. Both are examples of the Strategy Pattern.

public abstract class SomethingGetter<R, MSGIN, MSGOUT> {
    abstract MSGOUT query(MSGIN in);
    abstract List<R> getResults(MSGOUT out);

    public SomeClass<R> run(MSGIN query) {
       //some logic here
                MSGOUT callResult = queryFunc.apply(query);
       //another logic here
                buffer = getResultList.apply(callResult);
       //yet another logic

       //return someThing;
    }
}

Note that there can't be anything relating to R, MSGIN, or MSGOUT in the // logic heres, because you don't have any constraints on what they can be. Presumably it's something unconcerned with data like logging exceptions or profiling.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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