设计模式之:命令行模式

按自己对命令模式的理解.进行了一个简单的订餐情形演练,记录一下:

各种设计模式都演练过,但是基本上写过就过了,免得后面写起来遗忘,做一下记录

 

主要是先抽象了一个基类person: {基类派生了服务员,厨师,顾客}, 其中每种做的食物,抽象成一种命令.最后使用的时候,就按照:  顾客进来,点餐-->服务员招待-->通知厨师做饭; 同时维护了一个订单系统,便于服务员随时参看并更新顾客信息;

public class 命令模式_23
    {
        //模拟一下实际的餐厅的运作模式.

        //顾客...

        //餐厅:
        /*
         厨师:负责制作各种种类的食品
         服务员.负责点餐,送餐.
         */

        /*
         设计请求: 接口...抽象类....(命令模式)
         */


        //类似于一串串食物,这些食物包含的个数
        #region 命令(食物)
        public abstract class CommandBase
        {
            public CommandBase(string BarbecueName, int BarbecueNumber)
            {
                this.barbecueName = BarbecueName;
                this.barbecueNumber = BarbecueNumber;
            }
            public CommandBase() { }

            private string barbecueName; //命令名字
            public string BarbecueName { get => barbecueName; set => barbecueName = value; }

            private int barbecueNumber;  //要制作数目.
            public int BarbecueNumber { get => barbecueNumber; set => barbecueNumber = value; }

            private float price;
            public float Price { get => price; set => price = value; }
        }

        //鸡肉
        public class CommandChicken : CommandBase
        {
            public CommandChicken(string barname, int barNumber) : base(barname, barNumber)
            {
                Price = 10;
            }
            public CommandChicken(int barNumber)
            {
                BarbecueNumber = barNumber;
                Price = 10;
                BarbecueName = @"鸡肉";
            }

        }
        //羊肉
        public class CommandMutton : CommandBase
        {
            public CommandMutton(string barname, int barNumber) : base(barname, barNumber)
            {

            }
            public CommandMutton(int barNumber)
            {
                BarbecueNumber = barNumber;
                Price = 20;
                BarbecueName = @"羊肉";
            }
        }

        #endregion

        public abstract class Person
        {
            public string Name;
            public int Age;
            public string ID;
        }

        #region 顾客

        public interface CustomerAction
        {
            void AddCommand(CommandBase command);

            void RemoveCommand(CommandBase command);

        }

        ///顾客
        public abstract class CustomerBase : Person, CustomerAction
        {
            public CustomerBase(string cuscomerName)
            {
                Name = cuscomerName;
            }
            private List<CommandBase> CommandList = new List<CommandBase>();
            public List<CommandBase> orderList { get => CommandList; }

            public void AddCommand(CommandBase command)
            {
                CommandList.Add(command);
            }

            public void RemoveCommand(CommandBase command)
            {
                CommandList.Remove(command);
            }
            public double PayMoney()
            {
                double payNumber = 0;
                foreach (var item in CommandList)
                {
                    payNumber += (item.Price * item.BarbecueNumber);
                }
                CommandList.Clear();
                return payNumber;
            }
        }


        //具体的顾客
        public class ConcreteCustomer : CustomerBase
        {
            public ConcreteCustomer(string customerName) : base(customerName) { }
        }

        #endregion


        #region 服务系统,包括记录顾客,删除顾客.以及更新顾客(此类只对服务员开发)

        //系统的权责.
        public interface IRecordSystem
        {
            void AddCustomer(CustomerBase customer); //添加顾客
            void RemoveCustomer(CustomerBase customer);//删除顾客

            void UpdateCustomer(CustomerBase customer);

            int getCustomerNumber();
        }

        public abstract class RecordSystem : IRecordSystem
        {

            private List<CustomerBase> _CustomerList = new List<CustomerBase>();
            public void AddCustomer(CustomerBase customer)
            {
                _CustomerList.Add(customer);
            }

            public int getCustomerNumber()
            {
                return _CustomerList.Count;
            }

            public void RemoveCustomer(CustomerBase customer)
            {
                _CustomerList.Remove(customer);
            }

            public void UpdateCustomer(CustomerBase customer)
            {
                CustomerBase baseCustomer = _CustomerList.Find(item => item == customer);
                baseCustomer = customer;
            }
        }

        //具体的系统实现.
        public class ConcreteRecordSystem : RecordSystem
        { }

        #endregion


        //服务员权责
        #region 服务员
        public interface WaiterAction
        {
            double CloseMoney(CustomerBase customer);//结算
            void DisplayOrderList(CustomerBase customer);//显示订单

            void AddCustomer(CustomerBase customer);

            void RemoveCustomer(CustomerBase customer);

            void UpdateCustomer(CustomerBase customer);

            void Notify(ChefBase chef);
        }


        //服务员:服务顾客.
        public abstract class WaiterBase : Person, WaiterAction
        {
            public WaiterBase(string name)
            {
                Name = name;
            }
            public double CloseMoney(CustomerBase customer)
            {
                double payvalue = 0;
                foreach (var item in customer.orderList)
                {
                    payvalue += (item.Price * item.BarbecueNumber);
                }
                customer.orderList.Clear();//清空账单
                Console.WriteLine($"服务员: {Name}, 顾客总消费: {payvalue}");
                return payvalue;
            }
            public void DisplayOrderList(CustomerBase customer)
            {
                Console.WriteLine($"{customer.Name}: 消费订单");
                foreach (var item in customer.orderList)
                {
                    Console.WriteLine($"{item.BarbecueName}: {item.BarbecueNumber}");
                }
            }


            /// <summary>
            /// 下面四个函数类似于记账系统.
            /// </summary>
            /// <param name="customer"></param>
           public void AddCustomer(CustomerBase customer)
            {
                RecordSystem.AddCustomer(customer);
            }

            public void RemoveCustomer(CustomerBase customer)
            {
                RecordSystem.RemoveCustomer(customer);
            }

            public void UpdateCustomer(CustomerBase customer)
            {
                RecordSystem.UpdateCustomer(customer);
            }

            public int PerSonCount()
            {
                return RecordSystem.getCustomerNumber();
            }


            private CustomerBase recurrentCustomer;
            /// <summary>
            /// 指定顾客
            /// </summary>
            /// <param name="customer"></param>
            public void orderCustomer(CustomerBase customer)
            {
                recurrentCustomer = customer;
            }
            /// <summary>
            /// 通知厨师做饭
            /// </summary>
            /// <param name="chef"></param>
            public void Notify(ChefBase chef)
            {
               if(recurrentCustomer!=null)  chef.MakeFood(recurrentCustomer);
            }

            //服务员最重要的是通知.服务员通知厨师做饭.


            private static ConcreteRecordSystem RecordSystem = new ConcreteRecordSystem();

        }

        //具体的服务员
        public class ConcreteWaiter : WaiterBase
        {
            public ConcreteWaiter(string name) : base(name) { }
        }

        #endregion


        #region 厨师
        public interface ChefAction
        {
            void MakeFood(CustomerBase customer);
        }
        /// <summary>
        /// 厨师的职责是:做饭, 厨师的请求是通过服务员来获取到的.
        /// </summary>
        public abstract class ChefBase : Person, ChefAction
        {

            public ChefBase(string name)
            {
                Name = name;
            }
            public void MakeFood(CustomerBase customer)
            {
                Console.WriteLine($"\r\n厨师: {Name}  正在做顾客 {customer.Name} 的食物 进度: \r\n");
                foreach (var item in customer.orderList)
                {
                    Console.WriteLine($"{item.BarbecueName} {item.BarbecueNumber} 完成");
                }
            }
        }

        public class ConcreteChef : ChefBase
        {
            public ConcreteChef(string name) : base(name) { }

        }


        #endregion


        public static void TestData()
        {

            ConsoleFormate.consoleFormate(() =>
            {
                //创建食物指令.
                CommandBase comman1 = new CommandChicken(12);
                CommandBase comman2 = new CommandChicken("鸡肉", 33);
                CommandBase comman3 = new CommandMutton(5);
                CommandBase comman4 = new CommandMutton("羊肉", 23);

                //创建两个顾客
                CustomerBase customer1 = new ConcreteCustomer("张三");
                {
                    //点餐:
                    customer1.AddCommand(comman1);
                    customer1.AddCommand(comman2);
                    customer1.AddCommand(comman2);
                    customer1.AddCommand(comman1);
                }

                CustomerBase customer2 = new ConcreteCustomer("李四");
                {
                    customer2.AddCommand(comman1);
                    customer2.AddCommand(comman2);
                    customer2.AddCommand(comman2);
                }

                //创建服务员
                WaiterBase waiter = new ConcreteWaiter("林轩");
                WaiterBase waiter2 = new ConcreteWaiter("张珊珊");

                //登记.
                waiter.AddCustomer(customer1);
                waiter.AddCustomer(customer2);

                //登记.
                waiter2.AddCustomer(customer1);
                waiter2.AddCustomer(customer1);

                Console.WriteLine($"当前还有顾客 {waiter.PerSonCount()} 位");

                waiter.DisplayOrderList(customer1);
                waiter.DisplayOrderList(customer2);
              
                //厨师做饭.
                ChefBase chefBase = new ConcreteChef("四儿");
                waiter.orderCustomer(customer1);
                waiter.Notify(chefBase);//通知厨师做饭

                waiter.CloseMoney(customer1);
                waiter.CloseMoney(customer2);

            }, @"复杂命令形式");

        }

    }

设计模式用大白话讲,就是提取不变的,封装可变的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值