程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 無廢話C#設計模式之十七:Chain Of Resp(2)

無廢話C#設計模式之十七:Chain Of Resp(2)

編輯:關於C語言

示例代碼

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. 下一頁:
Copyright © 程式師世界 All Rights Reserved