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;
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.
add a comment |
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.
add a comment |
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.
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.
asked Mar 22 at 17:30
Mahdi-MalvMahdi-Malv
1,185823
1,185823
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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:
- The view calls the presenter method
fetchData(). - The presenter calls
showLoading()method of the contract. - (some time passes)
- The presenter calls
hideLoading(), andshowData(data)methods of the contract. - The user interacts and triggers
fetchData()again - 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.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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:
- The view calls the presenter method
fetchData(). - The presenter calls
showLoading()method of the contract. - (some time passes)
- The presenter calls
hideLoading(), andshowData(data)methods of the contract. - The user interacts and triggers
fetchData()again - 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.
add a comment |
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:
- The view calls the presenter method
fetchData(). - The presenter calls
showLoading()method of the contract. - (some time passes)
- The presenter calls
hideLoading(), andshowData(data)methods of the contract. - The user interacts and triggers
fetchData()again - 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.
add a comment |
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:
- The view calls the presenter method
fetchData(). - The presenter calls
showLoading()method of the contract. - (some time passes)
- The presenter calls
hideLoading(), andshowData(data)methods of the contract. - The user interacts and triggers
fetchData()again - 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.
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:
- The view calls the presenter method
fetchData(). - The presenter calls
showLoading()method of the contract. - (some time passes)
- The presenter calls
hideLoading(), andshowData(data)methods of the contract. - The user interacts and triggers
fetchData()again - 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.
answered Mar 22 at 18:24
Eric MartoriEric Martori
979411
979411
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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