简体   繁体   中英

Resources/App Context in Presenter (MVP architecture)

I need to sort the list using Comparator which depends on Resources . The list (data) is loaded from the repository (Model layer). How can I do this if a Presenter should not have access to Context ? Should I inject already created Comparator ?

new ProductsPresenter(new ProductsRepository(), productsFragment, new ProductsComparator(context))

(prodcutsFragment is a View layer)

  1. Is it correct?
  2. What if the Presenter needs to use more Context depended classes? If I inject all of them, the constructor will have a lot of parameters.

Here is the code from Android blueprints where for example I'd like to add sorting the list with Comparator depended on Resources.

1 - It's correct. An improvement would be to use some kind of dependency library, like Dagger, to inject the instances directly, so you don't have to write new in constructor

2 - IMHO the best way is to inyect classes directly, without Context , to make your Presenter decoupled of Android SDK, so you'll be able to test it more easily

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM