职责链(Chain of Responsibility)模式

 

participants

    The classes and/or objects participating in this pattern are:

  • Handler   (Approver)
    • defines an interface for handling the requests
    • (optional) implements the successor link
  • ConcreteHandler   (Director, VicePresident, President)
    • handles requests it is responsible for
    • can access its successor
    • if the ConcreteHandler can handle the request, it does so; otherwise it forwards the request to its successor
  • Client   (ChainApp)
    • initiates the request to a ConcreteHandler object on the chain

 

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

namespace ChainOfResponsibil
{
    class Program
    {
        static void Main(string[] args)
        {
            Approver larry = new Director();

            Approver sam = new VicePresident();

            Approver tammy = new President();

 

            larry.SetSuccessor(sam);

            sam.SetSuccessor(tammy);

 

            // Generate and process purchase requests

            Purchase p = new Purchase(2034, 99999.00, "Supplies");

            larry.ProcessRequest(p);


            // Wait for user

            Console.ReadKey();


        }
    }

    abstract class Approver
    {

        protected Approver successor;

 

        public void SetSuccessor(Approver successor)
        {

            this.successor = successor;

        }

 

        public abstract void ProcessRequest(Purchase purchase);

    }

 

    /// <summary>

    /// The 'ConcreteHandler' class

    /// </summary>

    class Director : Approver
    {

        public override void ProcessRequest(Purchase purchase)
        {

            if (purchase.Amount < 10000.0)
            {

                Console.WriteLine("{0} approved request# {1}",

                  this.GetType().Name, purchase.Number);

            }

            else if (successor != null)
            {

                Console.WriteLine(this.GetType().Name + "---->" + successor.GetType().Name);
                successor.ProcessRequest(purchase);

            }

        }

    }

 

    /// <summary>

    /// The 'ConcreteHandler' class

    /// </summary>

    class VicePresident : Approver
    {

        public override void ProcessRequest(Purchase purchase)
        {

            if (purchase.Amount < 25000.0)
            {

                Console.WriteLine("{0} approved request# {1}",

                  this.GetType().Name, purchase.Number);

            }

            else if (successor != null)
            {

                Console.WriteLine(this.GetType().Name + "---->" + successor.GetType().Name);
                successor.ProcessRequest(purchase);

            }

        }

    }

 

    /// <summary>

    /// The 'ConcreteHandler' class

    /// </summary>

    class President : Approver
    {

        public override void ProcessRequest(Purchase purchase)
        {

            if (purchase.Amount < 100000.0)
            {

                Console.WriteLine("{0} approved request# {1}",

                  this.GetType().Name, purchase.Number);

            }

            else
            {

                Console.WriteLine(

                  "Request# {0} requires an executive meeting!",

                  purchase.Number);

            }

        }

    }

 

    /// <summary>

    /// Class holding request details

    /// </summary>

    class Purchase
    {

        private int _number;

        private double _amount;

        private string _purpose;

 

        // Constructor

        public Purchase(int number, double amount, string purpose)
        {

            this._number = number;

            this._amount = amount;

            this._purpose = purpose;

        }

 

        // Gets or sets purchase number

        public int Number
        {

            get { return _number; }

            set { _number = value; }

        }

 

        // Gets or sets purchase amount

        public double Amount
        {

            get { return _amount; }

            set { _amount = value; }

        }

 

        // Gets or sets purchase purpose

        public string Purpose
        {

            get { return _purpose; }

            set { _purpose = value; }

        }

    }


}

posted @ 2008-12-19 15:17  昕友软件开发  阅读(265)  评论(0编辑  收藏  举报
欢迎访问我的开源项目:xyIM企业即时通讯