策略模式的典型应用

做了一个小东西,里面有多个角色,每个角色都有特殊的菜单项,现使用策略模式对其简单实现。

关于策略模式的介绍请参考其他书籍。

下面是项目架构和实现:

架构:

架构

实现:

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;
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值