WEB开发网
开发学院软件开发C语言 无废话C#设计模式之十七:Chain Of Resp 阅读

无废话C#设计模式之十七:Chain Of Resp

 2009-04-02 08:23:09 来源:WEB开发网   
核心提示: 示例代码using System;using System.Collections.Generic;using System.Text;namespace ChainOfRespExample{ class Program { static List<CustomerService&

示例代码

using System;

using System.Collections.Generic;

using System.Text;

 

namespace ChainOfRespExample

{

    class Program

    {

        static List<CustomerService> gmTeam = new List<CustomerService>();

        static List<CustomerService> managerTeam = new List<CustomerService>();

        static List<CustomerService> directorTeam = new List<CustomerService>();

        static Random random = new Random();

 

        static void InitCOR()

        {

            if (managerTeam.Count > 0)

            {

                foreach (CustomerService cs in gmTeam)

                    cs.SetLeader(managerTeam[random.Next(managerTeam.Count)]);

            }

            else

            {

                foreach (CustomerService cs in gmTeam)

                    cs.SetLeader(directorTeam[random.Next(directorTeam.Count)]);

            }

            foreach (CustomerService cs in managerTeam)

                cs.SetLeader(directorTeam[random.Next(directorTeam.Count)]);

            // These configs above depends on business logic.

        }

 

        static void InitGM()

        {

            for (int i = 1; i <= 9; i++)

            {

                CustomerService gm = new NormalGM("gm" + i);

                gm.SetResponsibleCaseCategory(new int[] { 1, 2 });

                gmTeam.Add(gm);

            }

            for (int i = 1; i <= 2; i++)

            {

                CustomerService manager = new GMManager("manager" + i);

                manager.SetResponsibleCaseCategory(new int[] { 3, 4 });

                managerTeam.Add(manager);

            }

            for (int i = 1; i <=1; i++)

                directorTeam.Add(new GMDirector("director" + i));

            // These configs above should be from database.

        }

 

        static void Main(string[] args)

        {

            InitGM();

            InitCOR();

            CustomerService gm = gmTeam[random.Next(gmTeam.Count)];

            gm.HandleCase(new Case(1, false));

            Console.WriteLine(Environment.NewLine);

            gm = gmTeam[random.Next(gmTeam.Count)];

            gm.HandleCase(new Case(2, true));

            Console.WriteLine(Environment.NewLine);

            gm = gmTeam[random.Next(gmTeam.Count)];

            gm.HandleCase(new Case(3, false));

            Console.WriteLine(Environment.NewLine);

            gm = gmTeam[random.Next(gmTeam.Count)];

            gm.HandleCase(new Case(4, true));

            Console.WriteLine(Environment.NewLine);

            gm = gmTeam[random.Next(gmTeam.Count)];

            gm.HandleCase(new Case(5, true));

        }

    }

 

    class Case

    {

        private int caseCategory;

 

        public int CaseCategory

        {

            get { return caseCategory; }

        }

 

        private bool importantCase;

 

        public bool ImportantCase

        {

            get { return importantCase; }

        }

 

        private string reply;

 

        public string Reply

        {

            get { return reply ; }

            set { reply = value; }

        }

 

        public Case(int caseCategory, bool importantCase)

        {

            this.caseCategory = caseCategory;

            this.importantCase = importantCase;

        }

    }

 

    abstract class CustomerService

    {

        protected CustomerService leader;

        protected List<int> responsibleCaseCategory = new List<int>();

        protected string name;

 

        public string Name

        {

            get { return name; }

        }

 

        public CustomerService(string name)

        {

            this.name = name;

        }

 

        public void SetLeader(CustomerService leader)

        {

            this.leader = leader;

        }

 

        public abstract void HandleCase(Case gameCase);

 

        public void SetResponsibleCaseCategory(int[] responsibleCaseCategory)

        {

            foreach (int i in responsibleCaseCategory)

                this.responsibleCaseCategory.Add(i);

        }

    }

 

    class NormalGM : CustomerService

    {

        public NormalGM(string name) : base(name) { }

 

        public override void HandleCase(Case gameCase)

        {

            if (responsibleCaseCategory.Contains(gameCase.CaseCategory))

            {

                gameCase.Reply = "OK";

                Console.WriteLine("The case has been replied by " + name);

                if (gameCase.ImportantCase)

                {

                    Console.WriteLine("The reply should be checked.");

                    leader.HandleCase(gameCase);

                }

                else

                    Console.WriteLine("The reply has been sent to player.");

            }

            else if (leader != null)

            {

                Console.WriteLine(string.Format("{0} reports this case to {1}.", name, leader.Name));

                leader.HandleCase(gameCase);

            }

        }

    }

 

    class GMManager : CustomerService

    {

        public GMManager(string name) : base(name) { }

 

        public override void HandleCase(Case gameCase)

        {

            if (responsibleCaseCategory.Contains(gameCase.CaseCategory))

            {

                gameCase.Reply = "OK";

                Console.WriteLine("The case has been replied by " + name);

                if (gameCase.ImportantCase)

                {

                    Console.WriteLine("The reply should be checked.");

                    leader.HandleCase(gameCase);

                }

                else

                    Console.WriteLine("The reply has been sent to player.");

            }

            else if (gameCase.Reply != null)

            {

                Console.WriteLine("The case has been checked by " + name);

                Console.WriteLine("The reply has been sent to player.");

            }

            else if (leader != null)

            {

                Console.WriteLine(string.Format("{0} reports this case to {1}.", name, leader.Name));

                leader.HandleCase(gameCase);

            }

        }

    }

 

    class GMDirector : CustomerService

    {

        public GMDirector(string name) : base(name) { }

 

        public override void HandleCase(Case gameCase)

        {

            if (gameCase.Reply != null)

            {

                Console.WriteLine("The case has been checked by " + name);

                Console.WriteLine("The reply has been sent to player.");

            }

            else

            {

                gameCase.Reply = "OK";

                Console.WriteLine("The case has been replied by " + name);

                Console.WriteLine("The reply has been sent to player.");

            }

        }

    }

}

上一页  1 2 3 4 5 6  下一页

Tags:废话 设计模式 十七

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接