class Program static void Main Context context new Context new StateA

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Program
{
static void Main()
{
Context context = new Context(new StateA());
context.Request(); // Переход в состояние StateB
context.Request(); // Переход в состояние StateA
}
}
abstract class State
{
public abstract void Handle(Context context);
}
class StateA : State
{
public override void Handle(Context context)
{
context.State = new StateB();
}
}
class StateB : State
{
public override void Handle(Context context)
{
context.State = new StateA();
}
}
class Context
{
public State State { get; set; }
public Context(State state)
{
this.State = state;
}
public void Request()
{
this.State.Handle(this);
}
}