Why should we use interface in MVP pattern for Android?What are MVP and MVC and what is the difference?Is there a way to run Python on Android?How do save an Android Activity state using save instance state?Does functional programming replace GoF design patterns?Difference between static class and singleton pattern?Close/hide the Android Soft KeyboardWhy is the Android emulator so slow? How can we speed up the Android emulator?Is there a unique Android device ID?What is 'Context' on Android?Proper use cases for Android UserManager.isUserAGoat()?
General purpose replacement for enum with FlagsAttribute
Dictionary size reduces upon increasing one element
Rename photos to match video titles
I think I may have violated academic integrity last year - what should I do?
Riley Rebuses that Share a Common Theme
Infinite Sequence based on Simple Rule
Employer demanding to see degree after poor code review
Does this degree 12 genus 1 curve have only one point over infinitely many finite fields?
At what point in European history could a government build a printing press given a basic description?
What is the difference between “/private/var/vm” and “/vm”?
Were pens caps holes designed to prevent death by suffocation if swallowed?
How to make a crossed out leftrightarrow?
Different circular sectors as new logo of the International System
Why does the 6502 have the BIT instruction?
Full backup on database creation
How were these pictures of spacecraft wind tunnel testing taken?
What are the benefits of cryosleep?
When and what was the first 3D acceleration device ever released?
What are these arcade games in Ghostbusters 1984?
Does revoking a certificate result in revocation of its key?
I unknowingly submitted plagiarised work
Why do airplanes use an axial flow jet engine instead of a more compact centrifugal jet engine?
Old short story, same personalities, differing planes of existence
How strong are Wi-Fi signals?
Why should we use interface in MVP pattern for Android?
What are MVP and MVC and what is the difference?Is there a way to run Python on Android?How do save an Android Activity state using save instance state?Does functional programming replace GoF design patterns?Difference between static class and singleton pattern?Close/hide the Android Soft KeyboardWhy is the Android emulator so slow? How can we speed up the Android emulator?Is there a unique Android device ID?What is 'Context' on Android?Proper use cases for Android UserManager.isUserAGoat()?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I'm making an Android app using Kotlin for the first time using MVP pattern. My questions is, why do I need interfaces for View and Presenter as Kotlin provides higher order functions? Can't we just communicate using those higher order functions? Is the use of pattern without interfaces bad?
I have looked and read lots of article and tutorials but non answered my question. Is what I am doing in the code below a wrong practice? Can someone explain it to me?
In my Activity
override fun init()
btn_login.setOnClickListener
LoginPresenter.userLogin(et_emailAddress.text.toString(),et_password.text.toString())
if (it)
//do something
else
//do something
My Presenter
object LoginPresenter
fun userLogin(emailId: String, password: String, completion: (Boolean) -> Unit)
//do something
completion(true)
add a comment |
I'm making an Android app using Kotlin for the first time using MVP pattern. My questions is, why do I need interfaces for View and Presenter as Kotlin provides higher order functions? Can't we just communicate using those higher order functions? Is the use of pattern without interfaces bad?
I have looked and read lots of article and tutorials but non answered my question. Is what I am doing in the code below a wrong practice? Can someone explain it to me?
In my Activity
override fun init()
btn_login.setOnClickListener
LoginPresenter.userLogin(et_emailAddress.text.toString(),et_password.text.toString())
if (it)
//do something
else
//do something
My Presenter
object LoginPresenter
fun userLogin(emailId: String, password: String, completion: (Boolean) -> Unit)
//do something
completion(true)
2
"why do I need interfaces for View and Presenter " - you don't need to use interfaces, however it's better to program to abstractions rather than implementations. Just think how this can benefit you in unit tests, or you change the implementation - the contract stays the same . I'd argue that there is no benefit in using an interface for a presenter, just the view. An observation, your Presenter is a singleton, it really should be scoped to the Activity. I'm not sure your argument for the higher order functions as you could supply aConsumer<T>in java? Also this breaks MVP flow control.
– Mark Keen
Mar 24 at 11:43
add a comment |
I'm making an Android app using Kotlin for the first time using MVP pattern. My questions is, why do I need interfaces for View and Presenter as Kotlin provides higher order functions? Can't we just communicate using those higher order functions? Is the use of pattern without interfaces bad?
I have looked and read lots of article and tutorials but non answered my question. Is what I am doing in the code below a wrong practice? Can someone explain it to me?
In my Activity
override fun init()
btn_login.setOnClickListener
LoginPresenter.userLogin(et_emailAddress.text.toString(),et_password.text.toString())
if (it)
//do something
else
//do something
My Presenter
object LoginPresenter
fun userLogin(emailId: String, password: String, completion: (Boolean) -> Unit)
//do something
completion(true)
I'm making an Android app using Kotlin for the first time using MVP pattern. My questions is, why do I need interfaces for View and Presenter as Kotlin provides higher order functions? Can't we just communicate using those higher order functions? Is the use of pattern without interfaces bad?
I have looked and read lots of article and tutorials but non answered my question. Is what I am doing in the code below a wrong practice? Can someone explain it to me?
In my Activity
override fun init()
btn_login.setOnClickListener
LoginPresenter.userLogin(et_emailAddress.text.toString(),et_password.text.toString())
if (it)
//do something
else
//do something
My Presenter
object LoginPresenter
fun userLogin(emailId: String, password: String, completion: (Boolean) -> Unit)
//do something
completion(true)
edited Mar 25 at 8:54
Onik
11.9k104465
11.9k104465
asked Mar 24 at 6:52
mandip girimandip giri
616
616
2
"why do I need interfaces for View and Presenter " - you don't need to use interfaces, however it's better to program to abstractions rather than implementations. Just think how this can benefit you in unit tests, or you change the implementation - the contract stays the same . I'd argue that there is no benefit in using an interface for a presenter, just the view. An observation, your Presenter is a singleton, it really should be scoped to the Activity. I'm not sure your argument for the higher order functions as you could supply aConsumer<T>in java? Also this breaks MVP flow control.
– Mark Keen
Mar 24 at 11:43
add a comment |
2
"why do I need interfaces for View and Presenter " - you don't need to use interfaces, however it's better to program to abstractions rather than implementations. Just think how this can benefit you in unit tests, or you change the implementation - the contract stays the same . I'd argue that there is no benefit in using an interface for a presenter, just the view. An observation, your Presenter is a singleton, it really should be scoped to the Activity. I'm not sure your argument for the higher order functions as you could supply aConsumer<T>in java? Also this breaks MVP flow control.
– Mark Keen
Mar 24 at 11:43
2
2
"why do I need interfaces for View and Presenter " - you don't need to use interfaces, however it's better to program to abstractions rather than implementations. Just think how this can benefit you in unit tests, or you change the implementation - the contract stays the same . I'd argue that there is no benefit in using an interface for a presenter, just the view. An observation, your Presenter is a singleton, it really should be scoped to the Activity. I'm not sure your argument for the higher order functions as you could supply a
Consumer<T> in java? Also this breaks MVP flow control.– Mark Keen
Mar 24 at 11:43
"why do I need interfaces for View and Presenter " - you don't need to use interfaces, however it's better to program to abstractions rather than implementations. Just think how this can benefit you in unit tests, or you change the implementation - the contract stays the same . I'd argue that there is no benefit in using an interface for a presenter, just the view. An observation, your Presenter is a singleton, it really should be scoped to the Activity. I'm not sure your argument for the higher order functions as you could supply a
Consumer<T> in java? Also this breaks MVP flow control.– Mark Keen
Mar 24 at 11:43
add a comment |
2 Answers
2
active
oldest
votes
why do I need interfaces for View and Presenter as Kotlin provides higher order functions?
This is rather a common practice in software development. And while you may not use interfaces, there is a number of key points why interfaces are preferable. Off the top of my head:
with interface you can have multiple implementations of it without actually caring about the concrete type of the implementation. This is what you're missing with the higher order functions - you're restricted with the only type,
LoginPresenter, when using theLoginPresenter.userLogin()method.most of the design patterns is based on the separation of interfaces from their implementations. So programming into implementation rather than abstraction won't let you make use of those.
you won't be able to properly unit test classes that depend on other implementations as no mocking is possible in this case.
code maintenance and extension becomes much harder with concrete implementation.
add a comment |
Higher-order function costs
Kotlin official documentation on the cost of higher order functions
Using higher-order functions imposes certain runtime penalties: each
function is an object, and it captures a closure, i.e. those variables
that are accessed in the body of the function. Memory allocations
(both for function objects and classes) and virtual calls introduce
runtime overhead.and if you're replacing all your interfaces with higher-order functions, you may end up with a bad performance.
2.
Interfaces can hold multiple functions, for which you'll need individual function params when using higher-order functions.
Consider the following case,
interface UserLoginInterface
fun onLoginSuccess(loggedInUser: User)
fun onLoginFailure(error: ErrorResponse)
fun onRedirect(someOtherObjectWithDirectives: SomeDataClass)
To translate this to higher-order functions usage, You'll have to use three Function params
Didn't knew using higher order functions could have caused bad performance.Thanks.
– mandip giri
Mar 25 at 6:46
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%2f55321400%2fwhy-should-we-use-interface-in-mvp-pattern-for-android%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
why do I need interfaces for View and Presenter as Kotlin provides higher order functions?
This is rather a common practice in software development. And while you may not use interfaces, there is a number of key points why interfaces are preferable. Off the top of my head:
with interface you can have multiple implementations of it without actually caring about the concrete type of the implementation. This is what you're missing with the higher order functions - you're restricted with the only type,
LoginPresenter, when using theLoginPresenter.userLogin()method.most of the design patterns is based on the separation of interfaces from their implementations. So programming into implementation rather than abstraction won't let you make use of those.
you won't be able to properly unit test classes that depend on other implementations as no mocking is possible in this case.
code maintenance and extension becomes much harder with concrete implementation.
add a comment |
why do I need interfaces for View and Presenter as Kotlin provides higher order functions?
This is rather a common practice in software development. And while you may not use interfaces, there is a number of key points why interfaces are preferable. Off the top of my head:
with interface you can have multiple implementations of it without actually caring about the concrete type of the implementation. This is what you're missing with the higher order functions - you're restricted with the only type,
LoginPresenter, when using theLoginPresenter.userLogin()method.most of the design patterns is based on the separation of interfaces from their implementations. So programming into implementation rather than abstraction won't let you make use of those.
you won't be able to properly unit test classes that depend on other implementations as no mocking is possible in this case.
code maintenance and extension becomes much harder with concrete implementation.
add a comment |
why do I need interfaces for View and Presenter as Kotlin provides higher order functions?
This is rather a common practice in software development. And while you may not use interfaces, there is a number of key points why interfaces are preferable. Off the top of my head:
with interface you can have multiple implementations of it without actually caring about the concrete type of the implementation. This is what you're missing with the higher order functions - you're restricted with the only type,
LoginPresenter, when using theLoginPresenter.userLogin()method.most of the design patterns is based on the separation of interfaces from their implementations. So programming into implementation rather than abstraction won't let you make use of those.
you won't be able to properly unit test classes that depend on other implementations as no mocking is possible in this case.
code maintenance and extension becomes much harder with concrete implementation.
why do I need interfaces for View and Presenter as Kotlin provides higher order functions?
This is rather a common practice in software development. And while you may not use interfaces, there is a number of key points why interfaces are preferable. Off the top of my head:
with interface you can have multiple implementations of it without actually caring about the concrete type of the implementation. This is what you're missing with the higher order functions - you're restricted with the only type,
LoginPresenter, when using theLoginPresenter.userLogin()method.most of the design patterns is based on the separation of interfaces from their implementations. So programming into implementation rather than abstraction won't let you make use of those.
you won't be able to properly unit test classes that depend on other implementations as no mocking is possible in this case.
code maintenance and extension becomes much harder with concrete implementation.
edited Mar 24 at 16:35
answered Mar 24 at 14:49
OnikOnik
11.9k104465
11.9k104465
add a comment |
add a comment |
Higher-order function costs
Kotlin official documentation on the cost of higher order functions
Using higher-order functions imposes certain runtime penalties: each
function is an object, and it captures a closure, i.e. those variables
that are accessed in the body of the function. Memory allocations
(both for function objects and classes) and virtual calls introduce
runtime overhead.and if you're replacing all your interfaces with higher-order functions, you may end up with a bad performance.
2.
Interfaces can hold multiple functions, for which you'll need individual function params when using higher-order functions.
Consider the following case,
interface UserLoginInterface
fun onLoginSuccess(loggedInUser: User)
fun onLoginFailure(error: ErrorResponse)
fun onRedirect(someOtherObjectWithDirectives: SomeDataClass)
To translate this to higher-order functions usage, You'll have to use three Function params
Didn't knew using higher order functions could have caused bad performance.Thanks.
– mandip giri
Mar 25 at 6:46
add a comment |
Higher-order function costs
Kotlin official documentation on the cost of higher order functions
Using higher-order functions imposes certain runtime penalties: each
function is an object, and it captures a closure, i.e. those variables
that are accessed in the body of the function. Memory allocations
(both for function objects and classes) and virtual calls introduce
runtime overhead.and if you're replacing all your interfaces with higher-order functions, you may end up with a bad performance.
2.
Interfaces can hold multiple functions, for which you'll need individual function params when using higher-order functions.
Consider the following case,
interface UserLoginInterface
fun onLoginSuccess(loggedInUser: User)
fun onLoginFailure(error: ErrorResponse)
fun onRedirect(someOtherObjectWithDirectives: SomeDataClass)
To translate this to higher-order functions usage, You'll have to use three Function params
Didn't knew using higher order functions could have caused bad performance.Thanks.
– mandip giri
Mar 25 at 6:46
add a comment |
Higher-order function costs
Kotlin official documentation on the cost of higher order functions
Using higher-order functions imposes certain runtime penalties: each
function is an object, and it captures a closure, i.e. those variables
that are accessed in the body of the function. Memory allocations
(both for function objects and classes) and virtual calls introduce
runtime overhead.and if you're replacing all your interfaces with higher-order functions, you may end up with a bad performance.
2.
Interfaces can hold multiple functions, for which you'll need individual function params when using higher-order functions.
Consider the following case,
interface UserLoginInterface
fun onLoginSuccess(loggedInUser: User)
fun onLoginFailure(error: ErrorResponse)
fun onRedirect(someOtherObjectWithDirectives: SomeDataClass)
To translate this to higher-order functions usage, You'll have to use three Function params
Higher-order function costs
Kotlin official documentation on the cost of higher order functions
Using higher-order functions imposes certain runtime penalties: each
function is an object, and it captures a closure, i.e. those variables
that are accessed in the body of the function. Memory allocations
(both for function objects and classes) and virtual calls introduce
runtime overhead.and if you're replacing all your interfaces with higher-order functions, you may end up with a bad performance.
2.
Interfaces can hold multiple functions, for which you'll need individual function params when using higher-order functions.
Consider the following case,
interface UserLoginInterface
fun onLoginSuccess(loggedInUser: User)
fun onLoginFailure(error: ErrorResponse)
fun onRedirect(someOtherObjectWithDirectives: SomeDataClass)
To translate this to higher-order functions usage, You'll have to use three Function params
answered Mar 24 at 7:54
Maran SubburayanMaran Subburayan
8618
8618
Didn't knew using higher order functions could have caused bad performance.Thanks.
– mandip giri
Mar 25 at 6:46
add a comment |
Didn't knew using higher order functions could have caused bad performance.Thanks.
– mandip giri
Mar 25 at 6:46
Didn't knew using higher order functions could have caused bad performance.Thanks.
– mandip giri
Mar 25 at 6:46
Didn't knew using higher order functions could have caused bad performance.Thanks.
– mandip giri
Mar 25 at 6:46
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%2f55321400%2fwhy-should-we-use-interface-in-mvp-pattern-for-android%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
2
"why do I need interfaces for View and Presenter " - you don't need to use interfaces, however it's better to program to abstractions rather than implementations. Just think how this can benefit you in unit tests, or you change the implementation - the contract stays the same . I'd argue that there is no benefit in using an interface for a presenter, just the view. An observation, your Presenter is a singleton, it really should be scoped to the Activity. I'm not sure your argument for the higher order functions as you could supply a
Consumer<T>in java? Also this breaks MVP flow control.– Mark Keen
Mar 24 at 11:43