예제 #1
0
        // This program demonstrates the state design pattern by using an electric car as a subject
        // to 4 states: Uncharged, minimally charged, low charged, normal charged. Each state prints
        // different strings.
        public static void Main(string[] args)
        {
            ElectricCar aCar = new ElectricCar();

            // Uncharged level by default
            Console.WriteLine(aCar.CurrentState.TurnOnEngine());
            Console.WriteLine(aCar.CurrentState.TurnOnHazardLights());
            Console.WriteLine(aCar.CurrentState.TurnOnAirConditioner());
            Console.WriteLine(aCar.CurrentState.TurnOnRadio());
            Console.WriteLine("\n\n");

            // Minimal charged state
            aCar.CurrentState.Charge();
            Console.WriteLine(aCar.CurrentState.TurnOnEngine());
            Console.WriteLine(aCar.CurrentState.TurnOnHazardLights());
            Console.WriteLine(aCar.CurrentState.TurnOnAirConditioner());
            Console.WriteLine(aCar.CurrentState.TurnOnRadio());
            Console.WriteLine("\n\n");

            // Low Charged state
            aCar.CurrentState.Charge();
            Console.WriteLine(aCar.CurrentState.TurnOnEngine());
            Console.WriteLine(aCar.CurrentState.TurnOnHazardLights());
            Console.WriteLine(aCar.CurrentState.TurnOnAirConditioner());
            Console.WriteLine(aCar.CurrentState.TurnOnRadio());
            Console.WriteLine("\n\n");

            // Normal Charged state
            aCar.CurrentState.Charge();
            Console.WriteLine(aCar.CurrentState.TurnOnEngine());
            Console.WriteLine(aCar.CurrentState.TurnOnHazardLights());
            Console.WriteLine(aCar.CurrentState.TurnOnAirConditioner());
            Console.WriteLine(aCar.CurrentState.TurnOnRadio());
            Console.WriteLine("\n\n");

            // Uncharged back to low charged state
            aCar.CurrentState.Uncharge();
            Console.WriteLine(aCar.CurrentState.TurnOnEngine());
            Console.WriteLine(aCar.CurrentState.TurnOnHazardLights());
            Console.WriteLine(aCar.CurrentState.TurnOnAirConditioner());
            Console.WriteLine(aCar.CurrentState.TurnOnRadio());

            Console.ReadLine();
        }
 // Constructor
 public MinimallyChargedState(ElectricCar car)
 {
     this.aCar = car;
 }
예제 #3
0
 // Constructor
 public LowChargedState(ElectricCar car)
 {
     this.aCar = car;
 }
예제 #4
0
 // Constructor
 public UnchargedState(ElectricCar car)
 {
     this.aCar = car;
 }
예제 #5
0
 public ElectricCar(ElectricCar car)
 {
     this.CurrentState = car.CurrentState;
 }
예제 #6
0
 // Constructor
 public NormalChargedState(ElectricCar car)
 {
     this.aCar = car;
 }