码迷,mamicode.com
首页 > 其他好文 > 详细

23种设计模式

时间:2017-09-02 23:21:51      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:https   采购   产品   使用   单点   sign   常用   null   app   

一、责任链模式的介绍

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

     

    

  

 

23种设计模式

标签:https   采购   产品   使用   单点   sign   常用   null   app   

原文地址:http://www.cnblogs.com/yujihaia/p/7468159.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!