随手撸个发出来:
V:界面层
//界面层需要实现P.View方法,然后重写P.View中的方法;M层给的数据就在这些个方法的参数中;
// 还要获取到P.Provide的实例,使用P.Provide去调用M层的方法;
public class ActivityDemo extends AppCompatActivity implements DemoP.View {private DemoP.Provide provide;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_demo);new DemoM(this);provide.start();}@Overridepublic void showMessage(String msg) {Log.i("mvp", "showMessage: "+msg);}@Overridepublic void Provide(DemoP.Provide m) {provide = m;}}
P:连接层
public interface DemoP {//提供界面interface View{void showMessage(String msg);void Provide(DemoP.Provide m);}//提供信息interface Provide{void start();}
}
M:数据层
// 数据层需要实现P.Provide,然后重写P.Provide;V层就要调用你的这些方法;
// 还要获取到P.View的实例,使用P.View把你处理好的数据返回给V层;
public class DemoM implements DemoP.Provide{private DemoP.View view;public DemoM(DemoP.View v){view = v;view.Provide(this);}@Overridepublic void start() {StringBuffer ss = new StringBuffer();for (int i = 0; i < 10; i++) {ss.append("\n--"+i);}view.showMessage(ss.toString());}
}
如果刚接触MVP看着有疑惑,就照着撸一遍,大致就能懂了;
还可以看看封装一下的:MVP案例