MVP模式(一)

**本文僅為個(gè)人在學(xué)習(xí)mvp模式中的一點(diǎn)個(gè)人見(jiàn)解

M:
model,實(shí)體層,此處不是傳統(tǒng)的實(shí)體,我們需要把實(shí)體視圖化,可以理解為Viewmodel,例如一個(gè)登陸視圖,含有用戶名、密碼,我們需要把model視圖化,并綁定到視圖上(我理解為該model的行為,如登陸、修改密碼)
V:
view,視圖層對(duì)應(yīng)xml,Activity,Fragment
P:
Presenter,邏輯控制層,同時(shí)持有model與view對(duì)象

*采用MVP模式的優(yōu)勢(shì)是:
把業(yè)務(wù)邏輯抽離到Presenter層中,View層專注于UI的處理。
分離視圖邏輯與業(yè)務(wù)邏輯,達(dá)到解耦的目的。
提高代碼的閱讀性。
Presenter被抽象成接口,可以根據(jù)Presenter的實(shí)現(xiàn)方式進(jìn)行單元測(cè)試。
可拓展性強(qiáng)。

*采用MVP模式的缺點(diǎn):
項(xiàng)目結(jié)構(gòu)會(huì)對(duì)后期的開(kāi)發(fā)和維護(hù)有一定的影響。具體視APP的體量而定。
代碼量會(huì)增多,如何避免編寫(xiě)過(guò)多功能相似的重復(fù)代碼是使用MVP開(kāi)發(fā)的一個(gè)重點(diǎn)要處理的問(wèn)題。
有一定的學(xué)習(xí)成本。

以用戶登陸為例:

model層的具體代碼

提供想要展示在view層的數(shù)據(jù)和業(yè)務(wù)邏輯實(shí)現(xiàn)

一、創(chuàng)建用戶model

  public class User {

    public String account;
    public String pwd;
}

二、把model視圖化

創(chuàng)建user的行為接口

public interface UserApi {

    interface LoginListener{
        void onSuccess();
        void onFail();
    }

    void login(User user, LoginListener listener);
}

三、實(shí)現(xiàn)行為接口

  public class UserImpl implements UserApi{


    @Override
    public void login(final User user, final LoginListener listener) {
        //模擬登陸
        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                if (TextUtils.isEmpty(user.account)||TextUtils.isEmpty(user.pwd)){
                    return;
                }
                int i =new Random().nextInt(1);
                if (i==0){
                    listener.onSuccess();
                }else {
                    listener.onFail();
                }

            }
        },2000L);
    }
}

View層的具體代碼

負(fù)責(zé)數(shù)據(jù)顯示、題提供用戶交互;設(shè)置監(jiān)聽(tīng)后把工作叫個(gè)Presenter,因此持有Presenter, 調(diào)用Presenter中的相關(guān)方法即可。

public class UserViewApi {

    public interface Login{
        void starLogin();
        void loginEnd();
        void loginSuccess();
        void loginFail();
    }

}

public class LoginActivity extends Activity implements UserViewApi.Login {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new UserPresenter(this,new UserImpl()).loginToServer(new User());
    }

    @Override
    public void starLogin() {

    }

    @Override
    public void loginEnd() {

    }

    @Override
    public void loginSuccess() {

    }

    @Override
    public void loginFail() {

    }
}

Presenter層具體代碼

Presenter在view與model中間層,獲得model數(shù)據(jù)后構(gòu)建到view,可以決定view層的各種操作

public interface UserPresenterApi {

    void loginToServer(User user);

}
public class UserPresenter implements UserPresenterApi, UserApi.LoginListener {

    private UserViewApi.Login viewApi;
    private UserImpl userImpl;

    public UserPresenter(UserViewApi.Login viewApi, UserImpl user){
        this.viewApi = viewApi;
        this.userImpl = user;
    }

    @Override
    public void loginToServer(User user) {
        viewApi.starLogin();
        userImpl.login(user,this);
    }

    @Override
    public void onSuccess() {
        viewApi.loginEnd();
        viewApi.loginSuccess();
    }

    @Override
    public void onFail() {
        viewApi.loginEnd();
        viewApi.loginFail();
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容