zoukankan      html  css  js  c++  java
  • 23种设计模式

    一、责任链模式的介绍

            责任链模式用简单点的话来说,将责任一步一步传下去,这就是责任,想到这个我们可以相当击鼓传花,这个是为了方便记忆,另外就是我们在项目中经常用到的审批流程等这一类的场景时我们就可以考虑是否可以使用责任链模式。下面的介绍将会按照这个审批的例子写Demo;

    二、责任链模式的Demo

       //采购请求
        public class PurchaseRequest
        {
            //金额
            public double Amount { get; set; }
            //产品名字
            public string ProductName { get; set; }

            public PurchaseRequest(double amount,string productName)
            {
                Amount = amount;
                ProductName = productName;
            }
        }

       //审批人。
        public abstract class Approver
        {
            
            public Approver NextApprover { get; set; }

            public string Name { get; set; }

            public Approver(string name)
            {
                Name = name;
            }

            public abstract void ProcessRequest(PurchaseRequest purchaseRequset);

        }

     //管理者
        public class Manager : Approver
        {
            public Manager(string name) : base(name)
            {

            }

            public override void ProcessRequest(PurchaseRequest purchaseRequset)
            {
                if (purchaseRequset.Amount < 10000)
                {
                    Console.WriteLine("{0}-{1} approved the request of purshing {2}", this, Name, purchaseRequset.ProductName);
                }
                else if(NextApprover!=null)
                {
                    NextApprover.ProcessRequest(purchaseRequset);
                }
            }
        }

    //副总
        public class VicePresident : Approver
        {
            public VicePresident(string name) : base(name)
            {
            }

            public override void ProcessRequest(PurchaseRequest purchaseRequset)
            {
                if (purchaseRequset.Amount < 20000)
                {
                    Console.WriteLine("{0}-{1} approved the request of purshing {2}", this, Name, purchaseRequset.ProductName);
                }
                else if (NextApprover != null)
                {
                    NextApprover.ProcessRequest(purchaseRequset);
                }
            }
        }

    //总经理
        public class President : Approver
        {
            public President(string name) : base(name)
            {
            }

            public override void ProcessRequest(PurchaseRequest purchaseRequset)
            {
                if (purchaseRequset.Amount < 100000)
                {
                    Console.WriteLine("{0}-{1} approved the request of purshing {2}", this, Name, 
                        purchaseRequset.ProductName);
                }
                else
                {
                    Console.WriteLine("我们需要组织会议");
                }
            }
        }

    三、Demo位置

          GitHub:https://github.com/wangtongzhou520/Designpattern

         

        

      

  • 相关阅读:
    lower版购物车模拟
    字典的增删改查和操作
    生成四位验证码
    列表的增删改查和操作
    根据输入字符串,分别计算大写,小写,数字,标点的个数
    检查一个数是不是质数
    非诚勿扰,选心动女生(小游戏)
    汉诺塔的实现
    一个纯虚函数导致的问题
    Hello World 之 CGAL
  • 原文地址:https://www.cnblogs.com/yujihaia/p/7468159.html
Copyright © 2011-2022 走看看