MVP简单的写法

M层
public interface IMedel {
    void onGetData(OnCompleted onCompleted);
    interface  OnCompleted{
        void onCompletedListnenr(String s);
    }
}
 
  

public class ModelImp implements  IMedel {
    @Override
    public void onGetData(OnCompleted onCompleted) {
        onCompleted.onCompletedListnenr("123456789");
    }
}
P层


public class MyPresenter  {
   IMedel iMedel= new ModelImp();
     private IView iView;

    public MyPresenter(IView iView) {
        this.iView = iView;
    }
    public  void  ImyP(){
        iMedel.onGetData(new IMedel.OnCompleted() {
            @Override
            public void onCompletedListnenr(String s) {
                iView.myView(s);
            }
        });
    }
    public  void  Isf(){
        iView=null;
    }

}

V层

public interface IView {

    void myView(String s);

}


调用更新Ui

public class MainActivity extends AppCompatActivity implements IView {

    private MyPresenter myPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myPresenter = new MyPresenter(this);
    myPresenter.ImyP();
    }

    @Override
    public void myView(String s) {
        Toast.makeText(this,""+s,Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        myPresenter.Isf();
    }
}




Moudle不带接口的写法

M层

public class IMoudle {
    public  String  myM(){
         return "123456789";
    }
}


P层

public class Ipresenter {
    IMoudle moudle=new IMoudle();
    Iview iview;

    public Ipresenter(Iview iview) {
        this.iview = iview;
    }

    public  void  ImyP(){
          String s = moudle.myM();
        iview.myView(s);
      }
    public  void  Isf(){
        iview=null;
    }

}



V层

public interface Iview {
    void myView(String s);
}
 
  

 
  
去掉用
public class MainActivity extends AppCompatActivity implements Iview {

    private Ipresenter ipresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ipresenter = new Ipresenter(this);
        ipresenter.ImyP();

    }



    @Override
    public void myView(String s) {
        Toast.makeText(MainActivity.this,""+s,Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ipresenter.Isf();
    }
}



你可能感兴趣的:(咨询项目)