• 无聊学习一下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 = 18 });
                list.Add(new PersonModel() { Name = "LXP2", Age = 19 });
                list.Add(new PersonModel() { Name = "LXP3", Age = 20 });
                list.Add(new PersonModel() { Name = "LXP4", Age = 21 });
                list.Add(new PersonModel() { Name = "LXP5", Age = 22 });
                list.Add(new PersonModel() { Name = "LXP6", Age = 23 });
                list.Add(new PersonModel() { Name = "LXP7", Age = 24 });
                list.Add(new PersonModel() { Name = "LXP8", Age = 25 });
                list.Add(new PersonModel() { Name = "LXP9", Age = 26 });
                list.Add(new PersonModel() { Name = "LXP10", Age = 27 });
               
            }
    
            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;
            }
        }
    }
  • 相关阅读:
    《人月神话》阅读笔记(三)
    记账软件开发进度(六)
    记账软件开发进度(五)
    记账软件开发进度(四)
    《人月神话》阅读笔记(二)
    记账软件开发进度(三)
    package bufio
    Go语言:net/http包的使用模式和源码解析
    package http
    Golang系列中常用包
  • 原文地址:https://www.cnblogs.com/lixipeng/p/11908796.html
Copyright © 2020-2023  润新知