日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

策略模式的典型應用

系統 1836 0

做了一個小東西,里面有多個角色,每個角色都有特殊的菜單項,現使用策略模式對其簡單實現。

關于策略模式的介紹請參考其他書籍。

下面是項目架構和實現:

架構:

架構

實現:

IMenuStrategy.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StrategyPattern.BLL
{
public interface IMenuStrategy
{
void ShowMenu(System.Windows.Forms.MenuStrip ms);
}
}

Developer.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StrategyPattern.BLL
{
public class Developer:IMenuStrategy
{
public void ShowMenu(System.Windows.Forms.MenuStrip ms)
{
ms.Items[0].Visible = false;
ms.Items[1].Visible = false;
ms.Items[2].Visible = false;
ms.Items[3].Visible = true;
ms.Items[4].Visible = false;
ms.Items[5].Visible = true;
}
}
}

Tester.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StrategyPattern.BLL
{
public class Tester:IMenuStrategy
{
public void ShowMenu(System.Windows.Forms.MenuStrip ms)
{
ms.Items[0].Visible = false;
ms.Items[1].Visible = false;
ms.Items[2].Visible = false;
ms.Items[3].Visible = false;
ms.Items[4].Visible = true;
ms.Items[5].Visible = true;
}
}
}

CommonUser.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StrategyPattern.BLL
{
public class CommonUser:IMenuStrategy
{
public void ShowMenu(System.Windows.Forms.MenuStrip ms)
{
ms.Items[0].Visible = true;
ms.Items[1].Visible = true;
ms.Items[2].Visible = true;
ms.Items[3].Visible = true;
ms.Items[4].Visible = true;
ms.Items[5].Visible = true;
}
}
}

MenuOP.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StrategyPattern.BLL
{
public class MenuOP
{
private IMenuStrategy ims;
public MenuOP(IMenuStrategy menuStrategy)
{
ims = menuStrategy;
}
public void SetMenu(System.Windows.Forms.MenuStrip ms)
{
ims.ShowMenu(ms);
}
}
}

界面:

登錄

代碼:

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;

namespace StrategyPattern
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();

}
private static string role;
public static string Role
{
get
{
return role;
}
set
{
role = value;
}
}
private void btnSubmit_Click(object sender, EventArgs e)
{
switch (txtName.Text.Trim())
{
case "D":
case "d":
if (txtPwd.Text.Trim() == "d")
{
Role = "d";
MainForm mf = new MainForm();
mf.Show();
}
break;
case "t":
case "T":
if (txtPwd.Text.Trim() == "t")
{
Role = "t";
MainForm mf = new MainForm();
mf.Show();
}
break;
case "c":
case "C":
if (txtPwd.Text.Trim() == "c")
{
Role = "c";
MainForm mf = new MainForm();
mf.Show();
}
break;

}
}
}
}

不同的登錄界面:

開發:

開發人員

測試 :

經理:

經理

代碼:

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;

namespace StrategyPattern
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}

private void MainForm_Load(object sender, EventArgs e)
{
switch (Login.Role)
{
case "d":
case "D":
BLL.MenuOP mo = new StrategyPattern.BLL.MenuOP(new BLL.Developer());
mo.SetMenu(this.mainMenuStrip);
break;
case "t":
case "T":
BLL.MenuOP mo1 = new StrategyPattern.BLL.MenuOP(new BLL.Tester());
mo1.SetMenu(this.mainMenuStrip);
break;
case "c":
case "C":
BLL.MenuOP mo2 = new StrategyPattern.BLL.MenuOP(new BLL.CommonUser());
mo2.SetMenu(this.mainMenuStrip);
break;
}
}
}
}

策略模式的典型應用


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。?!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 宜春市| 田阳县| 乐都县| 蒙阴县| 尤溪县| 同江市| 长葛市| 哈尔滨市| 运城市| 揭东县| 陇西县| 富川| 海盐县| 瑞昌市| 社会| 常熟市| 荥经县| 仪征市| 井研县| 潼南县| 磐安县| 和田市| 永顺县| 龙州县| 浦东新区| 宕昌县| 宣汉县| 隆德县| 鄂托克旗| 木里| 伊川县| 奉贤区| 嘉兴市| 吉水县| 盐城市| 浦城县| 固安县| 东乌珠穆沁旗| 梓潼县| 丹阳市| 景洪市|