• Android-----关于泛型CONTRACT的使用


    /**
     * 整个架构的父类、超级父类
     * Created by Administrator on 2020/8/12.
     */
    
    public abstract class SuperBase<CONTRACT> {
    
        public abstract CONTRACT getContract(); //子类必须实现方法,具体实现哪些看传过来的接口中有哪些方法
    }

    子类使用时:

    /**
     * 拿到P层,把结果给P层
     * 将由子类传 CONTRACT 到父类中
     * Created by Administrator on 2020/8/12.
     */
    
    public abstract class BaseModel<P extends BasePresenter , CONTRACT> extends SuperBase<CONTRACT> {
    
        public P presenter;
    
        public BaseModel(P mPresenter){
            this.presenter = mPresenter;    //拿到P层
        }
    
    }

    继承自BaseModel的子类要实现方法:

    /** BaseModel的子类继承后要传递的接口为:ActivityMvpContrast.Model
     *  所以要实现父类中的抽象方法:public abstract CONTRACT getContract()
     *  CONTRACT getContract()中的具体实现为:ActivityMvpContrast.Model
     * Created by Administrator on 2020/8/12.
     */
    
    public class ActivityMvpModel extends BaseModel<ActivityMvpPresenter,ActivityMvpContrast.Model>{
    
    
        public ActivityMvpModel(ActivityMvpPresenter mPresenter) {
            super(mPresenter);
        }
    
       /**【返回契约接口】**/
        @Override
        public ActivityMvpContrast.Model getContract() {
            return new ActivityMvpContrast.Model() {
                @Override
                public void validateLogin(String userName, String passWord) throws Exception {
    
                }
            };
        }
    }

    而传递进去的:ActivityMvpContrast.Model

    public interface ActivityMvpContrast {
    
        interface Model{
    
            //登录验证
            void validateLogin(String userName , String passWord) throws Exception;
    
        }
    
        interface ViewAmdPresenter{
    
            void requestLogin(String userName , String passWord);
    
            void responseResult(String msg);
    
        }
    
    
    }

    另外一种理解方式:CONTRACT 作为一个参数传进去,而子类则要实现抽象方法 getContract() ,返回的类型为 CONTRACT

    package com.mvp.org.Base;
    
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    
    /**
     * 只关注P层
     * Created by Administrator on 2020/8/12.
     */
    
    public abstract class BaseActivity<P extends BasePresenter ,CONTRACT> extends AppCompatActivity implements View.OnClickListener{
    
        public abstract CONTRACT getContract(); ///////
    
        public P mPresenter;
    
        public abstract P createPresenter();//实例化P层
    
        public abstract int getViewID();    //拿到布局视图
    
        public abstract void initViewUI();  //初始化组件
    
        public abstract void initListener();//初始化监听
    
        public abstract void destroy();     //销毁
    
        public abstract void initDatas();   //初始化数据
    
        //处理相应错误信息
        public abstract<ERROR extends Object> void responseError(ERROR error , Throwable throwable);
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(getViewID());
            initViewUI();
            initListener();
            initDatas();
            this.mPresenter = createPresenter();      //实例化P层
            mPresenter.attach(this);            //绑定
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            destroy();
            mPresenter.detach();                    //解绑
        }
    }

    子类继承父类中具体实现:

    package com.mvp.org.BaseDemo;
    
    import android.view.View;
    
    import com.beardedhen.androidbootstrap.BootstrapButton;
    import com.beardedhen.androidbootstrap.BootstrapEditText;
    import com.mvp.org.Base.BaseActivity;
    import com.mvp.org.R;
    import com.mvp.org.Utils.AlertDialogUtils;
    
    public class ActivityMvp extends BaseActivity<ActivityMvpPresenter,ActivityMvpContrast.ViewAmdPresenter>
            implements ActivityMvpContrast.ViewAmdPresenter{
    
        private BootstrapEditText userName , passWord;
        private BootstrapButton login , exit;
    
        private AlertDialogUtils utils;
    
        @Override
        public ActivityMvpContrast.ViewAmdPresenter getContract() {
            return new ActivityMvpContrast.ViewAmdPresenter() {
                @Override
                public void requestLogin(String userName, String passWord) {
    
                }
    
                @Override
                public void responseResult(String msg) {
    
                }
            };
        }
    
        @Override
        public ActivityMvpPresenter createPresenter() {
            return new ActivityMvpPresenter();
        }
    
        @Override
        public int getViewID() {
            return R.layout.activity_mvp;
        }
    
        @Override
        public void initViewUI() {
            userName = findViewById(R.id.EditTextUserName);
            passWord = findViewById(R.id.EditTextPassWord);
            login = findViewById(R.id.login);
            exit = findViewById(R.id.exit);
            utils = new AlertDialogUtils(this);
        }
    
        @Override
        public void initListener() {
            login.setOnClickListener(this);
            exit.setOnClickListener(this);
        }
    
        @Override
        public void destroy() {
    
        }
    
        @Override
        public void initDatas() {
            userName.setText("Admin");
            passWord.setText("Admins");
        }
    
        @Override
        public <ERROR> void responseError(ERROR error, Throwable throwable) {
            utils.AlertTitle("" + error,"error");
        }
    
    
        @Override
        public void onClick(View view) {
            String username = userName.getText().toString().trim();
            String password = passWord.getText().toString().trim();
            switch (view.getId()){
                case R.id.login:
    
                    requestLogin(username,password);
                    break;
                case R.id.exit:
                    utils.AlertTitleAndMessage(username,password,"warning");
                    break;
            }
        }
    
        @Override
        public void requestLogin(String userName, String passWord) {
            mPresenter.requestLogin(userName,passWord);     //直接从架构中拿到P层,V请求P层
        }
    
    
        @Override
        public void responseResult(String msg) {
            utils.AlertTitle(msg,"success");
        }
    }
  • 相关阅读:
    关于struts2的checkboxlist、select等标签发生could not be resolved as a collection/array/map/enumeration/iterator type异常的记录
    ajax Session失效如何跳转到登录页面
    将默认首页设置成index.do的方法
    tomcat应用转到weblogic上时的问题
    MyEclipse 8.5整合Git,并在Github上发布项目(转)
    C++ 类设计核查表
    编程书籍
    并发模型
    C++ AOP手法
    编程 网上资源平台
  • 原文地址:https://www.cnblogs.com/xiobai/p/13490978.html
Copyright © 2020-2023  润新知