基础MVP逻辑架构实现

1.定义Contract,用来管理mvp相关接口,实际项目中如果接口过多,也可以将mvp各职接口进行分包管理。

public interface HomeContract{
	//View
	interface IHomeView{
		public void homeView(String message);
	}
	//presenter
	interface IHomePresenter{	
		public void homePresenter();
	}
	//model-->presenter
	interface IHomeModel{
		void homeModel(IModelListener modelListener);
		interface IModelListener{
			public void onCompletion(String message);		
		}
	}		
}

2.定义基类IBaseModel

public class IBaseModel{
}

3.定义子类Model

public class HomeModel extends IBaseModel implements HomeContract.IHomeModel{
	@Override
	public void homeModel(IModelListener modelListener){
		String data = getCurrentData(BASE_URL);
		//获取数据
		if(modelListener != null){
			modelListener.onCompletion(data);
		}
	}
}

4.定义基类Presenter接口

public interface IBasePresenter<V>{
	void registerView(V view);
	void unRegisterView();
	boolean isViewAttached();
	V getView();
}

5.定义BasePresenter

public class BasePresenter<V> implements IBasePresenter<V>{
	private WeakReference<V> mWeakReference;
	@Override
	public void registerView(V view){
		mWeakReference = new WeakReference<>(view);
	}
	@Override
	public void unRegisterView(){
		if(mWeakReference != null){
			mWeakReference.clear();
			mWeakReference = null;
		}
	}
	@Override
	public boolean isViewAttached(){
		return mWeakReference != null && mWeakReference.get() != null;
	}
	@Override
	public V getView(){
		return mWeakReference != null : mWeakReference.get() : null;
	}
}

6.实际Presenter处理类

public class HomePresenter extends BasePresenter<HomeContract.View> implements HomeContract.IHomePresenter,HomeContract.IModelListener{
	private HomeContract.IHomeModel homeModel;
	public HomePresenter(HomeContract.IHomeModel homeModel){
		this.homeModel = homeModel;
	}
	@Override
	public void homePresenter(){
		homeModel.homeModel(this);
	}
	@Override
	public void onCompletion(String message){
		if(isViewAttached()){
			getView().homeView(message);				
		}
	}
}

7.定义IBaseView

public interface IBaseView{
	public Context getViewContext();
	public void bindView();
	public void unBindView();
}

8.定义BaseActivity中实现Presenter相关方法

public abstract class BaseActivity<P extends BasePresenter> extends Activity implements IBaseView{
    protected P mPresenter;	
	protected Context mContext;
	@Override
	public void onCreate(Bundle saveData){
		mContext = this;
		bindView();
	}
	@Override
	public void getViewContext(){
		return mContext;
	}
	@Override
	public void bindView(){
        mPresenter = createPresenter();
		if(mPresenter != null){
			mPresenter.registerView(this);
		}
	}
	public abstract P createPresenter();
	@Override
	public void unBindView(){
		if(mPresenter != null){
			mPresenter.unRegisterView(this);
		}
	}
	@Override
	public void onDestroy(){
		unBindView();
	}
	
}

9. 定义具体View(Activity)

public class HomeActivity extends BaseActivity<HomePresenter> implements HomeContract.IHomeView{
	@Override
	protected void initView() {
		setContentView(R.layout.activity_mvp);
	}

	@Override
	public HomePresenter createPresenter(){
		return new HomePresenter();
	}

	@Override
	protected void initData() {
		if (mPresenter != null){
		    mPresenter.mainPresenter();
		}
	}
	@Override
	public void homeView(String message){
		Log.d("mvp_log","[message]---"+message);
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值