Android MVP模式中,一个Activity对应多个Presenter时该怎样抽象?
阿神
阿神 2017-04-18 09:14:49
0
3
1344

这是网上常见的MvpActivity基类

public abstract class BaseMvpActivity<t extends BaseAdapter> extends AppCompatActivity {   
 
    public T presenter;    
 
     @Override  
     protected void onCreate(Bundle savedInstanceState) {        
         super.onCreate(savedInstanceState);        
         presenter = initPresenter();   
     }    
 
     @Override   
     protected void onResume() {        
          super.onResume();        
          presenter.attach((V)this);    
     }    
 
     @Override   
     protected void onDestroy() {       
        presenter.dettach();        
        super.onDestroy();    
     }   
 
     public abstract T initPresenter();
 
}

这个基类完成了presenter的实例化、presenter持有的Activity对象的释放.
那么当一个Activity有多个功能时,应该怎样实现呢。

我想,一种方法是让一个presenter实现多个功能,这时可以继续用上面的基类,但是程序的功能不容易复用。比如Activity1有功能A和B,Activity2有功能C和D,当Activity3有功能A和D时,无法复用Activity1和2的presenter; 一种方法是一个Activity持有多个presenter,这时就要重写BaseActivity,我写了一个,如下:

public abstract class MvpActivity extends BaseActivity {
    protected Map<String, BasePresenter> presenters;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        presenters = createPresenter();
        super.onCreate(savedInstanceState);
    }

    protected abstract Map<String, BasePresenter> createPresenter();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        for (Map.Entry<String, BasePresenter> entry : presenters.entrySet()) {
            if (entry.getValue() != null) {
                entry.getValue().detachView();
            }
        }
    }

    protected BasePresenter getPresenter(){
        return presenters.entrySet().iterator().next().getValue();
    }

    protected BasePresenter getPresenter(String key){
        return presenters.get(key);
    }
}

但是这个基类使用起来很别扭,说他别扭是因为:
在第一版中有public T presenter这个变量,可以直接调用presenter的功能,第二版中首先要调用getPresenter,还要类型转换才能使用。还有在createPresenter抽象方法实现起来不想第一版那么直观。

我JAVA功力不够,还请各位指点一下。

阿神
阿神

闭关修行中......

reply all(3)
PHPzhong

An activity corresponds to a presenter A, and then the corresponding presenter A can be expanded. It is only used as a proxy class, and other specific implementations are implemented by other presenters B, C, and D. This way, there is no need to change the call of the activity. In this way, you can also reuse the presenter, but it feels weird. . .

左手右手慢动作

There are protocol constraints between View and Presenter.
Assume that there are existing V1, P1, V2, P2, and an ActivityA corresponds to the functions of P1/P2. It should also indicate that it implements the V1/V2 interface. At this time, there should be PresenterA P1, P2 are implemented. This is the complete VP protocol.
Since multiple pages may use the same function, the simplest way is to use multiple inheritance on my node (one Persenter implements the four ABCD interfaces), In this way, regardless of the Activity, the same Presenter is actually used. If you are sure that it has been implemented now, just implement a proxy according to the method above.

If you really want to implement multiple Presenters, you can define several different classes to support the extension of different multiple Presenters:

class BaseMvpActivity1<P> extent BaseActivity {
    P presenter;
}
class BaseMvpActivity2<P1, P2> extent BaseActivity {
    P1 presenter1;
    P2 presenter2;
}

But this is not recommended (losing abstraction and violating the principle of dependency inversion); it is also not recommended to use arrays or Lists (the base class uses generics to facilitate compiler checking, and arrays or Lists need to be typed when used specifically)

大家讲道理

http://blog.csdn.net/github_3... There is an introduction inside

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template