职责链 --- 应用篇(机房收费系统C#版本)
【摘要】
前言:
职责链模式,重点是在链上。是 针对一个事件的一系列处理者,在一条职责链上有多个处理者可以对该事件进行处理操作。
就类似上面图片的线,从机房收费系统来说。对于登录就有三个对应的处理者,管理员,...
前言:
职责链模式,重点是在链上。是 针对一个事件的一系列处理者,在一条职责链上有多个处理者可以对该事件进行处理操作。
就类似上面图片的线,从机房收费系统来说。对于登录就有三个对应的处理者,管理员,操作员和顾客。
继续处理者的设置:
已上图为例,可以从左到右依次设置下一任处理者。也可以从右往左依次设置下一任处理者。这两种都可以,只要不是从中间开始就行。
代码:
客户端代码:
/// <summary>
/// 登录B层
/// </summary>
/// <param name="id">用户Id</param>
/// <param name="pwd">用户密码</param>
/// <param name="frmLogin">登陆窗体</param>
public void Login(string id, string pwd, Form frmLogin)
{
//用于返回信息
string strMsger = "";
//判断是否为空
CheckText checkText = new CheckText();
checkText.Check(id, pwd);
//获取用户信息
List<T_Login> t_Logins = new List<T_Login>();
ILogin login;
login = Factory.CreateConcreteClass("ConcreteDAL", "DAL", "logindal");
t_Logins = login.GetManagerById(id);
//验证账号是否存在
if (t_Logins.Count >0)
{
#region 设置继续处理者
Level.LoginAdmin admin = new Level.LoginAdmin("管理员");
Level.LoginCustomer loginCustomer = new Level.LoginCustomer("顾客");
Level.LoginOperator loginOperator = new Level.LoginOperator("操作员");
admin.SetSupertior(loginOperator);//管理员的下个处理者
loginOperator.SetSupertior(loginCustomer);//操作员的下个处理者
#endregion
#region 登录
String userlevel = t_Logins[0].UserLevel;//获取级别
admin.UserLogin(id, pwd, frmLogin, userlevel);//从管理员级别开始登录
#endregion
//判断是不是错误提示,去空之后等于"",说明不是错误信息,不返还
if (strMsger.Trim() != "") {
MessageBox.Show(strMsger);
}
}
else {
throw new Exception("用户不存在或密码错误!");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
职责链代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Entity;
using AbstractFactory;
using IDAL;
using System.Data;
using System.Windows.Forms;
namespace BLL.Level
{
/// <summary>
/// 用户等级的抽象类
/// </summary>
public abstract class UserLevel
{
/// <summary>
/// 级别字段
/// </summary>
protected string Level;
/// <summary>
/// 给等级字段赋值
/// </summary>
/// <param name="Level"></param>
public UserLevel(string Level)
{
this.Level = Level;
}
/// <summary>
/// 继续处理者字段
/// </summary>
protected UserLevel user;
/// <summary>
/// 设置继续处理者
/// </summary>
/// <param name="superior">具体的继续处理者</param>
public void SetSupertior(UserLevel superior)
{
this.user = superior;
}
/// <summary>
/// 登录
/// </summary>
/// <param name="UserId">用户ID</param>
/// <param name="pwd">密码</param>
/// <param name="frmLogin">登陆窗体</param>
/// <param name="level">级别</param>
abstract public string UserLogin(String UserId, String pwd,Form frmLogin,string level);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Entity;
using AbstractFactory;
using System.Data;
using System.Reflection;
using System.Configuration;
using IDAL;
using System.Windows.Forms;
namespace BLL.Level
{
/// <summary>
/// 管理员职责
/// </summary>
public class LoginAdmin : UserLevel
{
/// <summary>
/// 调用父类构造函数
/// </summary>
/// <param name="Level">给级别赋值</param>
public LoginAdmin(string Level) : base(Level)
{
}
ILogin login = Factory.CreateConcreteClass("ConcreteDAL","DAL", "logindal");
/// <summary>
/// 登录
/// </summary>
/// <param name="UserId">用户ID</param>
/// <param name="pwd">密码</param>
/// <param name="frmLogin">登陆窗体</param>
public override string UserLogin(String UserId, String pwd, Form frmLogin, string level)
{
//返回提示信息
string flage = "";
//判断是否是管理员
if (level.Equals("管理员"))
{
//储存查询结果
List<T_Manager> list = new List<T_Manager>();
//查询管理者密码
list = login.SelecTt_Managers(UserId, pwd);
//判断管理者密码是否正确
if (list.Count > 0)
{
//获取上机状态
List<T_WorkLog> WorkLogsState = login.SelectManagerState(UserId);
//判断上机状态(10代表上班 11 代表下班)
if (WorkLogsState.Count > 0)
{
//遍历判断登陆状态
foreach (T_WorkLog item in WorkLogsState)
{
//判断用户登陆状态
if (item.State == "10")
{
flage = "此用户已登录!";
//抛出异常(通过 异常返回提示信息)
throw new Exception(flage);
}
}
//进行登陆操作
#region 对实体赋值
T_WorkLog t_WorkLog = new T_WorkLog();
t_WorkLog.ManagerId = UserId;
t_WorkLog.OnDateTime = DateTime.Now.ToString();
t_WorkLog.State = "10";
t_WorkLog.Computer = Computer.GetMachineName();
t_WorkLog.GetLocaIp = Computer.GetLocalIp();
#endregion
Inert(t_WorkLog);
#region 登陆记录插入旧代码
判断登陆状态
//if (WorkLogsState[0].State == "10")
//{
// flage = "此用户已登录!";
//}
//else {
// //进行登陆操作
// #region 对实体赋值
// T_WorkLog t_WorkLog = new T_WorkLog();
// t_WorkLog.ManagerId = UserId;
// t_WorkLog.OnDateTime = DateTime.Now.ToString();
// t_WorkLog.State = "10";
// #endregion
// Inert(t_WorkLog);
// flage = "登陆成功!";
//}
#endregion
//打开管理员窗体
OperFromAdmin();
//隐藏登陆界面
frmLogin.Hide();
// flage = "登陆成功!";
}
else
{
//进行登陆
//#region 对实体赋值
T_WorkLog t_WorkLog = new T_WorkLog();
t_WorkLog.ManagerId = UserId;
t_WorkLog.OnDateTime = DateTime.Now.ToString();
t_WorkLog.State = "10";
t_WorkLog.Computer = Computer.GetMachineName();
t_WorkLog.GetLocaIp = Computer.GetLocalIp();
Inert(t_WorkLog);
//打开管理员窗体
OperFromAdmin();
//隐藏登陆界面
frmLogin.Hide();
// flage = "登陆成功!";
}
}
else
{
throw new Exception("密码错误");
}
}
else {
//跳转到下一级
user.UserLogin(UserId, pwd, frmLogin, level);//跳转操作员登录级别
}
return flage;
}
/// <summary>
/// 插入方法
/// </summary>
/// <param name="t_WorkLog">登陆记录表实体</param>
/// <returns></returns>
private int Inert(T_WorkLog t_WorkLog) {
return login.InsertManagerWorkLogLine(t_WorkLog);
}
/// <summary>
/// 打开管理员窗体
/// </summaryConcreteUI
private void OperFromAdmin() {
//通过接口工厂+反射实例化UI层窗体
Form formAdmin = AbstractFactory.Factory.CreateConcreteClass("ConcreteUI", "UI", "frmAdmin");
formAdmin.Show();//显示管理员窗体
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Entity;
using AbstractFactory;
using System.Data;
using System.Reflection;
using System.Configuration;
using System.Windows.Forms;
using IBLL;
using IDAL;
namespace BLL.Level
{
/// <summary>
/// 顾客职责
/// </summary>
public class LoginCustomer:UserLevel
{
/// <summary>
/// 登录
/// </summary>
/// <param name="UserId">用户ID</param>
/// <param name="pwd">密码</param>
public LoginCustomer(string Level) : base(Level)
{
}
/// <summary>
/// 登录
/// </summary>
/// <param name="userlevel">级别</param>
/// <param name="pwd">密码</param>
/// <param name="frmLogin">登陆窗体</param>
public override string UserLogin(String UserId, String pwd,Form frmLogin, string level)
{
string flage = "";
//判断是否是顾客
if (level.Equals("顾客"))
{
ILogin login = AbstractFactory.Factory.CreateConcreteClass("ConcreteDAL", "DAL", "logindal");
List<T_Customer> t_Customers = login.SelectCustomerInfo(UserId, pwd);
///判断密码是否正确
if (t_Customers.Count == 1)
{
/// 判断余额
if (t_Customers[0].CustomerCash <= 0)
{
throw new Exception("余额不足,请充值后上机");
}
else
{
List<T_Line> t_Lines = login.SelectLineStateInfo(UserId);
if (t_Lines.Count > 0)
{
foreach (T_Line item in t_Lines)
{
if (item.LineState.Trim() == "6")
{
flage = "用户正在上机";
//抛出异常
throw new Exception(flage);
}
}
///插入上机记录信息
login.InsertLine(UserId, Computer.GetMachineName());
//隐藏登陆窗体
frmLogin.Hide();
Form form = AbstractFactory.Factory.CreateConcreteClass("ConcreteUI", "UI", "frmstrudnet");
form.Show();
// flage = "登录成功";
}
else
{
login.InsertLine(UserId, Computer.GetMachineName());
//隐藏登陆窗体
frmLogin.Hide();
Form form = AbstractFactory.Factory.CreateConcreteClass("ConcreteUI", "UI", "frmstrudnet");
form.Show();
// flage = "登录成功";
}
}
}
else
{
throw new Exception("密码错误!请重新输入");
}
}
return flage;
//throw new Exception(flage);
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Entity;
using AbstractFactory;
using System.Data;
using System.Reflection;
using System.Configuration;
using IDAL;
using System.Windows.Forms;
namespace BLL.Level
{
/// <summary>
/// 操作员登陆职责
/// </summary>
public class LoginOperator : UserLevel
{
/// <summary>
/// 登录
/// </summary>
/// <param name="UserId">用户ID</param>
/// <param name="pwd">密码</param>
public LoginOperator(string Level) : base(Level)
{
}
ILogin login = Factory.CreateConcreteClass("ConcreteDAL", "DAL", "logindal");
/// <summary>
/// 登录
/// </summary>
/// <param name="userlevel">级别</param>
/// <param name="pwd">密码</param>
/// <param name="frmLogin">登陆窗体</param>
public override string UserLogin(String UserId, String pwd,Form frmLogin, string level)
{
//返回提示信息
string flage = "";
//判断是否是操作员
if (level.Equals("操作员"))
{
//储存查询结果
List<T_Manager> list = new List<T_Manager>();
//查询管理者密码
list = login.SelecTt_Managers(UserId, pwd);
//判断管理者密码是否正确
if (list.Count > 0)
{
//获取上机状态
List<T_WorkLog> WorkLogsState = login.SelectManagerState(UserId);
//判断上机状态(10代表上班 11 代表下班)
if (WorkLogsState.Count > 0)
{
//遍历判断登陆状态
foreach (T_WorkLog item in WorkLogsState)
{
//判断用户登陆状态
if (item.State == "10")
{
flage = "此用户已登录!";
//抛出异常(通过 异常返回提示信息)
throw new Exception(flage);
}
}
//进行登陆操作
#region 对实体赋值
T_WorkLog t_WorkLog = new T_WorkLog();
t_WorkLog.ManagerId = UserId;
t_WorkLog.OnDateTime = DateTime.Now.ToString();
t_WorkLog.State = "10";
t_WorkLog.Computer = Computer.GetMachineName();
t_WorkLog.GetLocaIp = Computer.GetLocalIp();
#endregion
Inert(t_WorkLog);
//打开操作员窗体
OperFromOpetar();
//隐藏登陆界面
frmLogin.Hide();
// flage = "登陆成功!";
}
else
{
//进行登陆
#region 插入登陆记录旧代码
//#region 对实体赋值
T_WorkLog t_WorkLog = new T_WorkLog();
t_WorkLog.ManagerId = UserId;
t_WorkLog.OnDateTime = DateTime.Now.ToString();
t_WorkLog.State = "10";
t_WorkLog.Computer = Computer.GetMachineName();
t_WorkLog.GetLocaIp = Computer.GetLocalIp();
#endregion
Inert(t_WorkLog);
//打开操作员窗体
OperFromOpetar();
//隐藏登陆界面
frmLogin.Hide();
// flage = "登陆成功!";
}
}
else
{
flage = "密码错误";
}
}
else {
//设置下一任继续处理者
user.UserLogin(UserId, pwd, frmLogin, level);//跳转操顾客登录级别
}
return flage;
}
/// <summary>
/// 插入方法
/// </summary>
/// <param name="t_WorkLog">登陆记录表实体</param>
/// <returns></returns>
private int Inert(T_WorkLog t_WorkLog)
{
return login.InsertManagerWorkLogLine(t_WorkLog);
}
/// <summary>
/// 打开管理员窗体
/// </summaryConcreteUI
private void OperFromOpetar()
{
//通过接口工厂+反射实例化UI层窗体
Form formAdmin = AbstractFactory.Factory.CreateConcreteClass("ConcreteUI", "UI", "frmOperato");
formAdmin.Show();//显示管理员窗体
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
管理者还有一些冗余代码,以上代码仅供参考。
文章来源: kangshihang.blog.csdn.net,作者:康世行,版权归原作者所有,如需转载,请联系作者。
原文链接:kangshihang.blog.csdn.net/article/details/109960309
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)