Skip to content

Commit ab4e53a

Browse files
committed
iluwatar#590 add explanation for State
1 parent b907a2a commit ab4e53a

1 file changed

Lines changed: 120 additions & 2 deletions

File tree

‎state/README.md‎

Lines changed: 120 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,126 @@ tags:
1212
Objects for States
1313

1414
## Intent
15-
Allow an object to alter its behavior when its internal state
16-
changes. The object will appear to change its class.
15+
Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.
16+
17+
## Explanation
18+
Real world example
19+
20+
> When observing a mammoth in its natural habitat it seems to change its behavior based on the situation. It may first appear calm but over time when it detects a threat it gets angry and dangerous to its surroundings.
21+
22+
In plain words
23+
24+
> State pattern allows an object to change its behavior.
25+
26+
Wikipedia says
27+
28+
> The state pattern is a behavioral software design pattern that allows an object to alter its behavior when its internal state changes. This pattern is close to the concept of finite-state machines. The state pattern can be interpreted as a strategy pattern, which is able to switch a strategy through invocations of methods defined in the pattern's interface.
29+
30+
**Programmatic Example**
31+
32+
Here is the state interface and its concrete implementations.
33+
34+
```java
35+
public interface State {
36+
37+
void onEnterState();
38+
39+
void observe();
40+
}
41+
42+
public class PeacefulState implements State {
43+
44+
private static final Logger LOGGER = LoggerFactory.getLogger(PeacefulState.class);
45+
46+
private Mammoth mammoth;
47+
48+
public PeacefulState(Mammoth mammoth) {
49+
this.mammoth = mammoth;
50+
}
51+
52+
@Override
53+
public void observe() {
54+
LOGGER.info("{} is calm and peaceful.", mammoth);
55+
}
56+
57+
@Override
58+
public void onEnterState() {
59+
LOGGER.info("{} calms down.", mammoth);
60+
}
61+
}
62+
63+
public class AngryState implements State {
64+
65+
private static final Logger LOGGER = LoggerFactory.getLogger(AngryState.class);
66+
67+
private Mammoth mammoth;
68+
69+
public AngryState(Mammoth mammoth) {
70+
this.mammoth = mammoth;
71+
}
72+
73+
@Override
74+
public void observe() {
75+
LOGGER.info("{} is furious!", mammoth);
76+
}
77+
78+
@Override
79+
public void onEnterState() {
80+
LOGGER.info("{} gets angry!", mammoth);
81+
}
82+
}
83+
```
84+
85+
And here is the mammoth containing the state.
86+
87+
```java
88+
public class Mammoth {
89+
90+
private State state;
91+
92+
public Mammoth() {
93+
state = new PeacefulState(this);
94+
}
95+
96+
public void timePasses() {
97+
if (state.getClass().equals(PeacefulState.class)) {
98+
changeStateTo(new AngryState(this));
99+
} else {
100+
changeStateTo(new PeacefulState(this));
101+
}
102+
}
103+
104+
private void changeStateTo(State newState) {
105+
this.state = newState;
106+
this.state.onEnterState();
107+
}
108+
109+
@Override
110+
public String toString() {
111+
return "The mammoth";
112+
}
113+
114+
public void observe() {
115+
this.state.observe();
116+
}
117+
}
118+
```
119+
120+
And here is the full example how the mammoth behaves over time.
121+
122+
```java
123+
var mammoth = new Mammoth();
124+
mammoth.observe();
125+
mammoth.timePasses();
126+
mammoth.observe();
127+
mammoth.timePasses();
128+
mammoth.observe();
129+
130+
// The mammoth gets angry!
131+
// The mammoth is furious!
132+
// The mammoth calms down.
133+
// The mammoth is calm and peaceful.
134+
```
17135

18136
## Class diagram
19137
![alt text](./etc/state_1.png "State")

0 commit comments

Comments
 (0)