其他分享
首页 > 其他分享> > 代理模式 请勿饮酒

代理模式 请勿饮酒

作者:互联网

行车不规范,情人两行泪!

代理模式:

 public interface ICar
    {
        void Driver();
    }

    public class Car : ICar
    {
        public void Driver()
        {
            Console.WriteLine("Car is running");
        }
    }
    public enum State 
    {
        Normal,//正常
        Fatigue,//疲劳
        drinking //饮酒
    }
    public record Dirver(string Name , int Age, State state);
    public class CarProxy : ICar
    {
        private Dirver dirver;

        public CarProxy(Dirver dirver)
        {
            this.dirver = dirver;
        }

        public void Driver()
        {
            switch (dirver.state)
            {
                case State.Normal:
                    new Car().Driver();
                    break;
                case State.Fatigue:
                    Console.WriteLine($"Stop the Car!Name:{dirver.Name},Age:{dirver.Age} Please do not drive tired!!!");
                    break;
                case State.drinking:
                    Console.WriteLine($"Police!!!Name:{dirver.Name},Age:{dirver.Age} Warning!Don't drive after drinking!!!");
                    break;
                default:
                    break;
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var dirver1 = new Dirver("Mr.Zhang", 17, State.drinking);
            var dirver2 = new Dirver("Mr.Wang", 18, State.Normal);
            var dirver3 = new Dirver("Mr.Fang", 19, State.Fatigue);
            CarProxy carProxy = new CarProxy(dirver1);
            carProxy.Driver();
            carProxy = new CarProxy(dirver2);
            carProxy.Driver();
            carProxy = new CarProxy(dirver3);
            carProxy.Driver();
        }
    }

 

标签:dirver,Driver,代理,State,CarProxy,请勿,new,饮酒,public
来源: https://www.cnblogs.com/Zingu/p/16291912.html