MVC設計模式:網站舉例
User類:M是對數據庫的操作(將控制器傳來的數據與數據庫進行對比)。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MVC設計模式demo
{
/// <summary>
/// 數據實體類
/// </summary>
class User
{
int age;
string name;
string address;
public int Age
{
get { return age; }
set { age = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public string Address
{
get { return address; }
set { address = value; }
}
public User(int age,string name,string add)
{
this.age = age;
this.name = name;
this.address = add;
}
public override string ToString()
{
return string.Format("name:{0}--age:{1}--address:{2}",name,age,address);//使用佔位符
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MVC設計模式demo
{
/// <summary>
/// C層,控制器層,處理用戶邏輯
/// </summary>
class UserManager
{
//用戶對象集合
List<User> list1 = new List<User>();
//增加數據
//public void AddItem(int age,string name,string add)
//{
// list1.Add(new User(age,name,add));
//}
public void AddItem(User user)
{
list1.Add(user);
}
//刪除數據
/// <summary>
/// 通過名字刪除
/// </summary>
/// <param name="name"></param>
public void RemoveByName(string name)
{
for(int i=0;i<list1.Count;i++)
{
if (list1[i].Name == name)
list1.Remove(list1[i]);
}
}
/// <summary>
/// 通過地址刪除
/// </summary>
/// <param name="add"></param>
public void RemoveByAddress(string add)
{
for (int i = 0; i < list1.Count; i++)
{
if (list1[i].Address == add)
list1.Remove(list1[i]);
}
}
//修改數據
public void Change(string name,int inforNum,string change)
{
for (int i = 0; i < list1.Count; i++)
{
if (list1[i].Name == name)
{
if (inforNum == 1)
list1[i].Name = change;
else if (inforNum == 2)
list1[i].Age = int.Parse(change);
else if (inforNum == 3)
list1[i].Address = change;
}
}
}
//查詢數據
public void Show()
{
for (int i = 0; i < list1.Count; i++)
{
Console.WriteLine(list1[i].ToString());
}
}
}
}
主函數類:V是視圖層(view),即是UI界面,用於與用戶進行的交互。(有按鈕,有文字,圖像等)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MVC設計模式demo
{
/// <summary>
/// view層和用戶進行交互
/// </summary>
class Program
{
static void Main(string[] args)
{
//UI層已經獲得用戶的數據,就需要傳遞給控制器層,所以需要在此層持有一個C層對象
UserManager um = new UserManager();
//死循環
while (true)
{
Console.WriteLine("請輸入1(增加),2(修改),3(刪除),4(查詢)");
//接受用戶輸入的信息
//等待用戶輸入一行,敲擊回車之後,輸入的信息就存儲在command中
string command = Console.ReadLine();
switch (command)
{
//等待用戶輸入,並存儲
case "1":
Console.WriteLine("請輸入姓名");
string name = Console.ReadLine();
Console.WriteLine("請輸入年齡");
//將數字的字符串表示形式轉換爲它的等效 32 位有符號整數。
int age = int.Parse(Console.ReadLine());
Console.WriteLine("請輸入地址");
string add = Console.ReadLine();
//將數據輸入管理對象中
um.AddItem(new User(age, name, add));
break;
//修改操作
case "2":
Console.WriteLine("請輸入修改人的姓名");
string name_2 = Console.ReadLine();
Console.WriteLine("請輸入要修改的信息:1(姓名),2(年齡),3(地址)");
int inforNum = int.Parse(Console.ReadLine());
Console.WriteLine("請輸入要修改的信息");
string changeInfor = Console.ReadLine();
//調用方法
um.Change(name_2, inforNum, changeInfor);
break;
//刪除操作
case "3":
Console.WriteLine("請輸入刪除的條件,A(地址)/N(姓名)");
char con = char.Parse(Console.ReadLine());
if (con == 'A')
{
Console.WriteLine("請輸入要刪除的地址");
string add_3 = Console.ReadLine();
um.RemoveByAddress(add_3);
}
else if (con == 'N')
{
Console.WriteLine("請輸入要刪除的名字");
string name_3 = Console.ReadLine();
um.RemoveByName(name_3);
}
break;
//查詢操作
case "4":
um.Show();
break;
default: break;
}
//判斷用戶的命令是要幹什麼
//再進行相應的處理
Console.ReadKey();
}
}
}
}