Car.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace StatePattern
  7. {
  8. /// <summary>
  9. /// 汽车
  10. /// </summary>
  11. public class Car
  12. {
  13. public string Name { get; set; }
  14. public Car()
  15. {
  16. this.CurrentCarState = StopState;//初始状态为停车状态
  17. }
  18. internal static ICarState StopState = new StopState();
  19. internal static ICarState RunState = new RuningState();
  20. internal static ICarState SpeedDownState = new SpeedDownState();
  21. internal static ICarState SpeedUpState = new SpeedUpState();
  22. public ICarState CurrentCarState { get; set; }
  23. public void Run()
  24. {
  25. this.CurrentCarState.Drive(this);
  26. }
  27. public void Stop()
  28. {
  29. this.CurrentCarState.Stop(this);
  30. }
  31. public void SpeedUp()
  32. {
  33. this.CurrentCarState.SpeedUp(this);
  34. }
  35. public void SpeedDown()
  36. {
  37. this.CurrentCarState.SpeedDown(this);
  38. }
  39. }
  40. }