一、简介

随着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. ASP.NET中Global.asax 文件是什么?

    Global.asax 文件,有时候叫做 ASP.NET 应用程序文件,提供了一种在一个中心位置响应应用程序级或模块级事件的方法.你可以使用这个文件实现应用程序安全性以及其它一些任务.下面让我们详细看 ...

  2. Mono for Android (4)-- 图片转为二进制,二进制转回图片

    最近纠结蓝牙打印的问题,想着图片先转为二进制发给打印机,找了好多资料,终于成功了,贴出来共享一下 先是图片转换为二进制的: Bitmap bitmap = BitmapFactory.DecodeRe ...

  3. JQuery 对 Select option 的操作---转载

    <select id="selectID" > <option value="1">1</option> <optio ...

  4. "渴了么"用户场景分析

    典型用户 (1)名字:王美丽 (2)年龄:21 (3)收入:勤工助学和兼职等 (4)代表的用户在市场上的比例和重要性(比例大不等同于重要性高,如付费的用户比例较少,但是影响大,所以更重要). 作为大学 ...

  5. js 完成对图片的等比例缩放的方法

    /* 重新按比例设置 页面上对应图片的长和高, */ function resetImgSize(id,imgWidth,imgHeight,posWidth,posHeight) { var wid ...

  6. 揭开NodeJS的神秘面纱!

    一.NodeJS是什么? Node是一个服务器端JavaScript解释器.Node.js是一套用来编写高性能网络服务器的JavaScript包. 二.Node的目标是什么? Node 公开宣称的目标 ...

  7. 通过HTML5实现发送声音

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  8. 【Construct Binary Tree from Inorder and Postorder Traversal】cpp

    题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume ...

  9. 单点登录的原理与CAS技术的研究

    1.什么是单点登录? 关于单点登录技术的说明参考文章:http://www.cnblogs.com/yupeng/archive/2012/05/24/2517317.html 一般来说,整个原理大家 ...

  10. BZOJ 1012: [JSOI2008]最大数maxnumber 线段树

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1012 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作.语法:Q L 功能: ...