Use Kotlin higher order functions in MVPKotlin Ternary Conditional OperatorWhat is the equivalent of Java static methods in Kotlin?Moshi's Custom Adapter with RxAndroid & Retrofit & KotlinTableView Callback in KotlinImplement domain <-> dto mapper functionality in Kotlinandroid-kotlin click Listener not working to replace fragmentThis use Navigation Drawer, and use Tab, and use Fragment. how communication between FragmentActivity and Fragment?Mockito retrofit2 with MVP architectureIs there a possibility to serialize Kotlin Data class with function literal?How to add child(Product) under a child(Store) in Firebase Database using RecyclerView

Can I criticise the more senior developers around me for not writing clean code?

How to denote matrix elements succinctly?

Overlay of two functions leaves gaps

How to stop co-workers from teasing me because I know Russian?

As an international instructor, should I openly talk about my accent?

Checks user level and limit the data before saving it to mongoDB

Extension of 2-adic valuation to the real numbers

Check if a string is entirely made of the same substring

Was there a Viking Exchange as well as a Columbian one?

"You've called the wrong number" or "You called the wrong number"

What causes platform events to fail to be published and should I cater for failed platform event creations?

Was there a shared-world project before "Thieves World"?

If a planet has 3 moons, is it possible to have triple Full/New Moons at once?

Pulling the rope with one hand is as heavy as with two hands?

How to limit Drive Letters Windows assigns to new removable USB drives

Two field separators (colon and space) in awk

What is the most expensive material in the world that could be used to create Pun-Pun's lute?

Could the terminal length of components like resistors be reduced?

Can SQL Server create collisions in system generated constraint names?

Why does Mind Blank stop the Feeblemind spell?

What does the integral of a function times a function of a random variable represent, conceptually?

How can I practically buy stocks?

What's the polite way to say "I need to urinate"?

Initiative: Do I lose my attack/action if my target moves or dies before my turn in combat?



Use Kotlin higher order functions in MVP


Kotlin Ternary Conditional OperatorWhat is the equivalent of Java static methods in Kotlin?Moshi's Custom Adapter with RxAndroid & Retrofit & KotlinTableView Callback in KotlinImplement domain <-> dto mapper functionality in Kotlinandroid-kotlin click Listener not working to replace fragmentThis use Navigation Drawer, and use Tab, and use Fragment. how communication between FragmentActivity and Fragment?Mockito retrofit2 with MVP architectureIs there a possibility to serialize Kotlin Data class with function literal?How to add child(Product) under a child(Store) in Firebase Database using RecyclerView






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








3















Normally with Java, we make a *Contract interface to handle interaction between View and Presenter, like:



MainActivity

As View



public class MainActivity extends Activity implements MainContract {

@Override
public void onCreate(Bundle b)
presenter.requestData();


@Override
public void showData(String data)
// Handle data ...



MainPresenter

As Presenter



public class MainPresenter 

public void requestData()
contract.showData("data");




MainContract

As interface



public interface MainContract 
void showData(String data);



Since Kotlin has the feature "Higher order function", should we simply pass functions to handle interaction between view and presenter? It can be something like:



View:



presenter.requestData data ->
// Handle data



Presenter:



fun requestData(handler: (String) -> Unit) 
handler("data")



I'm not asking about the possibility, I'm asking if it's a best practice or not.










share|improve this question




























    3















    Normally with Java, we make a *Contract interface to handle interaction between View and Presenter, like:



    MainActivity

    As View



    public class MainActivity extends Activity implements MainContract {

    @Override
    public void onCreate(Bundle b)
    presenter.requestData();


    @Override
    public void showData(String data)
    // Handle data ...



    MainPresenter

    As Presenter



    public class MainPresenter 

    public void requestData()
    contract.showData("data");




    MainContract

    As interface



    public interface MainContract 
    void showData(String data);



    Since Kotlin has the feature "Higher order function", should we simply pass functions to handle interaction between view and presenter? It can be something like:



    View:



    presenter.requestData data ->
    // Handle data



    Presenter:



    fun requestData(handler: (String) -> Unit) 
    handler("data")



    I'm not asking about the possibility, I'm asking if it's a best practice or not.










    share|improve this question
























      3












      3








      3








      Normally with Java, we make a *Contract interface to handle interaction between View and Presenter, like:



      MainActivity

      As View



      public class MainActivity extends Activity implements MainContract {

