行车不规范,情人两行泪!
代理模式:
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(); } }