{"id":262195,"date":"2015-07-24T12:47:07","date_gmt":"2015-07-24T08:47:07","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=262195"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=262195","title":{"rendered":"\u0412\u0430\u0440\u0438\u0430\u043d\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 MVP + DI(Dagger 2) \u0432 Android"},"content":{"rendered":"<p>             \u0421 \u043d\u0435\u0434\u0430\u0432\u043d\u0438\u0445 \u043f\u043e\u0440 \u0437\u0430\u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043e\u0432\u0430\u043b\u0441\u044f \u0434\u0430\u043d\u043d\u043e\u0439 \u0442\u0435\u043c\u043e\u0439 \u0438 \u043f\u043e\u0440\u044b\u043b\u0441\u044f \u0432 \u0434\u0435\u0431\u0440\u044f\u0445 \u0441\u0435\u0442\u0438 \u043d\u0430 \u044d\u0442\u0443 \u0442\u0435\u043c\u0443. \u041d\u0430 \u0430\u043d\u0433\u043b\u043e\u044f\u0437\u044b\u0447\u043d\u044b\u0445 \u0440\u0435\u0441\u0443\u0440\u0441\u0430\u0445 \u0435\u0441\u0442\u044c \u0440\u0430\u0441\u043a\u0438\u0434\u0430\u043d\u043d\u0430\u044f \u043f\u043e \u0440\u0430\u0437\u043d\u044b\u043c \u043c\u0435\u0441\u0442\u0430\u043c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f. \u041f\u0440\u043e\u0447\u0438\u0442\u0430\u043b \u0432\u0441\u0435, \u0447\u0442\u043e \u0435\u0441\u0442\u044c \u0432 \u0440\u0443\u043d\u0435\u0442\u0435 \u043d\u0430 \u044d\u0442\u0443 \u0442\u0435\u043c\u0443. \u0421\u0441\u044b\u043b\u043a\u0438 \u043f\u0440\u0438\u0432\u0435\u0434\u0443 \u0432 \u043a\u043e\u043d\u0446\u0435 \u0441\u0442\u0430\u0442\u044c\u0438. \u0412 \u0438\u0442\u043e\u0433\u0435, \u0441\u0442\u0430\u043b \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u044d\u0442\u043e\u0442 \u043f\u043e\u0434\u0445\u043e\u0434 \u0432 \u0441\u0432\u043e\u0438\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445.<\/p>\n<p>  \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430, \u0432\u0441\u043f\u043e\u043c\u043d\u0438\u043c, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 <a href=\"https:\/\/ru.wikipedia.org\/wiki\/Model-View-Presenter\">MVP<\/a>. MVP \u2014 \u044d\u0442\u043e \u043f\u0430\u0442\u0442\u0435\u0440\u043d, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0440\u0430\u0437\u0431\u0438\u0432\u0430\u0442\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043d\u0430 \u0442\u0440\u0438 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u0441\u043b\u043e\u044f (\u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430):   <\/p>\n<ol>\n<li>\u041c\u043e\u0434\u0435\u043b\u044c (Model) \u2014 \u0433\u0434\u0435 \u0441\u043e\u0441\u0440\u0435\u0434\u043e\u0442\u043e\u0447\u0435\u043d\u044b \u0434\u0430\u043d\u043d\u044b\u0435;<\/li>\n<li>\u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 (View) \u2014 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f (UI \u2014 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b);<\/li>\n<li>Presenter \u2014 \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u044b\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0441\u0432\u044f\u0437\u044c \u043c\u0435\u0436\u0434\u0443 \u041c\u043e\u0434\u0435\u043b\u044c\u044e \u0438 \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435\u043c.<\/li>\n<\/ol>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/bc1\/d6a\/4cc\/bc1d6a4cc80a447d94cfade436724c9f.png\" alt=\"image\"\/><\/p>\n<p>  MVP \u043f\u0430\u0442\u0442\u0435\u0440\u043d \u2014 \u044d\u0442\u043e \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a \u0431\u043e\u043b\u0435\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e\u0433\u043e \u0448\u0430\u0431\u043b\u043e\u043d\u0430 \u2014 <a href=\"https:\/\/ru.wikipedia.org\/wiki\/Model-View-Controller\">MVC<\/a>. \u041f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0442\u0430\u043a\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u0442\u0434\u0435\u043b\u0438\u0442\u044c \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u043b\u043e\u0433\u0438\u043a\u0443 \u043e\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430.<br \/>  <a name=\"habracut\"><\/a><br \/>  \u0422\u043e, \u043a\u0430\u043a \u0441\u0435\u0439\u0447\u0430\u0441 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u0430 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 Android, \u0441 \u00ab\u043d\u0430\u0442\u044f\u0436\u043a\u043e\u0439\u00bb \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0437\u0432\u0430\u0442\u044c \u0447\u0435\u043c-\u0442\u043e \u043f\u043e\u0445\u043e\u0436\u0438\u043c \u043d\u0430 MVP. \u0422.\u0435. \u043c\u043e\u0434\u0435\u043b\u044c \u2014 \u044d\u0442\u043e \u043a\u0430\u043a\u0438\u0435-\u043b\u0438\u0431\u043e \u0434\u0430\u043d\u043d\u044b\u0435, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u2014 \u044d\u0442\u043e UI-\u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u043d\u0430\u0448\u0438\u0445 layout-xml \u0444\u0430\u0439\u043b\u0430\u0445, \u0430 \u043a presenter \u043c\u043e\u0436\u043d\u043e \u043e\u0442\u043d\u0435\u0441\u0442\u0438 Activity \u0438 Fragment. \u041d\u043e, \u044d\u0442\u043e \u043d\u0435 \u0442\u0430\u043a \u0438 \u043d\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u043e\u0442\u0434\u0435\u043b\u0438\u0442\u044c \u043b\u043e\u0433\u0438\u043a\u0443 \u043e\u0442 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u044d\u0442\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445. <\/p>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c MVP \u0432 \u043d\u0430\u0448\u0435\u043c Android \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438.<\/p>\n<p>  \u0414\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 presenter \u0432\u043e view \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u043f\u0430\u0442\u0442\u0435\u0440\u043d \u2014 <a href=\"https:\/\/ru.wikipedia.org\/wiki\/%D0%92%D0%BD%D0%B5%D0%B4%D1%80%D0%B5%D0%BD%D0%B8%D0%B5_%D0%B7%D0%B0%D0%B2%D0%B8%D1%81%D0%B8%D0%BC%D0%BE%D1%81%D1%82%D0%B8\">Dependency Injection (DI)<\/a>. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u043e\u0439 \u043e\u0442 Google \u2013 <a href=\"http:\/\/google.github.io\/dagger\/\">Dagger 2<\/a>. \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u043d\u0435 \u0431\u0443\u0434\u0443 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u044c \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u0438 \u0435\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u044b \u0435\u0435 \u0440\u0430\u0431\u043e\u0442\u044b. \u041f\u043e\u0434\u0440\u0430\u0437\u0443\u043c\u0435\u0432\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u044d\u0442\u043e \u043d\u0430\u043c \u0443\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e.<\/p>\n<p>  \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u043a\u043e\u043d\u0444\u0435\u0440\u0435\u043d\u0446\u0438\u0439 \u0441 \u0441\u0430\u0439\u0442\u0430 <a href=\"http:\/\/www.ted.com\">www.ted.com<\/a>. \u041e\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043e\u0434\u043d\u0443 \u0433\u043b\u0430\u0432\u043d\u0443\u044e activity \u0438 \u0442\u0440\u0438 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0430: \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u0441\u043e \u0441\u043f\u0438\u0441\u043a\u043e\u043c, \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u0441 \u0434\u0435\u0442\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u0438 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0430 \u043a\u043e\u043d\u0444\u0435\u0440\u0435\u043d\u0446\u0438\u0438.<\/p>\n<p>  \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0433\u0440\u0430\u0444\u0430 \u0434\u043b\u044f DI \u0432\u044b\u043d\u0435\u0441\u0435\u043c \u0432 \u043a\u043b\u0430\u0441\u0441 \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a Application:<\/p>\n<pre><code class=\"java\">public class TalksTEDApp extends Application {      private ITalksTEDAppComponent appComponent;      public static TalksTEDApp get(Context context) {         return (TalksTEDApp) context.getApplicationContext();     }      @Override     public void onCreate() {         super.onCreate();         buildGraphAndInject();     }      public ITalksTEDAppComponent getAppComponent() {         return appComponent;     }      public void buildGraphAndInject() {         appComponent = DaggerITalksTEDAppComponent.builder()                 .talksTEDAppModule(new TalksTEDAppModule(this))                 .build();         appComponent.inject(this);     } } <\/code><\/pre>\n<p>  \u0412 \u0431\u0430\u0437\u043e\u0432\u043e\u043c \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u043e\u043c \u043a\u043b\u0430\u0441\u0441\u0435 BaseActivity \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 setupComponent, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u0430\u043a\u0442\u0438\u0432\u0438\u0442\u0438 \u0432 \u043d\u0430\u0448\u0435\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438.<\/p>\n<pre><code class=\"java\">public abstract class BaseActivity extends AppCompatActivity {      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setupComponent(TalksTEDApp.get(this).getAppComponent());     }      protected abstract void setupComponent(ITalksTEDAppComponent appComponent);  } <\/code><\/pre>\n<p>  \u0414\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c DI \u0438 \u0434\u043b\u044f \u043d\u0430\u0448\u0438\u0445 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u043e\u0432, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e:<\/p>\n<p>  1. \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 IHasComponent, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d \u0432 \u043a\u0430\u0436\u0434\u043e\u0439 \u043d\u0430\u0448\u0435\u0439 \u0430\u043a\u0442\u0438\u0432\u0438\u0442\u0438:<\/p>\n<pre><code class=\"java\">public interface IHasComponent &lt;T&gt; {     T getComponent(); } <\/code><\/pre>\n<p>  2. \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u043e\u0432:<\/p>\n<pre><code class=\"java\">public abstract class BaseFragment extends Fragment {     @SuppressWarnings(&quot;unchecked&quot;)     protected &lt;T&gt; T getComponent(Class&lt;T&gt; componentType) {         return componentType.cast(((IHasComponent&lt;T&gt;)getActivity()).getComponent());     } } <\/code><\/pre>\n<p>  3. \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441, \u043a\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d \u0432 \u043a\u0430\u0436\u0434\u043e\u043c Presenter \u0434\u043b\u044f \u043d\u0430\u0448\u0438\u0445 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u043e\u0432:<\/p>\n<pre><code class=\"java\">public interface BaseFragmentPresenter&lt;T&gt; {     void init(T view); } <\/code><\/pre>\n<p>  \u0414\u0430\u043b\u0435\u0435, \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c Module \u0438 Component \u043a\u043b\u0430\u0441\u0441\u044b \u0434\u043b\u044f \u043d\u0430\u0448\u0435\u0433\u043e Application:<\/p>\n<pre><code class=\"java\">@Module public class TalksTEDAppModule {      private final TalksTEDApp app;      public TalksTEDAppModule(TalksTEDApp app) {         this.app = app;     }      @Provides     @Singleton     public Application provideApplication() {         return app;     } }  <\/code><\/pre>\n<p>  <\/p>\n<pre><code class=\"java\">@Singleton @Component(         modules = {                 TalksTEDAppModule.class         } ) public interface ITalksTEDAppComponent {     void inject(TalksTEDApp app); } <\/code><\/pre>\n<p>  \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0434\u043b\u044f \u043d\u0430\u0448\u0438\u0445 \u0430\u043a\u0442\u0438\u0432\u0438\u0442\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u0441\u043a\u043e\u0443\u043f \u043a\u0430\u043a:<\/p>\n<pre><code class=\"java\">@Scope @Retention(RetentionPolicy.RUNTIME) public @interface ActivityScope { } <\/code><\/pre>\n<p>  \u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441 \u0430\u043a\u0442\u0438\u0432\u0438\u0442\u0438 \u0434\u043b\u044f \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u044d\u0442\u043e \u043e\u0434\u043d\u0430 \u0433\u043b\u0430\u0432\u043d\u0430\u044f \u0430\u043a\u0442\u0438\u0432\u0438\u0442\u0438. \u0412 \u043a\u043e\u0434\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u044b \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0435 \u043d\u0430\u043c \u043c\u0435\u0442\u043e\u0434\u044b, \u0430 \u043f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 GitHub (\u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043f\u0440\u043e\u0435\u043a\u0442 \u0432 \u043a\u043e\u043d\u0446\u0435 \u0441\u0442\u0430\u0442\u044c\u0438.)<\/p>\n<pre><code class=\"java\">public class MainActivity extends BaseActivity implements IMainActivityView, IHasComponent&lt;IMainActivityComponent&gt; {      @Inject     MainActivityPresenterImpl presenter;      private IMainActivityComponent mainActivityComponent;      ...      @Override     protected void setupComponent(ITalksTEDAppComponent appComponent) {         mainActivityComponent = DaggerIMainActivityComponent.builder()                 .iTalksTEDAppComponent(appComponent)                 .mainActivityModule(new MainActivityModule(this))                 .build();         mainActivityComponent.inject(this);     }      @Override     public IMainActivityComponent getComponent() {         return mainActivityComponent;     }      ...     } <\/code><\/pre>\n<p>  \u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0448\u0430\u0433 \u2014 \u044d\u0442\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0430\u0448\u0438\u0445 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u043e\u0432 (\u043f\u0440\u0438\u0432\u0435\u0434\u0443 \u043b\u0438\u0448\u044c \u043a\u043e\u0434 \u043c\u0435\u0442\u043e\u0434\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0430\u043c \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b \u0432 \u043f\u043b\u0430\u043d\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 DI):<\/p>\n<pre><code class=\"java\">public class ListFragment extends BaseFragment implements IListFragmentView {      @Inject     ListFragmentPresenterImpl presenter;      protected SpiceManager spiceManager = new SpiceManager(TalkTEDService.class);      private Activity activity;     private ListView listView;     private TalkListAdapter talkListAdapter;     private View rootView;      public ListFragment() {     }      ...      @Override     public void onActivityCreated(Bundle savedInstanceState) {         super.onActivityCreated(savedInstanceState);         this.getComponent(IMainActivityComponent.class).inject(this);     }      @Override     public void onResume() {         super.onResume();         presenter.init(this);         presenter.onResume(spiceManager);      }      ...  } <\/code><\/pre>\n<p>  \u0418 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 \u2014 \u044d\u0442\u043e \u043f\u0440\u0438\u043c\u0435\u0440 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0430\u0448\u0438\u0445 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 presenter.<\/p>\n<p>  \u0414\u043b\u044f \u0430\u043a\u0442\u0438\u0432\u0438\u0442\u0438:<\/p>\n<pre><code class=\"java\">public class MainActivityPresenterImpl implements IMainActivityPresenter {      private IMainActivityView view;      @Inject     public MainActivityPresenterImpl(IMainActivityView view) {         this.view = view;     }      @Override     public void onBackPressed() {         view.popFragmentFromStack();     } } <\/code><\/pre>\n<p>  \u0414\u043b\u044f \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0430:<\/p>\n<pre><code class=\"java\">public class ListFragmentPresenterImpl implements IListFragmentPresenter {      int offset = 0;      private static final String URL_LIST_TALKS_API = &quot;https:\/\/api.ted.com\/v1\/talks.json?api-key=umdz5qctsk4g9nmqnp5btsmf&limit=30&quot;;      private IListFragmentView view;     int totalTalks;     private SpiceManager spiceManager;      @Inject     public ListFragmentPresenterImpl() {     }      @Override     public void init(IListFragmentView view) {         this.view=view;     }      ... } <\/code><\/pre>\n<p>  \u042d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f \u043d\u0435 \u043f\u0440\u0435\u0442\u0435\u043d\u0434\u0443\u0435\u0442 \u043d\u0430 \u043f\u043e\u043b\u043d\u043e\u0435 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 MVP \u0438 Dependency Injection, \u044d\u0442\u043e \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u043f\u0440\u0438\u043c\u0435\u0440, \u043a\u0430\u043a \u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u0432 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435 Android \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u0412\u043d\u0430\u0447\u0430\u043b\u0435 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u043d\u0430\u0433\u0440\u043e\u043c\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u043b\u0438\u0448\u043d\u0438\u0445 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0438 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u0432 \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u0442 \u0447\u0438\u0442\u0430\u0435\u043c\u043e\u0441\u0442\u044c \u043a\u043e\u0434\u0430, \u043d\u043e \u044d\u0442\u043e \u043d\u0435 \u0442\u0430\u043a. \u041f\u043e\u0441\u043b\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f MVP \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043f\u0440\u043e\u0449\u0435 \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438, \u043a\u043e\u0434 \u043f\u0440\u043e\u0449\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u0442\u044c. <br \/>  \u041c\u044b \u0432\u0441\u0435 \u0431\u043e\u043b\u044c\u0448\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u0443\u0435\u043c \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u0432, \u0430 \u043d\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438, \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0441\u043b\u0430\u0431\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u044b, \u0447\u0442\u043e \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043e\u0448\u0438\u0431\u043e\u043a \u043f\u0440\u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f\u0445. \u0410 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c Dependency Injection \u043a\u043e\u0434 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0431\u043e\u043b\u0435\u0435 \u043b\u0430\u043a\u043e\u043d\u0438\u0447\u043d\u044b\u043c \u0438 \u0447\u0438\u0442\u0430\u0435\u043c\u044b\u043c.<\/p>\n<p>  \u041e\u0442\u043c\u0435\u0447\u0443, \u0447\u0442\u043e \u0432 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u0432 Dagger 2 \u0435\u0441\u0442\u044c \u043e\u0434\u0438\u043d \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u043a. \u041c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u0434\u0435\u043b\u0430\u0442\u044c \u0432 \u043c\u043e\u0434\u0443\u043b\u0435 override (\u044d\u0442\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043e \u0432 <a href=\"http:\/\/square.github.io\/dagger\/\">Dagger<\/a> \u043e\u0442 Square). \u042d\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u043f\u0440\u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0438 \u0442\u0435\u0441\u0442\u043e\u0432. \u041d\u0430\u0434\u0435\u044e\u0441\u044c, \u0447\u0442\u043e \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f\u0445 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u044d\u0442\u043e\u0442 \u043d\u0435\u0434\u043e\u0447\u0435\u0442 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d. \u041a\u043e\u043c\u0443 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0430 \u044d\u0442\u0430 \u0442\u0435\u043c\u0430 \u2014 \u0435\u0441\u0442\u044c \u043f\u043e\u0441\u0442 \u043d\u0430 StackOverflow <a href=\"http:\/\/stackoverflow.com\/questions\/29989245\/android-unit-tests-with-dagger-2\">\u0437\u0434\u0435\u0441\u044c<\/a> \u0438 <a href=\"https:\/\/stackoverflow.com\/questions\/26939340\/how-do-you-override-a-module-dependency-in-a-unit-test-with-dagger-2-0?lq=1\">\u0437\u0434\u0435\u0441\u044c<\/a>.<\/p>\n<p>  \u041f\u0440\u0438\u0432\u0435\u0434\u0443 \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u0441\u0442\u0430\u0442\u044c\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0434\u043e\u0432\u0435\u043b\u0438 \u043c\u0435\u043d\u044f \u0434\u043e \u0442\u0430\u043a\u043e\u0439 \u0436\u0438\u0437\u043d\u0438:<\/p>\n<p>  <a href=\"http:\/\/habrahabr.ru\/post\/202866\/\">habrahabr.ru\/post\/202866<\/a><br \/>  <a href=\"http:\/\/habrahabr.ru\/post\/252903\/\">habrahabr.ru\/post\/252903<\/a><br \/>  <a href=\"http:\/\/antonioleiva.com\/mvp-android\/\">antonioleiva.com\/mvp-android<\/a><br \/>  <a href=\"http:\/\/antonioleiva.com\/dependency-injection-android-dagger-part-1\/\">antonioleiva.com\/dependency-injection-android-dagger-part-1<\/a><br \/>  <a href=\"http:\/\/antonioleiva.com\/dagger-android-part-2\/\">antonioleiva.com\/dagger-android-part-2<\/a><br \/>  <a href=\"http:\/\/antonioleiva.com\/dagger-3\/\">antonioleiva.com\/dagger-3<\/a><br \/>  <a href=\"https:\/\/plus.google.com\/communities\/114285790907815804707\">Android MVP \u2014 Community<\/a><\/p>\n<p>  \u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434 \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d \u0432 \u0441\u0442\u0430\u0442\u044c\u0435, \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0438 \u0441\u043a\u0430\u0447\u0430\u0442\u044c \u043d\u0430 <a href=\"https:\/\/github.com\/Mitutov\/TalksTED\">GitHub<\/a>.             <\/p>\n<div class=\"clear\"><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/263465\/\"> http:\/\/habrahabr.ru\/post\/263465\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>             \u0421 \u043d\u0435\u0434\u0430\u0432\u043d\u0438\u0445 \u043f\u043e\u0440 \u0437\u0430\u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043e\u0432\u0430\u043b\u0441\u044f \u0434\u0430\u043d\u043d\u043e\u0439 \u0442\u0435\u043c\u043e\u0439 \u0438 \u043f\u043e\u0440\u044b\u043b\u0441\u044f \u0432 \u0434\u0435\u0431\u0440\u044f\u0445 \u0441\u0435\u0442\u0438 \u043d\u0430 \u044d\u0442\u0443 \u0442\u0435\u043c\u0443. \u041d\u0430 \u0430\u043d\u0433\u043b\u043e\u044f\u0437\u044b\u0447\u043d\u044b\u0445 \u0440\u0435\u0441\u0443\u0440\u0441\u0430\u0445 \u0435\u0441\u0442\u044c \u0440\u0430\u0441\u043a\u0438\u0434\u0430\u043d\u043d\u0430\u044f \u043f\u043e \u0440\u0430\u0437\u043d\u044b\u043c \u043c\u0435\u0441\u0442\u0430\u043c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f. \u041f\u0440\u043e\u0447\u0438\u0442\u0430\u043b \u0432\u0441\u0435, \u0447\u0442\u043e \u0435\u0441\u0442\u044c \u0432 \u0440\u0443\u043d\u0435\u0442\u0435 \u043d\u0430 \u044d\u0442\u0443 \u0442\u0435\u043c\u0443. \u0421\u0441\u044b\u043b\u043a\u0438 \u043f\u0440\u0438\u0432\u0435\u0434\u0443 \u0432 \u043a\u043e\u043d\u0446\u0435 \u0441\u0442\u0430\u0442\u044c\u0438. \u0412 \u0438\u0442\u043e\u0433\u0435, \u0441\u0442\u0430\u043b \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u044d\u0442\u043e\u0442 \u043f\u043e\u0434\u0445\u043e\u0434 \u0432 \u0441\u0432\u043e\u0438\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445.<\/p>\n<p>  \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430, \u0432\u0441\u043f\u043e\u043c\u043d\u0438\u043c, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 <a href=\"https:\/\/ru.wikipedia.org\/wiki\/Model-View-Presenter\">MVP<\/a>. MVP \u2014 \u044d\u0442\u043e \u043f\u0430\u0442\u0442\u0435\u0440\u043d, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0440\u0430\u0437\u0431\u0438\u0432\u0430\u0442\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043d\u0430 \u0442\u0440\u0438 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u0441\u043b\u043e\u044f (\u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430):   <\/p>\n<ol>\n<li>\u041c\u043e\u0434\u0435\u043b\u044c (Model) \u2014 \u0433\u0434\u0435 \u0441\u043e\u0441\u0440\u0435\u0434\u043e\u0442\u043e\u0447\u0435\u043d\u044b \u0434\u0430\u043d\u043d\u044b\u0435;<\/li>\n<li>\u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 (View) \u2014 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f (UI \u2014 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b);<\/li>\n<li>Presenter \u2014 \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u044b\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0441\u0432\u044f\u0437\u044c \u043c\u0435\u0436\u0434\u0443 \u041c\u043e\u0434\u0435\u043b\u044c\u044e \u0438 \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435\u043c.<\/li>\n<\/ol>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/bc1\/d6a\/4cc\/bc1d6a4cc80a447d94cfade436724c9f.png\" alt=\"image\"\/><\/p>\n<p>  MVP \u043f\u0430\u0442\u0442\u0435\u0440\u043d \u2014 \u044d\u0442\u043e \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a \u0431\u043e\u043b\u0435\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e\u0433\u043e \u0448\u0430\u0431\u043b\u043e\u043d\u0430 \u2014 <a href=\"https:\/\/ru.wikipedia.org\/wiki\/Model-View-Controller\">MVC<\/a>. \u041f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0442\u0430\u043a\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u0442\u0434\u0435\u043b\u0438\u0442\u044c \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u043b\u043e\u0433\u0438\u043a\u0443 \u043e\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-262195","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/262195","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=262195"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/262195\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=262195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=262195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=262195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}