      @Override
      public void onCreate(Bundle b)
      presenter.requestData();


      @Override
      public void showData(String data)
      // Handle data ...



      MainPresenter

      As Presenter



      public class MainPresenter 

      public void requestData()
      contract.showData("data");




      MainContract

      As interface



      public interface MainContract 
      void showData(String data);



      Since Kotlin has the feature "Higher order function", should we simply pass functions to handle interaction between view and presenter? It can be something like:



      View:



      presenter.requestData data ->
      // Handle data



      Presenter:



      fun requestData(handler: (String) -> Unit) 
      handler("data")



      I'm not asking about the possibility, I'm asking if it's a best practice or not.










      share|improve this question














      Normally with Java, we make a *Contract interface to handle interaction between View and Presenter, like:



      MainActivity

      As View



      public class MainActivity extends Activity implements MainContract {

      @Override
      public void onCreate(Bundle b)
      presenter.requestData();


      @Override
      public void showData(String data)
      // Handle data ...



      MainPresenter

      As Presenter



      public class MainPresenter 

      public void requestData()
      contract.showData("data");




      MainContract

      As interface



      public interface MainContract 
      void showData(String data);



      Since Kotlin has the feature "Higher order function", should we simply pass functions to handle interaction between view and presenter? It can be something like:



      View:



      presenter.requestData data ->
      // Handle data



      Presenter:



      fun requestData(handler: (String) -> Unit) 
      handler("data")



      I'm not asking about the possibility, I'm asking if it's a best practice or not.







      android kotlin android-mvp






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 22 at 17:30









      Mahdi-MalvMahdi-Malv

      1,185823




      1,185823






















          1 Answer
          1






          active

          oldest

          votes


















          2














          The answer of this question is more related architectural decisions than technical limitations. You could implement the same thing in Java (even in Java7) with anonymous instances (granted it would have a lot more of boilerplate and would be harder to read).



          The idea in MVP for having a contract that the view implements is that each presenter knows how to fetch, manipulate and present to said contract. Potentially a view can implement multiple contracts and have multiple presenters. Also each presenter instance only works with one implementation of the contract, but you could have two instances serving two different implementations.



          If instead of each view conforming to the contracts of each presenter each call of the presenter takes a lambda sooner or later you end up facing a problems.



          For example, imagine a presenter that fetches data asynchronously and caches it in memory:



          1. The view calls the presenter method fetchData().

          2. The presenter calls showLoading() method of the contract.

          3. (some time passes)

          4. The presenter calls hideLoading(), and showData(data) methods of the contract.

          5. The user interacts and triggers fetchData() again

          6. The presenter calls showData() with the cached data

          In this case if we used lambdas instead of a contract, we would need to request two different lambdas in the same method: one for when it's cached and one for when it's not.
          We, also, are coupling the view implementation to the presenter, in the future another implementation of the presenter interface might not need this two lambdas because the logic have changed.



          It is important to remember that in MVP ideally both view and presenter communicate to each other though interfaces and in this case the presenter interface should not be influenced by the implementation details and just expose what actions they are able to do.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55304960%2fuse-kotlin-higher-order-functions-in-mvp%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            The answer of this question is more related architectural decisions than technical limitations. You could implement the same thing in Java (even in Java7) with anonymous instances (granted it would have a lot more of boilerplate and would be harder to read).



            The idea in MVP for having a contract that the view implements is that each presenter knows how to fetch, manipulate and present to said contract. Potentially a view can implement multiple contracts and have multiple presenters. Also each presenter instance only works with one implementation of the contract, but you could have two instances serving two different implementations.



            If instead of each view conforming to the contracts of each presenter each call of the presenter takes a lambda sooner or later you end up facing a problems.



            For example, imagine a presenter that fetches data asynchronously and caches it in memory:



            1. The view calls the presenter method fetchData().

            2. The presenter calls showLoading() method of the contract.

            3. (some time passes)

            4. The presenter calls hideLoading(), and showData(data) methods of the contract.

            5. The user interacts and triggers fetchData() again

            6. The presenter calls showData() with the cached data

            In this case if we used lambdas instead of a contract, we would need to request two different lambdas in the same method: one for when it's cached and one for when it's not.
            We, also, are coupling the view implementation to the presenter, in the future another implementation of the presenter interface might not need this two lambdas because the logic have changed.



            It is important to remember that in MVP ideally both view and presenter communicate to each other though interfaces and in this case the presenter interface should not be influenced by the implementation details and just expose what actions they are able to do.






