机房合作之装饰模式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xsh096011/article/details/81911486

用装饰模式管实现理员登陆;

这里写图片描述
这里写图片描述
B层

public bool LoginAdmin(AdminInfo admin)
        {

            bool flag = false;
            AdminLogin adminLogin = new AdminLogin();
            TestAdmin testAdmin = new TestAdmin();
            TestLoginState testLoginState = new TestLoginState();
            TestUserExist testUserExist = new TestUserExist();
            TestLoginPwd testLoginPwd = new TestLoginPwd();

            testLoginState.SetLogin(adminLogin);
            testUserExist.SetLogin(testLoginState);
            testLoginPwd.SetLogin(testUserExist);

            if (testUserExist.TestLogin(admin)==true && testLoginPwd.TestLogin(admin)==true)
            {
                // 管理员是否正在上机
                OnWorkIDAL onWorkDal = sqlFactory.CreateOnWork();
                List<OnWorkInfo> onWorkList = onWorkDal.InquiryOnWork(new OnWorkInfo() { AdminID = admin.AdminID });
                if (onWorkList.Count > 0)
                {
                    throw new Exception("该管理员正在上机!!!");
                }
                else
                {
                    //添加到正在工作表中
                    string computer = System.Environment.MachineName;//获取本地计算机名
                    onWorkDal.AddOnWork(new OnWorkInfo() { AdminID = admin.AdminID, ComputerName = computer });
                    flag = true;
                }
            }
            return flag;
            }

AdminLogin

namespace DecoratorModel_AdminLogin
{
    public class AdminLogin
    {
        public virtual bool TestLogin(AdminInfo admin)
        {
            return true;
        }
    }
}

TestAdmin

namespace DecoratorModel_AdminLogin
{
    public class TestAdmin:AdminLogin
    {
        protected AdminLogin adminLogin;
        public void SetLogin(AdminLogin adminLogin)
        {
            this.adminLogin = adminLogin;
        }
        public override bool TestLogin(AdminInfo admin)
        {
            try
            {
                if (admin != null)
                {
                    adminLogin.TestLogin(admin);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (System.Exception)
            {
                throw new Exception("管理员登陆信息错误请重新登陆!");
            }

        }
    }
}

TestLoginPwd

namespace DecoratorModel_AdminLogin
{
    public class TestLoginPwd: TestAdmin
    {
        private SqlServerFactory sqlFactory = null;
        private AdminIDAL adminIDAL = null;

        public override bool TestLogin(AdminInfo admin)
        {
            bool flag = false;
            try
            {
                SqlServerFactory factory = new SqlServerFactory();
                AdminIDAL adminIDAL = factory.CreateAdmin();
                List<AdminInfo> adminList = adminIDAL.InquiryAllAdmin(admin.Pwd);

                if (admin.Pwd != adminList[0].Pwd.Trim())
                {
                    throw new Exception("密码不正确!");
                }
                else
                {
                    flag = true;
                }
            }
            catch (System.Exception)
            {
                throw new Exception("密码不正确");
            }
            return flag;
        }
    }
}

TestLoginState

namespace DecoratorModel_AdminLogin
{
    public class TestLoginState : TestAdmin
    {
        public override bool TestLogin(AdminInfo admin)
        {
            bool flag = true;
            return flag;
        }
    }
}

TestUserExist

namespace DecoratorModel_AdminLogin
{
    public class TestUserExist:TestAdmin
    {
        private SqlServerFactory sqlFactory = null;
        private AdminIDAL adminIDAL = null;


        public override bool TestLogin(AdminInfo admin)
        {
            bool flag = false;
            try
            {
                SqlServerFactory factory = new SqlServerFactory();
                AdminIDAL adminIDAL = factory.CreateAdmin();
                List<AdminInfo> adminList = adminIDAL.InquiryAllAdmin(admin.AdminID);

                if (adminList.Count == 0)
                {
                    throw new Exception("没有该管理员账号,请重新输入账号!");
                }
                else
                {
                    flag = true;
                }
            }
            catch (System.Exception)
            {
                throw new Exception("没有该管理员账号,请重新输入账号!");
            }
            return flag;
        }
    }
}

个人感觉装饰模式和职责链模式在功能上有类似;多尝试设计模式,你会发现设计模式会有如此的趣味性;

猜你喜欢

转载自blog.csdn.net/xsh096011/article/details/81911486