MVP設(shè)計(jì)架構(gòu)+生命周期封裝防止內(nèi)存泄漏

1 MVP模式的三個(gè)角色

1 .Persenter-交互的中間人

主要作為View和model的橋梁,負(fù)責(zé)書寫一些業(yè)務(wù)邏輯和一些網(wǎng)絡(luò)通信請求通過model來拿到數(shù)據(jù),返回給View顯示,這樣就可以完全的把View和model層分開

2.View-用戶界面

通常指的是Activity 和 fragment或者 某個(gè)view,他具有一個(gè) Presenter的成員變量。通常需要他去實(shí)現(xiàn)一個(gè)view的邏輯接口,將view的上一些操作轉(zhuǎn)給Presenter來實(shí)現(xiàn),最后Presenter將結(jié)果返回 戰(zhàn)士在view的上面。

3.Model-數(shù)據(jù)的存取

其實(shí)就是網(wǎng)路獲取數(shù)據(jù)或者本地化數(shù)據(jù)存儲(chǔ)的一個(gè)方式操作,(可以簡單的理解為網(wǎng)絡(luò)請求,只要是數(shù)據(jù)的操作都放著里面沒毛?。?/p>

2 下面來做一個(gè)簡單的獲取圖片鏈接來加載到Imageview上的操作

1定義一個(gè) Presenter

public class MainPresenter {

   //view的接口 角色
MainViewInterface mainViewInterfaceView;
  //數(shù)據(jù)接口校色
TextModel textModel = new TextModel();

public MainPresenter(MainViewInterface mainViewInterface) {
    mainViewInterfaceView = mainViewInterface;
}

;

public void getImageurl() {

    textModel.getUrl();
    mainViewInterfaceView.loadImageview(textModel.getUrl()); 
    mainViewInterfaceView.toast();

}

}
該P(yáng)resenter持有了MainViewInterface的引用和TextModel的引用,MainViewInterface為主界面的邏輯接口 比如顯示數(shù)據(jù) 彈出toast等

2 接口代碼如下:
public interface MainViewInterface {

//加載圖片
public void loadImageview(String url);

//線視提示
public void toast();

}
一些邏輯,你可以自行擴(kuò)展 比如加載dialog 或者一些其他操作,都可以定義為接口可以自行返回?cái)?shù)據(jù)或者不返回

3.Activity實(shí)現(xiàn)過程

public class MainActivity extends Activity implements MainViewInterface {

private LinearLayout layout;
private ImageView imageView;
private Button button;
private Context mConetext;
MainPresenter mainPresenter ;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //實(shí)例化 Presentrer
    mainPresenter = new MainPresenter(this);
    mConetext = this;
    imageView = findViewById(R.id.imga_pic);
    button = findViewById(R.id.clearbu);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mainPresenter.getImageurl();
        }
    });
}


@Override
public void loadImageview(String url) {
    Glide.with(mConetext).load(url).into(imageView);
}

@Override
public void toast() {
    Toast.makeText(mConetext, "666", Toast.LENGTH_SHORT).show();
}

}

該Acticity實(shí)現(xiàn)了主界面的邏輯接口,Presenter正好處理數(shù)據(jù) 而數(shù)據(jù)的展示剛好就交給Activity,獲取數(shù)據(jù)正好就交給Model層來進(jìn)行獲取,到此為止MVP模式基本成型

2 關(guān)于Activity和fragment的的生命周期問題

1 由于Presemnter經(jīng)常要做一些網(wǎng)絡(luò)請求數(shù)據(jù)操作耗時(shí)操作,如果耗時(shí)操作在結(jié)束之前 activity被銷毀了,請求未返回導(dǎo)致Presenter一直持有Activity的對象 導(dǎo)致無法回收,造成內(nèi)存泄漏為了解決這個(gè)問題 可以采用弱引用。建立一個(gè)BasePresenter代碼如下:
public abstract class BasePresenter<T> {

//view 的 接口類型的 弱引用
protected Reference<T> mVieRef;

//建立 關(guān)聯(lián)
public void attachView(T view) {
    mVieRef = new WeakReference<T>(view);
}

protected T getView() {
    return mVieRef.get();
}

public boolean isViewAttached() {
    return mVieRef != null && mVieRef.get() != null;
}


public void detachView() {
    if (mVieRef != null) {
        mVieRef.clear();
        mVieRef = null;
    }

}

}

定義自己的Presenter來繼承這ta,繼承的時(shí)候傳遞自己定義的ViewInterface接口 來實(shí)現(xiàn)綁定

2 建立MvpBaseActivity基類:

public abstract class MVPBaseActivity<V, T extends BasePresenter<V>> extends Activity {

protected T mPresenter;

@SuppressWarnings("unchecked")
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    mPresenter = createPresenter();
    mPresenter.attachView((V) this);

}

@Override
protected void onDestroy() {
    super.onDestroy();
    if (mPresenter.isViewAttached()) {
        mPresenter.detachView();
    }

}

protected abstract T createPresenter();

}

在destory生命周進(jìn)行判斷 解除關(guān)聯(lián),避免內(nèi)存泄漏

實(shí)際應(yīng)用
public class MainActivity extends MVPBaseActivity<MainViewInterface, MainPresenter> implements MainViewInterface {

private LinearLayout layout;
private ImageView imageView;

private Button button;
private Context mConetext;
MainPresenter mainPresenter = new MainPresenter(this);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mConetext = this;
    imageView = findViewById(R.id.imga_pic);
    button = findViewById(R.id.clearbu);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mainPresenter.getImageurl();
        }
    });
}

@Override
protected MainPresenter createPresenter() {
    return mainPresenter;

}


@Override
public void loadImageview(String url) {
    Glide.with(mConetext).load(url).into(imageView);
}

@Override
public void toast() {
    Toast.makeText(mConetext, "666", Toast.LENGTH_SHORT).show();
}

}

是不是非常簡單 沒有你想的那么復(fù)雜,記住一句話 解耦 分層,Presenter用來中轉(zhuǎn)數(shù)據(jù) 各司其職就好。補(bǔ)充一下Model的獲取數(shù)據(jù)代碼 ,模擬一下網(wǎng)絡(luò)數(shù)據(jù),請?jiān)试S我偷個(gè)懶。哈哈哈
public class TextModel {

//獲取網(wǎng)絡(luò)數(shù)據(jù)
public String getUrl() {
    //模擬網(wǎng)絡(luò)請求
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return "http://www.hao4k.com/data/attachment/forum/201705/27/154118c85uw88wwsmwajoj.jpg";
}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容