设计模式之责任链模式

Posted Jonny

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设计模式之责任链模式相关的知识,希望对你有一定的参考价值。

1、类图

责任链模式结构

实例类图

2、创建项目

…………………………

3、 PurchaseRequest:采购单类,充当请求类

namespace CoRSample

{

    class PurchaseRequest

    {

        private double amount;  //采购金额

        private int number;  //采购单编号

        private string purpose;  //采购目的

 

        public PurchaseRequest(double amount, int number, string purpose)

        {

            this.amount = amount;

            this.number = number;

            this.purpose = purpose;

        }

 

        public double Amount

        {

            get { return amount; }

            set { amount = value; }

        }

 

        public int Number

        {

            get { return number; }

            set { number = value; }

        }

 

        public string Purpose

        {

            get { return purpose; }

            set { purpose = value; }

        }

    }

}

 

4、 Approver:审批者类,充当抽象处理者

namespace CoRSample

{

    abstract class Approver

    {

        protected Approver successor; //定义后继对象

        protected string name; //审批者姓名

 

        public Approver(string name)

        {

            this.name = name;

        }

 

        //设置后继者

        public void SetSuccessor(Approver successor)

        {

            this.successor = successor;

        }

 

        //抽象请求处理方法

        public abstract void ProcessRequest(PurchaseRequest request);

    }

}

 

5、 Director:主任类,充当具体处理者

 

using System;

 

namespace CoRSample

{

    class Director : Approver

    {

        public Director(string name) : base(name)

        {

        }

 

        //具体请求处理方法

        public override void ProcessRequest(PurchaseRequest request)

        {

      if (request.Amount < 50000)

            {

                Console.WriteLine("主任{0}审批采购单:{1},金额:{2}元,采购目的:{3}。", this.name, request.Number, request.Amount, request.Purpose);  //处理请求

      }

      else {

      this.successor.ProcessRequest(request);  //转发请求

      }

      }

    }

}

6、 VicePresident:副董事长类,充当具体处理者

using System;

 

namespace CoRSample

{

    class VicePresident : Approver

    {

        public VicePresident(string name) : base(name)

        {

        }

 

        //具体请求处理方法

        public override void ProcessRequest(PurchaseRequest request)

        {

      if (request.Amount < 100000)

            {

                Console.WriteLine("副董事长{0}审批采购单:{1},金额:{2}元,采购目的:{3}。", this.name, request.Number, request.Amount, request.Purpose);  //处理请求

      }

      else

            {

      this.successor.ProcessRequest(request);  //转发请求

      }

      }

    }

}

 

7、 President:董事长类,充当具体处理者

using System;

 

namespace CoRSample

{

    class President : Approver

    {

        public President(string name) : base(name)

        {

        }

 

        //具体请求处理方法

        public override void ProcessRequest(PurchaseRequest request)

        {

      if (request.Amount < 500000)

            {

      Console.WriteLine("董事长{0}审批采购单:{1},金额:{2}元,采购目的:{3}。", this.name, request.Number, request.Amount, request.Purpose);  //处理请求

      }

      else

            {

      this.successor.ProcessRequest(request);  //转发请求

      }

      }

    }

}

8、 Congress:董事会类,充当具体处理者

using System;

 

namespace CoRSample

{

    class Congress : Approver

    {

        public Congress(string name) : base(name)

        {

        }

 

        //具体请求处理方法

        public override void ProcessRequest(PurchaseRequest request)

        {

      Console.WriteLine("召开董事会审批采购单:{0},金额:{1}元,采购目的:{2}。",request.Number, request.Amount, request.Purpose);     //处理请求

      }  

    }

}

 

9、 Program:客户端测试类

using System;

 

namespace CoRSample

{

    class Program

    {

        static void Main(string[] args)

        {

            Approver wjzhang, gyang, jguo, meeting;

            //Approver rhuang;

            //rhuang = new Manager("黄蓉");

 

            wjzhang = new Director("张无忌");

            gyang = new VicePresident("杨过");

            jguo = new President("郭靖");

            meeting = new Congress("董事会");

 

            //创建职责链

            wjzhang.SetSuccessor(gyang);

            gyang.SetSuccessor(jguo);

            jguo.SetSuccessor(meeting);

 

            //wjzhang.SetSuccessor(rhuang); //将“黄蓉”作为“张无忌”的下家

            //rhuang.SetSuccessor(gyang); //将“杨过”作为“黄蓉”的下家

            //gyang.SetSuccessor(jguo);

            //jguo.SetSuccessor(meeting);

 

 

            //创建采购单

            PurchaseRequest pr1 = new PurchaseRequest(45000, 10001, "购买倚天剑");

            wjzhang.ProcessRequest(pr1);

 

            PurchaseRequest pr2 = new PurchaseRequest(60000, 10002, "购买《葵花宝典》");

            wjzhang.ProcessRequest(pr2);

 

            PurchaseRequest pr3 = new PurchaseRequest(160000, 10003, "购买《金刚经》");

            wjzhang.ProcessRequest(pr3);

 

            PurchaseRequest pr4 = new PurchaseRequest(800000, 10004, "购买桃花岛");

            wjzhang.ProcessRequest(pr4);

 

            Console.Read();

        }

    }

}

 

10、 结果及分析,编译并运行程序,输出结果如下:

 

 

以上是关于设计模式之责任链模式的主要内容,如果未能解决你的问题,请参考以下文章

行为型设计模式之责任链模式

设计模式之策略模式与责任链模式详解和应用

设计模式之责任链模式

设计模式之责任链模式

5 行为型模式之- 责任链模式

设计模式之责任链模式