Help me figure out the State pattern. There is a task - to write a program that simulates turning on and off the PC. I was prompted to use the State pattern. But I'm not sure that I use it correctly. My turn pc model has things in common with a real one, but certainly not for sure. Therefore, I divided the boot process into several stages - turning on the computer, boot loaders 1-3 level and loading the kernel os. Program structure.
At each stage, their actions are performed, in addition to implementing the methods of the implemented interface. In the implementation of the interface methods, I assign a new object to the state of the pc. I do this because I saw a similar action in the example. But I don’t understand how to use it later.
Interface:
internal interface IPCState
{
bool TurnON(Computer computer);
bool TurnOff(Computer computer);
}
An example implementation of the method in one of the stages:
// Level 1 bootloader.Master boot record
internal class LoaderOfTheFirstLevel : IPCState
{
public bool TurnOff(Computer computer)
{
throw new NotImplementedException();
}
public bool TurnON(Computer computer)
{
// If the self-testing stage has been successfully completed, this means that the processes of the first stage are completed successfully we move on
if (POST())
{
computer.State = new LoaderOfTheSecondLevel();
return true;
}
return false;
}
// Self-test: we check hardware failures, one signal after POST signals that everything is in order.
private bool POST() => true;
}
In the main method, I call the instance constructor of each stage and put the result in a list. And if at the end all the stages worked without errors - the PC is started and ready for use. Thanks in advance.
Main:
static void Main()
{
List<bool> operationRes = new List<bool>();
PowerSocket.SetElectricityState(true);
Computer computer = new Computer(new SystemStartup());
operationRes.Add(computer.TurnOn());
computer = new Computer(new LoaderOfTheFirstLevel());
operationRes.Add(computer.TurnOn());
computer = new Computer(new LoaderOfTheSecondLevel(operationRes.Last()));
operationRes.Add(computer.TurnOn());
computer = new Computer(new LoaderOfTheThirdLevel(operationRes.Last()));
operationRes.Add(computer.TurnOn());
computer = new Computer(new BootLoaderOSKernel(operationRes.Last()));
operationRes.Add(computer.TurnOn());
if (operationRes.All(r => r))
{
Console.WriteLine("OS is ready to work");
}
else
{
Console.WriteLine("Error");
}
Console.ReadLine();
}
Can someone explain what i did wrong?
