Add code examples, and documentation
This commit is contained in:
6
patterns/state/BadCarState/BadCarState.csproj
Normal file
6
patterns/state/BadCarState/BadCarState.csproj
Normal file
@@ -0,0 +1,6 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
81
patterns/state/BadCarState/Car.cs
Normal file
81
patterns/state/BadCarState/Car.cs
Normal file
@@ -0,0 +1,81 @@
|
||||
#region
|
||||
|
||||
using System;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace BadCarState;
|
||||
|
||||
public class Car
|
||||
{
|
||||
private State _state = State.Off;
|
||||
|
||||
public bool HandleInput(string input)
|
||||
{
|
||||
switch (input)
|
||||
{
|
||||
case "turn-clockwise":
|
||||
switch (_state)
|
||||
{
|
||||
case State.Off:
|
||||
Console.WriteLine("The engine has started");
|
||||
Console.WriteLine();
|
||||
_state = State.Stopped;
|
||||
break;
|
||||
case State.Stopped:
|
||||
case State.Driving:
|
||||
Console.WriteLine("You hear a grinding noise...");
|
||||
Console.WriteLine();
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case "turn-counter":
|
||||
switch (_state)
|
||||
{
|
||||
case State.Stopped:
|
||||
Console.WriteLine("The engine has stopped");
|
||||
Console.WriteLine();
|
||||
_state = State.Off;
|
||||
break;
|
||||
case State.Driving:
|
||||
Console.WriteLine(
|
||||
"The engine turns off, and the car stops suddenly"
|
||||
);
|
||||
Console.WriteLine();
|
||||
_state = State.Off;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case "gas":
|
||||
switch (_state)
|
||||
{
|
||||
case State.Stopped:
|
||||
Console.WriteLine("The car begins to drive");
|
||||
Console.WriteLine();
|
||||
_state = State.Driving;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case "brake":
|
||||
switch (_state)
|
||||
{
|
||||
case State.Driving:
|
||||
Console.WriteLine("The car slows to a stop");
|
||||
Console.WriteLine();
|
||||
_state = State.Stopped;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case "exit":
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private enum State
|
||||
{
|
||||
Off,
|
||||
Stopped,
|
||||
Driving,
|
||||
}
|
||||
}
|
||||
41
patterns/state/BadCarState/Program.cs
Normal file
41
patterns/state/BadCarState/Program.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
#region
|
||||
|
||||
using System;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace BadCarState;
|
||||
|
||||
internal class Program
|
||||
{
|
||||
private static void Main(string[] args)
|
||||
{
|
||||
var car = new Car();
|
||||
|
||||
string input;
|
||||
bool keepRunning = true;
|
||||
|
||||
Console.WriteLine("Welcome to the Driving Simulater");
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("Usage:");
|
||||
Console.WriteLine(" turn-clockwise -Turns the car's key clockwise");
|
||||
Console.WriteLine(
|
||||
" turn-counter -Turns the car's key counter clockwise"
|
||||
);
|
||||
Console.WriteLine(" gas -Presses the gas pedal");
|
||||
Console.WriteLine(" brake -Presses the brake pedal");
|
||||
Console.WriteLine(" exit -Exits the simulator");
|
||||
Console.WriteLine();
|
||||
|
||||
do
|
||||
{
|
||||
Console.Write("> ");
|
||||
input = Console.ReadLine();
|
||||
Console.WriteLine();
|
||||
keepRunning = car.HandleInput(input);
|
||||
} while (keepRunning);
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("goodbye");
|
||||
Console.WriteLine();
|
||||
}
|
||||
}
|
||||
44
patterns/state/CarState/Car.cs
Normal file
44
patterns/state/CarState/Car.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
#region
|
||||
|
||||
using CarState.States;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace CarState;
|
||||
|
||||
public class Car
|
||||
{
|
||||
private State _state;
|
||||
|
||||
public Car()
|
||||
{
|
||||
ChangeState(new Off(this));
|
||||
}
|
||||
|
||||
public bool HandleInput(string input)
|
||||
{
|
||||
switch (input)
|
||||
{
|
||||
case "turn-clockwise":
|
||||
_state.TurnKeyClockwise();
|
||||
break;
|
||||
case "turn-counter":
|
||||
_state.TurnKeyCounterClockwise();
|
||||
break;
|
||||
case "gas":
|
||||
_state.PressGas();
|
||||
break;
|
||||
case "brake":
|
||||
_state.PressBrake();
|
||||
break;
|
||||
case "exit":
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void ChangeState(State state)
|
||||
{
|
||||
_state = state;
|
||||
}
|
||||
}
|
||||
6
patterns/state/CarState/CarState.csproj
Normal file
6
patterns/state/CarState/CarState.csproj
Normal file
@@ -0,0 +1,6 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
41
patterns/state/CarState/Program.cs
Normal file
41
patterns/state/CarState/Program.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
#region
|
||||
|
||||
using System;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace CarState;
|
||||
|
||||
internal class Program
|
||||
{
|
||||
private static void Main(string[] args)
|
||||
{
|
||||
var car = new Car();
|
||||
|
||||
string input;
|
||||
bool keepRunning = true;
|
||||
|
||||
Console.WriteLine("Welcome to the Driving Simulater");
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("Usage:");
|
||||
Console.WriteLine(" turn-clockwise -Turns the car's key clockwise");
|
||||
Console.WriteLine(
|
||||
" turn-counter -Turns the car's key counter clockwise"
|
||||
);
|
||||
Console.WriteLine(" gas -Presses the gas pedal");
|
||||
Console.WriteLine(" brake -Presses the brake pedal");
|
||||
Console.WriteLine(" exit -Exits the simulator");
|
||||
Console.WriteLine();
|
||||
|
||||
do
|
||||
{
|
||||
Console.Write("> ");
|
||||
input = Console.ReadLine();
|
||||
Console.WriteLine();
|
||||
keepRunning = car.HandleInput(input);
|
||||
} while (keepRunning);
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("goodbye");
|
||||
Console.WriteLine();
|
||||
}
|
||||
}
|
||||
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));
|
||||
}
|
||||
}
|
||||
44
patterns/state/WithStatePattern/Car.cs
Normal file
44
patterns/state/WithStatePattern/Car.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
#region
|
||||
|
||||
using WithStatePattern.States;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace WithStatePattern;
|
||||
|
||||
public class Car
|
||||
{
|
||||
private State _state;
|
||||
|
||||
public Car()
|
||||
{
|
||||
ChangeState(new Off(this));
|
||||
}
|
||||
|
||||
public bool HandleInput(string input)
|
||||
{
|
||||
switch (input)
|
||||
{
|
||||
case "turn-clockwise":
|
||||
_state.TurnKeyClockwise();
|
||||
break;
|
||||
case "turn-counter":
|
||||
_state.TurnKeyCounterClockwise();
|
||||
break;
|
||||
case "gas":
|
||||
_state.PressGas();
|
||||
break;
|
||||
case "brake":
|
||||
_state.PressBrake();
|
||||
break;
|
||||
case "exit":
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void ChangeState(State state)
|
||||
{
|
||||
_state = state;
|
||||
}
|
||||
}
|
||||
35
patterns/state/WithStatePattern/Program.cs
Normal file
35
patterns/state/WithStatePattern/Program.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
namespace WithStatePattern;
|
||||
|
||||
public class Program
|
||||
{
|
||||
private static void Main(string[] args)
|
||||
{
|
||||
var car = new Car();
|
||||
|
||||
string input;
|
||||
bool keepRunning = true;
|
||||
|
||||
Console.WriteLine("Welcome to the Driving Simulater");
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("Usage:");
|
||||
Console.WriteLine(" turn-clockwise -Turns the car's key clockwise");
|
||||
Console.WriteLine(
|
||||
" turn-counter -Turns the car's key counter clockwise"
|
||||
);
|
||||
Console.WriteLine(" gas -Presses the gas pedal");
|
||||
Console.WriteLine(" brake -Presses the brake pedal");
|
||||
Console.WriteLine(" exit -Exits the simulator");
|
||||
Console.WriteLine();
|
||||
|
||||
do
|
||||
{
|
||||
Console.Write("> ");
|
||||
input = Console.ReadLine();
|
||||
Console.WriteLine();
|
||||
keepRunning = car.HandleInput(input);
|
||||
} while (keepRunning);
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("goodbye");
|
||||
Console.WriteLine();
|
||||
}
|
||||
}
|
||||
27
patterns/state/WithStatePattern/States/Driving.cs
Normal file
27
patterns/state/WithStatePattern/States/Driving.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
namespace WithStatePattern.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));
|
||||
}
|
||||
}
|
||||
14
patterns/state/WithStatePattern/States/Off.cs
Normal file
14
patterns/state/WithStatePattern/States/Off.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
namespace WithStatePattern.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/WithStatePattern/States/State.cs
Normal file
19
patterns/state/WithStatePattern/States/State.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
namespace WithStatePattern.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() { }
|
||||
}
|
||||
27
patterns/state/WithStatePattern/States/Stopped.cs
Normal file
27
patterns/state/WithStatePattern/States/Stopped.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
namespace WithStatePattern.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));
|
||||
}
|
||||
}
|
||||
8
patterns/state/WithStatePattern/WithStatePattern.csproj
Normal file
8
patterns/state/WithStatePattern/WithStatePattern.csproj
Normal file
@@ -0,0 +1,8 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
75
patterns/state/WithoutStatePattern/Car.cs
Normal file
75
patterns/state/WithoutStatePattern/Car.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
namespace WithoutStatePattern;
|
||||
|
||||
public class Car
|
||||
{
|
||||
private State _state = State.Off;
|
||||
|
||||
public bool HandleInput(string input)
|
||||
{
|
||||
switch (input)
|
||||
{
|
||||
case "turn-clockwise":
|
||||
switch (_state)
|
||||
{
|
||||
case State.Off:
|
||||
Console.WriteLine("The engine has started");
|
||||
Console.WriteLine();
|
||||
_state = State.Stopped;
|
||||
break;
|
||||
case State.Stopped:
|
||||
case State.Driving:
|
||||
Console.WriteLine("You hear a grinding noise...");
|
||||
Console.WriteLine();
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case "turn-counter":
|
||||
switch (_state)
|
||||
{
|
||||
case State.Stopped:
|
||||
Console.WriteLine("The engine has stopped");
|
||||
Console.WriteLine();
|
||||
_state = State.Off;
|
||||
break;
|
||||
case State.Driving:
|
||||
Console.WriteLine(
|
||||
"The engine turns off, and the car stops suddenly"
|
||||
);
|
||||
Console.WriteLine();
|
||||
_state = State.Off;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case "gas":
|
||||
switch (_state)
|
||||
{
|
||||
case State.Stopped:
|
||||
Console.WriteLine("The car begins to drive");
|
||||
Console.WriteLine();
|
||||
_state = State.Driving;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case "brake":
|
||||
switch (_state)
|
||||
{
|
||||
case State.Driving:
|
||||
Console.WriteLine("The car slows to a stop");
|
||||
Console.WriteLine();
|
||||
_state = State.Stopped;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case "exit":
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private enum State
|
||||
{
|
||||
Off,
|
||||
Stopped,
|
||||
Driving,
|
||||
}
|
||||
}
|
||||
35
patterns/state/WithoutStatePattern/Program.cs
Normal file
35
patterns/state/WithoutStatePattern/Program.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
namespace WithoutStatePattern;
|
||||
|
||||
internal class Program
|
||||
{
|
||||
private static void Main(string[] args)
|
||||
{
|
||||
var car = new Car();
|
||||
|
||||
string input;
|
||||
bool keepRunning = true;
|
||||
|
||||
Console.WriteLine("Welcome to the Driving Simulater");
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("Usage:");
|
||||
Console.WriteLine(" turn-clockwise -Turns the car's key clockwise");
|
||||
Console.WriteLine(
|
||||
" turn-counter -Turns the car's key counter clockwise"
|
||||
);
|
||||
Console.WriteLine(" gas -Presses the gas pedal");
|
||||
Console.WriteLine(" brake -Presses the brake pedal");
|
||||
Console.WriteLine(" exit -Exits the simulator");
|
||||
Console.WriteLine();
|
||||
|
||||
do
|
||||
{
|
||||
Console.Write("> ");
|
||||
input = Console.ReadLine();
|
||||
Console.WriteLine();
|
||||
keepRunning = car.HandleInput(input);
|
||||
} while (keepRunning);
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("goodbye");
|
||||
Console.WriteLine();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
Reference in New Issue
Block a user