一、简介

随着UI创建技术的功能日益增强,UI层也履行着越来越多的职责。为了更好地细分视图(View)与模型(Model)的功能,让View专注于处理数 据的可视化以及与用户的交互,同时让Model只关系数据的处理,基于MVC概念的MVP(Model-View-Presenter)模式应运而生。

在MVP模式里通常包含4个要素:

    View:负责绘制UI元素、与用户进行交互(在Android中体现为Activity);
    View interface:需要View实现的接口,View通过View interface与Presenter进行交互,降低耦合,方便进行单元测试;
    Model:负责存储、检索、操纵数据(有时也实现一个Model interface用来降低耦合);
    Presenter:作为View与Model交互的中间纽带,处理与用户交互的负责逻辑。
所谓的MVP,即是(model-处理业务逻辑(主要是数据读写,或者与后台通信(其实也是读写数据)),view-处理ui控件,presenter-主导器,操作model和view)

二、实例

1.新建一个项目,项目结构如下

2.做一个根据id读取数据的实例,界面如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入id"
android:inputType="number" />
<EditText
android:id="@+id/first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="inter first name"
android:inputType="text" />
<EditText
android:id="@+id/last"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="inter last name"
android:inputType="text" />
<Button
android:id="@+id/save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="save" />
<Button
android:id="@+id/load"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="load" />
</LinearLayout>

3.建立bean

 public class UserBean
{
private String mFirstName { get; set; }
private String mLastName { get; set; } public UserBean(String firstName, String lastName)
{
this.mFirstName = firstName;
this.mLastName = lastName;
}
}

4.建立model(处理业务逻辑),先写接口,后写实现

    public interface IUserModel
{
void SetID(int id); void SetFirstName(String firstName); void SetLastName(String lastName); int GetID(); UserBean Load(int id);// 通过id读取user信息,返回一个UserBean
}
 public class UserModel : IUserModel
{
public int GetID()
{
return ;
} public UserBean Load(int id)
{
// 查数据库或者联网获取id的userbean
return new UserBean("", "");
} public void SetFirstName(string firstName)
{ } public void SetID(int id)
{ } public void SetLastName(string lastName)
{ }
}

5.建立view(更新ui中view状态),这里列出需要操作当前view的方法,也就是接口。

    public interface IUserView
{
int GetID(); String GetFristName(); String GetLastName(); void SetFirstName(String firstName); void SetLastName(String lastName);
}

6.建立presenter(主导器,通过ivew和imodel接口操作model和view),activity可以把所有逻辑给presenter处理

 public class UserPresenter
{
private IUserView mUserView;
private IUserModel mUserModel; public UserPresenter(IUserView view)
{
mUserView = view;
mUserModel = new UserModel();
} public void SaveUser(int id, String firstName, String lastName)
{
mUserModel.SetID(id);
mUserModel.SetFirstName(firstName);
mUserModel.SetLastName(lastName);
} public void LoadUser(int id)
{
UserBean user = mUserModel.Load(id);
mUserView.SetFirstName(user.mFirstName); // 通过调用IUserView的方法来更新显示
mUserView.SetLastName(user.mLastName);
}
}

7.activity中实现iview接口,在其中操作view,实例化一个presenter变量。

 public class MainActivity : Activity, IUserView, View.IOnClickListener
{
UserPresenter presenter;
EditText id, first, last;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle); // Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main); // Get our button from the layout resource,
// and attach an event to it
FindViewById<Button>(Resource.Id.save).SetOnClickListener(this);
FindViewById<Button>(Resource.Id.load).SetOnClickListener(this); ; id = FindViewById<EditText>(Resource.Id.id);
first = FindViewById<EditText>(Resource.Id.first);
last = FindViewById<EditText>(Resource.Id.last);
presenter = new UserPresenter(this);
} public int GetID()
{
return Convert.ToInt32(id.Text.ToString());
} public string GetFristName()
{
return first.Text.ToString();
} public string GetLastName()
{
return last.Text.ToString();
} public void SetFirstName(string firstName)
{
first.Text = firstName;
} public void SetLastName(string lastName)
{
last.Text = lastName;
} public void OnClick(View v)
{
switch (v.Id)
{
case Resource.Id.save:
presenter.SaveUser(GetID(), GetFristName(), GetLastName());
break;
case Resource.Id.load:
presenter.LoadUser(GetID());
break;
}
}
}

7.源码地址

https://github.com/huguodong/XamarinMVP

里面又更详细的实例

