Skip to content

Commit c4969e3

Browse files
committed
State: improve conceptual example
1 parent 59aa01c commit c4969e3

File tree

2 files changed

+83
-38
lines changed

2 files changed

+83
-38
lines changed

State.Conceptual/Output.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
Context: Transition to RefactoringGuru.DesignPatterns.State.Conceptual.ConcreteStateA.
1+
Context: Transition to ConcreteStateA.
22
ConcreteStateA handles request1.
33
ConcreteStateA wants to change the state of the context.
4-
Context: Transition to RefactoringGuru.DesignPatterns.State.Conceptual.ConcreteStateB.
4+
Context: Transition to ConcreteStateB.
55
ConcreteStateB handles request2.
66
ConcreteStateB wants to change the state of the context.
7-
Context: Transition to RefactoringGuru.DesignPatterns.State.Conceptual.ConcreteStateA.
7+
Context: Transition to ConcreteStateA.

State.Conceptual/Program.cs

Lines changed: 80 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,138 @@
1-
using System;
1+
// EN: State Design Pattern
2+
//
3+
// Intent: Allow an object to alter its behavior when its internal state
4+
// changes. The object will appear to change its class.
5+
//
6+
// RU: Паттерн Состояние
7+
//
8+
// Назначение: Позволяет объекту менять поведение при изменении его внутреннего
9+
// состояния. Со стороны может казаться, что объект меняет свой класс.
10+
11+
using System;
212

313
namespace RefactoringGuru.DesignPatterns.State.Conceptual
414
{
15+
// EN: The Context defines the interface of interest to clients. It also
16+
// maintains a reference to an instance of a State subclass, which represents
17+
// the current state of the Context.
18+
//
19+
// RU: Контекст определяет интерфейс, представляющий интерес для клиентов. Он
20+
// также хранит ссылку на экземпляр подкласса Состояния, который отображает
21+
// текущее состояние Контекста.
522
class Context
623
{
24+
// EN: A reference to the current state of the Context.
25+
//
26+
// RU: Ссылка на текущее состояние Контекста.
727
private State _state = null;
828

929
public Context(State state)
1030
{
11-
this.transitionTo(state);
31+
this.TransitionTo(state);
1232
}
1333

14-
public void transitionTo(State state)
34+
// EN: The Context allows changing the State object at runtime.
35+
//
36+
// RU: Контекст позволяет изменять объект Состояния во время выполнения.
37+
public void TransitionTo(State state)
1538
{
16-
Console.Write("Context: Transition to " + state.ToString() + ".\n");
39+
Console.WriteLine($"Context: Transition to {state.GetType().Name}.");
1740
this._state = state;
18-
this._state.setContext(this);
41+
this._state.SetContext(this);
1942
}
2043

21-
public void request1()
44+
// EN: The Context delegates part of its behavior to the current State
45+
// object.
46+
//
47+
// RU: Контекст делегирует часть своего поведения текущему объекту
48+
// Состояния.
49+
public void Request1()
2250
{
23-
this._state.handle1();
51+
this._state.Handle1();
2452
}
2553

26-
public void request2()
54+
public void Request2()
2755
{
28-
this._state.handle2();
56+
this._state.Handle2();
2957
}
3058
}
3159

60+
// EN: The base State class declares methods that all Concrete State should
61+
// implement and also provides a backreference to the Context object, associated
62+
// with the State. This backreference can be used by States to transition the
63+
// Context to another State.
64+
//
65+
// RU: Базовый класс Состояния объявляет методы, которые должны реализовать все
66+
// Конкретные Состояния, а также предоставляет обратную ссылку на объект
67+
// Контекст, связанный с Состоянием. Эта обратная ссылка может использоваться
68+
// Состояниями для передачи Контекста другому Состоянию.
3269
abstract class State
3370
{
34-
protected Context context;
71+
protected Context _context;
3572

36-
public void setContext(Context context)
73+
public void SetContext(Context context)
3774
{
38-
this.context = context;
75+
this._context = context;
3976
}
4077

41-
public abstract void handle1();
78+
public abstract void Handle1();
4279

43-
public abstract void handle2();
80+
public abstract void Handle2();
4481
}
4582

83+
// EN: Concrete States implement various behaviors, associated with a state of
84+
// the Context.
85+
//
86+
// RU: Конкретные Состояния реализуют различные модели поведения, связанные с
87+
// состоянием Контекста.
4688
class ConcreteStateA : State
4789
{
48-
public override void handle1()
90+
public override void Handle1()
4991
{
50-
Console.Write("ConcreteStateA handles request1.\n");
51-
Console.Write("ConcreteStateA wants to change the state of the context.\n");
52-
this.context.transitionTo(new ConcreteStateB());
92+
Console.WriteLine("ConcreteStateA handles request1.");
93+
Console.WriteLine("ConcreteStateA wants to change the state of the context.");
94+
this._context.TransitionTo(new ConcreteStateB());
5395
}
5496

55-
public override void handle2()
97+
public override void Handle2()
5698
{
57-
Console.Write("ConcreteStateA handles request2.\n");
99+
Console.WriteLine("ConcreteStateA handles request2.");
58100
}
59101
}
60102

61103
class ConcreteStateB : State
62104
{
63-
public override void handle1()
105+
public override void Handle1()
64106
{
65-
Console.Write("ConcreteStateB handles request1.\n");
107+
Console.Write("ConcreteStateB handles request1.");
66108
}
67109

68-
public override void handle2()
110+
public override void Handle2()
69111
{
70-
Console.Write("ConcreteStateB handles request2.\n");
71-
Console.Write("ConcreteStateB wants to change the state of the context.\n");
72-
this.context.transitionTo(new ConcreteStateA());
112+
Console.WriteLine("ConcreteStateB handles request2.");
113+
Console.WriteLine("ConcreteStateB wants to change the state of the context.");
114+
this._context.TransitionTo(new ConcreteStateA());
73115
}
74116
}
75117

76-
class Program
118+
class Client
77119
{
78-
static void Main(string[] args)
120+
public static void ClientCode()
79121
{
80-
Client.ClientCode();
122+
// EN: The client code.
123+
//
124+
// RU: Клиентский код.
125+
var context = new Context(new ConcreteStateA());
126+
context.Request1();
127+
context.Request2();
81128
}
82129
}
83-
84-
class Client
130+
131+
class Program
85132
{
86-
public static void ClientCode()
133+
static void Main(string[] args)
87134
{
88-
var context = new Context(new ConcreteStateA());
89-
context.request1();
90-
context.request2();
135+
Client.ClientCode();
91136
}
92137
}
93138
}

0 commit comments

Comments
 (0)