码迷,mamicode.com
首页 > Windows程序 > 详细

责任链模式 c#

时间:2016-01-11 14:06:44      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

  就是你找政府部门办事,需要层层审批,求情一层层的传递直到有权利的人通过审批为止。

 

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {

        public class FetchSalary
        {
            public double Amount
            { get; set; }

            public FetchSalary(double amount)
            {
                Amount = amount;
            }
        }

        public abstract class Approver
        {
            public Approver Next{get;set;}

            public abstract void ProcessRequest(FetchSalary request);
        }

        public class PL:Approver
        {
            public override void ProcessRequest(FetchSalary request)
            {
                if (request.Amount < 100f)
                {
                    Console.WriteLine("Pl approved");
                }
                else if (Next != null)
                {
                    Next.ProcessRequest(request);
                }
            }
        }

        public class PM: Approver
        {
            public override void ProcessRequest(FetchSalary request)
            {
                if (request.Amount < 1000f)
                {
                    Console.WriteLine("PM approved");
                }
                else if (Next != null)
                {
                    Next.ProcessRequest(request);
                }
            }
        }

        public class Predident: Approver
        {
            public override void ProcessRequest(FetchSalary request)
            {
                if (request.Amount < 10000f)
                {
                    Console.WriteLine("Predident approved");
                }
                else if(Next != null)
                {
                    Next.ProcessRequest(request);
                }
            }
        }

        static void Main(string[] args)
        {
            FetchSalary fetchSalary100 = new FetchSalary(100f);
            FetchSalary fetchSalary1000 = new FetchSalary(1000f);
            FetchSalary fetchSalary10000 = new FetchSalary(10000f);

            Approver pl = new PL();
            Approver pm = new PM();
            Approver president = new Predident();

            pl.Next = pm;
            pm.Next = president;
            president.Next = null;

            pl.ProcessRequest(fetchSalary100);
            pl.ProcessRequest(fetchSalary1000);
            pl.ProcessRequest(fetchSalary10000);
        }
    }
}

 

责任链模式 c#

标签:

原文地址:http://www.cnblogs.com/zkzk945/p/5120848.html

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