无聊学习一下MVP这个概念
记录一下学习MVP,好处是便于替换前台页面(winfrom替换成asp.net),不改变页面逻辑层及其以后的层
M:业务逻辑
V:页面
P:页面逻辑 ,充当 页面和业务逻辑的中间层
规则:V和M不能直接接触,由P来做中间人
调用流程:V 去调用P,P去调用M,P调用M完成后,P调用V改变页面(这里为了保证P调用V不出错,使用了接口,规定V必须实现的方法)
建立四个库:
1 M :业务逻辑,可以用具体逻辑填充
2 V :页面,有自己的简单必要的代码逻辑,和调用P的代码 和 实现P规定的接口方法
3 P:页面逻辑,接到V的调用的代码,并调用M进行逻辑处理和调用V进行页面反馈
4 Models: 公用的实体类

V的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using P;
using Models; // 该命名空间下 存放页面类
namespace V
{
public partial class Form1 : Form, IPerson
{
private PersonLogic p;
public Form1()
{
InitializeComponent();
p = new PersonLogic(this);// 这行代码将MVP 中的V(页面)和P(页面逻辑)关联起来
} private void Form1_Load(object sender, EventArgs e)
{
p.ShowPersonListData();
} private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
if (e.IsSelected)
{
p.SelectedIndex(e.Item.Text);
}
} #region P要回调的方法 接口的实现 public void ShowMessage(string message)
{
MessageBox.Show(message);
} public void ShowPersonList(List<PersonModel> list)
{
foreach (var item in list)
{
ListViewItem c = listView1.Items.Add(item.Name);
c.SubItems.Add(item.Age.ToString());
}
} public void SelectName(string name, string age)
{
textBox1.Text = name;
textBox2.Text = age;
} #endregion
}
}
P的接口代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Models; namespace P
{
/// <summary>
/// 规定页面中要实现的方法 会在页面逻辑类中调用
/// </summary>
public interface IPerson
{
void ShowMessage(string message); void ShowPersonList(List<PersonModel> list); void SelectName(string name, string age); }
}
P的页面逻辑代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using M;
using Models; // 该命名空间下 存放 页面逻辑
namespace P
{
public class PersonLogic
{
private IPerson view;
PersonSevise sevice = new PersonSevise();
public PersonLogic(IPerson iView)
{
view = iView;
} public void ShowPersonListData()
{
view.ShowPersonList(sevice.list);
} public void SelectedIndex(string name)
{
PersonModel model = sevice.list.Where(M => M.Name == name).FirstOrDefault();
view.SelectName(model.Name, model.Age.ToString());
view.ShowMessage("选中了");
}
}
}
M的逻辑代码 (数据处理,读写数据库):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Models; // 该命名空间下 存放业务逻辑类 实际中 就数据库和代码逻辑
namespace M
{
public class PersonSevise
{
public List<PersonModel> list { get; set; }
public PersonSevise()
{
list = new List<PersonModel>();
list.Add(new PersonModel() { Name = "LXP1", Age = });
list.Add(new PersonModel() { Name = "LXP2", Age = });
list.Add(new PersonModel() { Name = "LXP3", Age = });
list.Add(new PersonModel() { Name = "LXP4", Age = });
list.Add(new PersonModel() { Name = "LXP5", Age = });
list.Add(new PersonModel() { Name = "LXP6", Age = });
list.Add(new PersonModel() { Name = "LXP7", Age = });
list.Add(new PersonModel() { Name = "LXP8", Age = });
list.Add(new PersonModel() { Name = "LXP9", Age = });
list.Add(new PersonModel() { Name = "LXP10", Age = }); } public void Add(PersonModel model)
{
list.Add(model);
}
public void Delete(PersonModel model)
{
list.Remove(list.Where(M => M.Equals(model)).FirstOrDefault());
}
}
}
Models实体类代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; // 该命名空间下存放模型实体类
namespace Models
{
public class PersonModel : IEquatable<PersonModel>
{
public string Name { get; set; }
public int Age { get; set; } public bool Equals(PersonModel other)
{
return this.Name == other.Name;
}
}
}
无聊学习一下MVP这个概念的更多相关文章
- Optaplanner逐步学习(0) : 基本概念 - Optaplanner,规划问题, 约束,方案
之前的文章中,分别从APS,排产到规划引擎叙述了一些理论基础:并介绍了一些Optaplanner大概的情况:并一步步将Optaplanner的示例运行起来,将示例源码导进Eclipse分析了一下它的H ...
- JavaScript:学习笔记(2)——基本概念与数据类型
JavaScript:学习笔记(2)——基本概念与数据类型 语法 1.区分大小写.Test 和 test 是完全不同的两个变量. 2.语句最好以分号结束,也就是说不以分号结束也可以. 变量 1.JS的 ...
- Linux 系统编程 学习:00-有关概念
Linux 系统编程 学习:00-有关概念 背景 系统编程其实就是利用系统中被支持的调度API进行开发的一个过程. 从这一讲开始,我们来介绍有关Linux 系统编程的学习. 知识 在进行Linux系统 ...
- java web学习总结(一) -------------------基本概念
一.基本概念 1.1.WEB开发的相关知识 WEB,在英语中web即表示网页的意思,它用于表示Internet主机上供外界访问的资源. Internet上供外界访问的Web资源分为: 静态web资源( ...
- Python新手学习基础之函数-概念与定义
什么是函数? 函数是可以实现一些特定功能的方法或是程序,简单的理解下函数的概念,就是你编写了一些语句,为了方便使用,把这些语句组合在一起,给它起一个名字,即函数名.使用的时候只要调用这个名字,就可以实 ...
- Elasticserach学习笔记-01基础概念
本文系本人根据官方文档的翻译,能力有限.水平一般,如果对想学习Elasticsearch的朋友有帮助,将是本人的莫大荣幸. 原文出处:https://www.elastic.co/guide/en/e ...
- es6学习笔记-class之一概念
前段时间复习了面向对象这一部分,其中提到在es6之前,Javasript是没有类的概念的,只从es6之后出现了类的概念和继承.于是乎,花时间学习一下class. 简介 JavaScript 语言中,生 ...
- SpringMVC学习(一)——概念、流程图、源码简析
学习资料:开涛的<跟我学SpringMVC.pdf> 众所周知,springMVC是比较常用的web框架,通常整合spring使用.这里抛开spring,单纯的对springMVC做一下总 ...
- Python学习---模版/包的概念
1.1. 模块/包的概念 在Python中,一个.py文件就称之为一个模块(Module) 模块一共三种: python标准库 第三方模块 应用程序自定义模块 模块的使用:模块是用来组织函数的 解释器 ...
随机推荐
- 题解【洛谷P1596】[USACO10OCT]Lake Counting
题面 \(\text{Flood Fill}\) 模板题. \(\text{Flood Fill}\) 可以快速求出一个图中连通块的个数. 大概就是遍历每一个点,如果它没有被遍历过且是一个新连通块,那 ...
- C# ASCII码的转换、转义字符、对照表
var splitStr = new byte[] { 0x05, 0x0D, 0x0A };//var splitStr = new byte[] { 5, 13, 10 };这样写也可以 var ...
- php 获取视频时长
利用ffmpeg只能获取到本地视频的信息. function video_time($file) { ob_start(); passthru(sprintf(FFMPEG_PATH, $file)) ...
- ControlTemplate in WPF
Shared in all file window Button CheckBox Radiobutton Textbox ComboBox ListBox ItemsControl TreeView ...
- Docker学习笔记(2):使用Dockerfile构建镜像
Dockerfile是用来构建Docker镜像的文件,是由一系列命令和参数构成的脚本.每条指令都必须为大写字母且后面要跟随至少一个参数,每条指令都会创建一个新的镜像层,并对镜像进行提交. Docker ...
- gtid环境下mysqldump对于set-gtid-purged的取值
gtid环境备份的时候,还在为set-gtid-purged=0|1的选择而烦恼吗,一起来分析一下. [mysql@lxd-vm1@/home/mysql]$ mysqldump --help | g ...
- OpenGL 编程指南 (5.2)
1.使用纹理 GLSL从纹理中读取数据使用内置函数texture的多种重载方法 Gvec4 texture(gsampler1D, float texCoord[,float bias]) Gvec4 ...
- jQuery使用ajax跨域请求获取数据
jQuery使用ajax跨域请求获取数据 跨域是我在日常面试中经常会问到的问题,这词在前端界出现的频率不低,主要原因还是由于安全限制(同源策略, 即JavaScript或Cookie只能访问同域下的 ...
- MVC理解(全程白话不拽词)
我所发的所有博客只为了给想干程序员,但是基础没有或者很差,刚入职场的人 所有的观点不一定准确,我不懂的或者不理解的都会备注出来 先说MVC MVC理解为:M = Model = 数据库表里面每 ...
- Spring Boot整合Freemarker
一.首先导入依赖 <!-- 添加freemarker模版的依赖 --> <dependency> <groupId>org.springframework. ...