            share|improve this answer



























              2














              The answer of this question is more related architectural decisions than technical limitations. You could implement the same thing in Java (even in Java7) with anonymous instances (granted it would have a lot more of boilerplate and would be harder to read).



              The idea in MVP for having a contract that the view implements is that each presenter knows how to fetch, manipulate and present to said contract. Potentially a view can implement multiple contracts and have multiple presenters. Also each presenter instance only works with one implementation of the contract, but you could have two instances serving two different implementations.



              If instead of each view conforming to the contracts of each presenter each call of the presenter takes a lambda sooner or later you end up facing a problems.



              For example, imagine a presenter that fetches data asynchronously and caches it in memory:



              1. The view calls the presenter method fetchData().

              2. The presenter calls showLoading() method of the contract.

              3. (some time passes)

              4. The presenter calls hideLoading(), and showData(data) methods of the contract.

              5. The user interacts and triggers fetchData() again

              6. The presenter calls showData() with the cached data

              In this case if we used lambdas instead of a contract, we would need to request two different lambdas in the same method: one for when it's cached and one for when it's not.
              We, also, are coupling the view implementation to the presenter, in the future another implementation of the presenter interface might not need this two lambdas because the logic have changed.



              It is important to remember that in MVP ideally both view and presenter communicate to each other though interfaces and in this case the presenter interface should not be influenced by the implementation details and just expose what actions they are able to do.






              share|improve this answer

























                2












                2








                2







                The answer of this question is more related architectural decisions than technical limitations. You could implement the same thing in Java (even in Java7) with anonymous instances (granted it would have a lot more of boilerplate and would be harder to read).



                The idea in MVP for having a contract that the view implements is that each presenter knows how to fetch, manipulate and present to said contract. Potentially a view can implement multiple contracts and have multiple presenters. Also each presenter instance only works with one implementation of the contract, but you could have two instances serving two different implementations.



                If instead of each view conforming to the contracts of each presenter each call of the presenter takes a lambda sooner or later you end up facing a problems.



                For example, imagine a presenter that fetches data asynchronously and caches it in memory:



                1. The view calls the presenter method fetchData().

                2. The presenter calls showLoading() method of the contract.

                3. (some time passes)

                4. The presenter calls hideLoading(), and showData(data) methods of the contract.

                5. The user interacts and triggers fetchData() again

                6. The presenter calls showData() with the cached data

                In this case if we used lambdas instead of a contract, we would need to request two different lambdas in the same method: one for when it's cached and one for when it's not.
                We, also, are coupling the view implementation to the presenter, in the future another implementation of the presenter interface might not need this two lambdas because the logic have changed.



                It is important to remember that in MVP ideally both view and presenter communicate to each other though interfaces and in this case the presenter interface should not be influenced by the implementation details and just expose what actions they are able to do.






                share|improve this answer













                The answer of this question is more related architectural decisions than technical limitations. You could implement the same thing in Java (even in Java7) with anonymous instances (granted it would have a lot more of boilerplate and would be harder to read).



                The idea in MVP for having a contract that the view implements is that each presenter knows how to fetch, manipulate and present to said contract. Potentially a view can implement multiple contracts and have multiple presenters. Also each presenter instance only works with one implementation of the contract, but you could have two instances serving two different implementations.



                If instead of each view conforming to the contracts of each presenter each call of the presenter takes a lambda sooner or later you end up facing a problems.



                For example, imagine a presenter that fetches data asynchronously and caches it in memory:



                1. The view calls the presenter method fetchData().

                2. The presenter calls showLoading() method of the contract.

                3. (some time passes)

                4. The presenter calls hideLoading(), and showData(data) methods of the contract.

                5. The user interacts and triggers fetchData() again

                6. The presenter calls showData() with the cached data

                In this case if we used lambdas instead of a contract, we would need to request two different lambdas in the same method: one for when it's cached and one for when it's not.
                We, also, are coupling the view implementation to the presenter, in the future another implementation of the presenter interface might not need this two lambdas because the logic have changed.



                It is important to remember that in MVP ideally both view and presenter communicate to each other though interfaces and in this case the presenter interface should not be influenced by the implementation details and just expose what actions they are able to do.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 22 at 18:24









                Eric MartoriEric Martori

                979411




                979411





























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55304960%2fuse-kotlin-higher-order-functions-in-mvp%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                    용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

                    155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해