Add code examples, and documentation
This commit is contained in:
33
patterns/state/CarState/States/Driving.cs
Normal file
33
patterns/state/CarState/States/Driving.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
#region
|
||||
|
||||
using System;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace CarState.States;
|
||||
|
||||
public class Driving : State
|
||||
{
|
||||
public Driving(Car car)
|
||||
: base(car) { }
|
||||
|
||||
public override void TurnKeyClockwise()
|
||||
{
|
||||
Console.WriteLine("You hear a grinding noise...");
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
public override void TurnKeyCounterClockwise()
|
||||
{
|
||||
Console.WriteLine("The engine turns off, and the car stops suddenly");
|
||||
Console.WriteLine();
|
||||
_car.ChangeState(new Off(_car));
|
||||
}
|
||||
|
||||
public override void PressBrake()
|
||||
{
|
||||
Console.WriteLine("The car slows to a stop");
|
||||
Console.WriteLine();
|
||||
_car.ChangeState(new Stopped(_car));
|
||||
}
|
||||
}
|
||||
20
patterns/state/CarState/States/Off.cs
Normal file
20
patterns/state/CarState/States/Off.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
#region
|
||||
|
||||
using System;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace CarState.States;
|
||||
|
||||
public class Off : State
|
||||
{
|
||||
public Off(Car car)
|
||||
: base(car) { }
|
||||
|
||||
public override void TurnKeyClockwise()
|
||||
{
|
||||
Console.WriteLine("The engine has started");
|
||||
Console.WriteLine();
|
||||
_car.ChangeState(new Stopped(_car));
|
||||
}
|
||||
}
|
||||
19
patterns/state/CarState/States/State.cs
Normal file
19
patterns/state/CarState/States/State.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
namespace CarState.States;
|
||||
|
||||
public class State
|
||||
{
|
||||
protected Car _car;
|
||||
|
||||
public State(Car car)
|
||||
{
|
||||
_car = car;
|
||||
}
|
||||
|
||||
public virtual void TurnKeyClockwise() { }
|
||||
|
||||
public virtual void TurnKeyCounterClockwise() { }
|
||||
|
||||
public virtual void PressGas() { }
|
||||
|
||||
public virtual void PressBrake() { }
|
||||
}
|
||||
33
patterns/state/CarState/States/Stopped.cs
Normal file
33
patterns/state/CarState/States/Stopped.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
#region
|
||||
|
||||
using System;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace CarState.States;
|
||||
|
||||
public class Stopped : State
|
||||
{
|
||||
public Stopped(Car car)
|
||||
: base(car) { }
|
||||
|
||||
public override void PressGas()
|
||||
{
|
||||
Console.WriteLine("The car begins to drive");
|
||||
Console.WriteLine();
|
||||
_car.ChangeState(new Driving(_car));
|
||||
}
|
||||
|
||||
public override void TurnKeyClockwise()
|
||||
{
|
||||
Console.WriteLine("You hear a grinding noise...");
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
public override void TurnKeyCounterClockwise()
|
||||
{
|
||||
Console.WriteLine("The engine has stopped");
|
||||
Console.WriteLine();
|
||||
_car.ChangeState(new Off(_car));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user