Xamarin.Android MVP模式的更多相关文章

  1. android MVP模式介绍与实战

    android MVP模式介绍与实战 描述 MVP模式是什么?MVP 是从经典的模式MVC演变而来,它们的基本思想有相通的地方:Controller/Presenter负责逻辑的处理,Model提供数 ...

  2. Android MVP模式

    转自http://segmentfault.com/blogs,转载请注明出处Android MVP Pattern Android MVP模式\[1\]也不是什么新鲜的东西了,我在自己的项目里也普遍 ...

  3. Android MVP模式 简单易懂的介绍方式

    主要学习这位大神的博客:简而易懂 Android MVP模式 简单易懂的介绍方式 https://segmentfault.com/a/1190000003927200

  4. Android MVP模式简单易懂的介绍方式 (三)

    Android MVP模式简单易懂的介绍方式 (一) Android MVP模式简单易懂的介绍方式 (二) Android MVP模式简单易懂的介绍方式 (三) 讲完M和P,接下来就要讲V了.View ...

  5. Android MVP模式简单易懂的介绍方式 (二)

    Android MVP模式简单易懂的介绍方式 (一) Android MVP模式简单易懂的介绍方式 (二) Android MVP模式简单易懂的介绍方式 (三) 上一篇文章我们介绍完了Model的创建 ...

  6. Android MVP模式简单易懂的介绍方式 (一)

    Android MVP模式简单易懂的介绍方式 (一) Android MVP模式简单易懂的介绍方式 (二) Android MVP模式简单易懂的介绍方式 (三) 最近正在研究Android的MVP模式 ...

  7. Android MVP模式 谷歌官方代码解读

    Google官方MVP Sample代码解读 关于Android程序的构架, 当前(2016.10)最流行的模式即为MVP模式, Google官方提供了Sample代码来展示这种模式的用法. Repo ...

  8. Android mvp模式、mvvm模式

    MVC和MVP的区别2007年08月08日 星期三 上午 09:23 MVC和MVP到底有什么区别呢? 从这幅图可以看到,我们可以看到在MVC里,View是可以直接访问Model的!从而,View里会 ...

  9. Android MVP模式就是这么回事儿

    MVP模式 概念就不说了,大家都懂.View层通过Persenter层相互通信,避免了View中大量的业务代码,而将其提取到Model中.其实简单的来说,就是通过接口回调,把业务分离出去.提高代码的可 ...

随机推荐

  1. Altera USB Blaster 仿真器(EPM240仿制版

    转载:http://tigerwang202.blogbus.com/logs/35981280.html 其他很好的资料:http://bbs.ednchina.com/BLOG_ARTICLE_1 ...

  2. Team Homework #2 Decide the roles of each team member ——IloveSE

    大家好,我们是IloveSEers! 徐姗,我是一个性格开朗,但却认为计算机比较枯燥的女生.经过两年的学习,自己的编程能力,并不是很强,在这方便还需多多练习.对于软件工程这门课,我充满期待,因为我不仅 ...

  3. 玩耍Hibernate系列(一)--基础知识

    Hibernate框架介绍: Hibernate  ORM  主要用于持久化对象(最常用的框架) Hibernate  Search 用于对对象进行搜索,底层基于Apache Lucene做的 Hib ...

  4. jQuery实现模拟滚动条效果;

    滚动条在web开发中,很常见,原生的HTML滚动条很难看,因此很多网站借助JS来模拟实现滚动条效果: 滚动条的实现原理其实比较简单,拿垂直滚动条来说: 1),最外层容器需要设置overflow:hid ...

  5. Node.js 学习(五)Node.js 事件循环

    Node.js 是单进程单线程应用程序,但是通过事件和回调支持并发,所以性能非常高. Node.js 的每一个 API 都是异步的,并作为一个独立线程运行,使用异步函数调用,并处理并发. Node.j ...

  6. 安装配置OPENCMS的Replication cluster(从)详细过程

    1.  把opencms.war拷贝到tomcat下的webapps目录,启动tomcat服务. 2.  在安装之前,打开解压缩后的war包目录(tomcat启动后会自动把war包解开),删除目录 $ ...

  7. winform - BackgroundWorker

    http://www.cnblogs.com/happy555/archive/2007/11/07/952315.html 在VS2005中添加了BackgroundWorker组件,该组件在多线程 ...

  8. 查看python selenium的api

    打开命令行工具,doc中输入: python -m pydoc -p 然后在浏览器中访问http://localhost:4567/,此时应该可以看到python中所有的Modules 按ctrl+f ...

  9. 【概率DP/高斯消元】BZOJ 2337:[HNOI2011]XOR和路径

    2337: [HNOI2011]XOR和路径 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 682  Solved: 384[Submit][Stat ...

  10. BZOJ1500 维修数列

    AC通道:http://www.lydsy.com/JudgeOnline/problem.php?id=1500 [前言] 据说没打这题就相当于没打过Splay,这题简直就是让你内心崩溃的... 这 ...