整理契约mvp+总结

public class MainActivity extends AppCompatActivity implements IContract.IView {

    private IContract.IPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        presenter = new PresenterImpl();
        presenter.attachView(this);
    }

    @Override
    public void requestDataPre(final String ss) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
               // Toast.makeText(MainActivity.this, ss, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void requestDataPre1(final String ss1) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
               Toast.makeText(MainActivity.this, ss1, Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void login(View view) {
       // presenter.requestDataPre();
           presenter.requestDataPre1();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.detachView(this);
    }
}

 

 

 

 

public interface IContract {
    interface IModel {

        interface ResponseStr{
            void sendMMsg(String ss);
            void sendMMsg1(String ss1);
        }

        void requestDataMod(ResponseStr responseStr);
        void requestDataMod1(ResponseStr responseStr);
    }

    interface IPresenter<IView> {

        void detachView(IView iView);

        void attachView(IView iView);

        void requestDataPre();
        void requestDataPre1();
    }

    interface IView {
        void requestDataPre(String ss);
        void requestDataPre1(String ss1);
    }
}

 

 

 

public class ModelImpl implements IContract.IModel {

    @Override
    public void requestDataMod(final ResponseStr responseStr) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                    String ss="hahaha";
                    responseStr.sendMMsg(ss);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }

    @Override
    public void requestDataMod1(final ResponseStr responseStr1) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                    String ss1="哈哈";
                    responseStr1.sendMMsg1(ss1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

}

 

 

public class PresenterImpl implements IContract.IPresenter<IContract.IView> {

    IContract.IView iView;
    private IContract.IModel model;
    private WeakReference<IContract.IView> iViewWeakReference;
    private WeakReference<IContract.IModel> iModelWeakReference;

    @Override
    public void attachView(IContract.IView iView) {
        this.iView = iView;
        model = new ModelImpl();
        iViewWeakReference = new WeakReference<>(iView);
        iModelWeakReference = new WeakReference<>(model);
    }

    @Override
    public void detachView(IContract.IView iView) {
        iViewWeakReference.clear();
        iModelWeakReference.clear();
    }

    @Override
    public void requestDataPre() {
        model.requestDataMod(new IContract.IModel.ResponseStr() {
            @Override
            public void sendMMsg(String ss) {
                Log.d("TAG","开过ss");
                iView.requestDataPre(ss);

            }

            @Override
            public void sendMMsg1(String ss1) {
                Log.d("TAG","开过ss1");
                 iView.requestDataPre1(ss1);
            }

        });
    }

    @Override
    public void requestDataPre1() {
        model.requestDataMod1(new IContract.IModel.ResponseStr() {
            @Override
            public void sendMMsg(String ss) {

            }

            @Override
            public void sendMMsg1(String ss1) {

                iView.requestDataPre1(ss1);

            }
        });
    }

}

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦想不上班

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值