Skip to content

Commit 9166d47

Browse files
committed
Added state pattern sample
1 parent b3d48cc commit 9166d47

File tree

7 files changed

+123
-0
lines changed

7 files changed

+123
-0
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
<module>mediator</module>
3838
<module>memento</module>
3939
<module>observer</module>
40+
<module>state</module>
4041
</modules>
4142

4243
<build>

state/pom.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0"?>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>com.iluwatar</groupId>
7+
<artifactId>java-design-patterns</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<groupId>com.iluwatar</groupId>
11+
<artifactId>state</artifactId>
12+
<version>1.0-SNAPSHOT</version>
13+
<name>state</name>
14+
<url>http://maven.apache.org</url>
15+
<dependencies>
16+
<dependency>
17+
<groupId>junit</groupId>
18+
<artifactId>junit</artifactId>
19+
<version>3.8.1</version>
20+
<scope>test</scope>
21+
</dependency>
22+
</dependencies>
23+
</project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.iluwatar;
2+
3+
public class AngryState implements State {
4+
5+
private Mammoth mammoth;
6+
7+
public AngryState(Mammoth mammoth) {
8+
this.mammoth = mammoth;
9+
}
10+
11+
@Override
12+
public void observe() {
13+
System.out.println(String.format("%s is furious!", mammoth));
14+
}
15+
16+
@Override
17+
public void onEnterState() {
18+
System.out.println(String.format("%s gets angry!", mammoth));
19+
}
20+
21+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.iluwatar;
2+
3+
public class App
4+
{
5+
public static void main( String[] args )
6+
{
7+
8+
Mammoth mammoth = new Mammoth();
9+
mammoth.observe();
10+
mammoth.timePasses();
11+
mammoth.observe();
12+
mammoth.timePasses();
13+
mammoth.observe();
14+
15+
}
16+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.iluwatar;
2+
3+
public class Mammoth {
4+
5+
private State state;
6+
7+
public Mammoth() {
8+
state = new PeacefulState(this);
9+
}
10+
11+
public void timePasses() {
12+
if (state.getClass().equals(PeacefulState.class)) {
13+
changeStateTo(new AngryState(this));
14+
} else {
15+
changeStateTo(new PeacefulState(this));
16+
}
17+
}
18+
19+
private void changeStateTo(State newState) {
20+
this.state = newState;
21+
this.state.onEnterState();
22+
}
23+
24+
@Override
25+
public String toString() {
26+
return "The mammoth";
27+
}
28+
29+
public void observe() {
30+
this.state.observe();
31+
}
32+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.iluwatar;
2+
3+
public class PeacefulState implements State {
4+
5+
private Mammoth mammoth;
6+
7+
public PeacefulState(Mammoth mammoth) {
8+
this.mammoth = mammoth;
9+
}
10+
11+
@Override
12+
public void observe() {
13+
System.out.println(String.format("%s is calm and peaceful.", mammoth));
14+
}
15+
16+
@Override
17+
public void onEnterState() {
18+
System.out.println(String.format("%s calms down.", mammoth));
19+
}
20+
21+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.iluwatar;
2+
3+
public interface State {
4+
5+
void onEnterState();
6+
7+
void observe();
8+
9+
}

0 commit comments

Comments
 (0)