标签:
MVP(Model / View / Presenter) 是从经典的模式MVC演变而来,Presenter代替activity和fragment成为控制器,而activity和fragment专心做View层该做的事。
2、Presenter可以重复利用了。
3、如果我们把逻辑放在Presenter中,单元测试更简单了
public interface ICard { void onStartLoading();//开始加载 void onLoading();//加载中 void onLoadingDone(Card card);//加载成功 }
public class Card { public Card(String name, String tel) { this.name = name; this.tel = tel; } public String name; public String tel; public String getName() { return name; } public String getTel() { return tel; } public String toString() { return " name:" + name + " tel:" + tel; } }
public class CardParsenter { private String TAG = "CardParsenter"; private ICard iCard; public CardParsenter(ICard iCard) { this.iCard = iCard; } public void getCard() { iCard.onLoadingDone(new Card("小明", "12345678")); } public void setCard(Card card) { Log.i(TAG, card.toString()); } }
public class UserActivity extends AppCompatActivity implements ICard { public CardParsenter cardParsenter; public EditText nameET, telET; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); cardParsenter = new CardParsenter(this); nameET = (EditText) findViewById(R.id.nameET); telET = (EditText) findViewById(R.id.telET); } public void click(View view) { switch (view.getId()) { case R.id.setBT: cardParsenter.setCard(new Card(nameET.getText().toString(), telET.getText().toString())); break; case R.id.getBT: cardParsenter.getCard(); break; } } @Override public void onStartLoading() { } @Override public void onLoading() { } @Override public void onLoadingDone(Card card) { nameET.setText(card.getName()); telET.setText(card.getTel()); } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context=".view.UserActivity" tools:showIn="@layout/activity_main"> <EditText android:id="@+id/nameET" android:layout_width="250dp" android:layout_height="wrap_content" android:hint="nameET" android:textSize="20sp" /> <EditText android:id="@+id/telET" android:layout_width="250dp" android:layout_height="wrap_content" android:layout_below="@+id/nameET" android:hint="tel" android:textSize="20sp" /> <Button android:id="@+id/setBT" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/telET" android:onClick="click" android:text="set" android:textSize="20sp" /> <Button android:id="@+id/getBT" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/telET" android:layout_toEndOf="@+id/setBT" android:onClick="click" android:text="get" android:textSize="20sp" /> </RelativeLayout>
MVC有个缺点就是View层和model层耦合在一起,造成了Activity做控制的同时也做View层的事(比如设置监听,设置动画、初始化控件等...)。MVP在MVC的基础上分离了View层和model层,然后用parsenter控制模型,得到结果后通知View刷新UI,从而避免耦合。
代码地址:http://download.csdn.net/detail/s4336723/9515111
一个MVP模式的框架:https://github.com/sockeqwe/mosby
标签:
原文地址:http://blog.csdn.net/s4336723/article/details/51351662